SpringBoot---myBatis数据库操作

一,分页查询

  • 现在controller中设置参数,@RequestParam(defaultValue = "1") 设置默认值
@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实现类中
@Override
    public PageBean page(Integer page, Integer pageSize,String name, Short gender, LocalDate begin, LocalDate end) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

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

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
  • 再Mapper接口中
public List list(String name, Short gender, LocalDate begin, LocalDate end);
  • 然后再resources下创建xml文件
  •  
        
    

你可能感兴趣的:(spring,boot,mybatis,数据库)