1.controller编写
/**
* 根据条件分页查询与模糊查询
* @param page 当前页
* @param limit 每页的个数
* @param queryTeacherVo 自定义vo,为查询条件创建的类
* @return 统一返回结果对象
*/
@GetMapping(value = "getTeacherByPage/{page}/{limit}")
public Result getTeacherByPage(@PathVariable("page") Long page,
@PathVariable("limit") Long limit,
QueryTeacherVO queryTeacherVo){
Page<Teacher> teacherPage = new Page<>(page, limit);
teacherService.getTeahcerByPage(teacherPage,queryTeacherVo);//getTeahcerByPage自定义方法,
return Result.ok().data("teacherList",teacherPage.getRecords()).data("tatal",teacherPage.getTotal());
}
2.getTeahcerByPage方法的编写
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.orderByAsc("sort");
//如果没有条件,就只做简单的查询
if(queryTeacher == null){
baseMapper.selectPage(pageParam, wrapper);
return ;
}
//如果有条件,获取条件参数,把条件加到wrapper中
String name = queryTeacher.getName();
Integer level = queryTeacher.getLevel();
String begin = queryTeacher.getBegin();
String end = queryTeacher.getEnd();
//判断各参数是否有值,没有值,不管了;有值,把值加在wrapper中
if(!StringUtils.isEmpty(name)){
wrapper.like("name", name);
}
if(!StringUtils.isEmpty(level)){
wrapper.eq("level", level);
}
if(!StringUtils.isEmpty(begin)){
wrapper.ge("gmt_create", begin);
}
if(!StringUtils.isEmpty(end)){
wrapper.le("gmt_create", end);
}
baseMapper.selectPage(pageParam, wrapper);