SpringBoot---myBatis数据库操作

一,分页查询

  • 现在controller中设置参数,@RequestParam(defaultValue = "1") 设置默认值
java 复制代码
@GetMapping
public Result page(@RequestParam(defaultValue = "1") Integer page,
                 @RequestParam(defaultValue = "10") Integer pageSize,
                 String name, Short gender,
                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
log.info("分页查询, 参数: {},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
//调用service分页查询
PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
return Result.success(pageBean);
}
  • 然后再service实现类中
java 复制代码
@Override
    public PageBean page(Integer page, Integer pageSize,String name, Short gender, LocalDate begin, LocalDate end) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

        //2. 执行查询
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
  • 再Mapper接口中
java 复制代码
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
  • 然后再resources下创建xml文件
XML 复制代码
 <!--条件查询-->
    <select id="list" resultType="com.itheima.pojo.Emp">
        select *
        from emp
        <where>
          <if test="name != null and name != ''">
              name like concat('%', #{name}, '%')
          </if>
          <if test="gender != null">
              and gender = #{gender}
          </if>
          <if test="begin != null and end != null">
              and entrydate between #{begin} and #{end}
          </if>
        </where>
        order by update_time desc
    </select>
相关推荐
言之。11 分钟前
【Django】REST 常用类
数据库·django·sqlite
程序员小刚15 分钟前
基于SpringBoot + Vue 的作业管理系统
vue.js·spring boot·后端
问道飞鱼1 小时前
【Springboot知识】Springboot计划任务Schedule详解
java·spring boot·后端·schedule
VB.Net1 小时前
C# 综合示例 库存管理系统20 操作员管理(FormAdmin)
开发语言·数据库·c#
码熔burning2 小时前
【MongoDB篇】MongoDB的聚合框架!
数据库·mongodb·nosql
凌叁儿2 小时前
使用PyMongo连接MongoDB的基本操作
数据库·python·mongodb
2401_837088502 小时前
Mysql order by 用法
数据库·mysql
程序员buddha3 小时前
【Spring Boot】Spring Boot + Thymeleaf搭建mvc项目
spring boot·后端·mvc
认真的小羽❅3 小时前
Spring Boot Validation实战详解:从入门到自定义规则
java·数据库·spring boot
MaCa .BaKa3 小时前
36-校园反诈系统(小程序)
java·spring boot·mysql·小程序·vue·maven·uniapp