MyBatis Plus实现带条件分页查询
TeacherQuery
条件字段封装
/**
* <p>
* Description:
* </p>
*
* @author songzixian
* @version v1.0.0
* @create 2020-05-24 18:19
* @see com.greatdata.provider.domain
*/
@ApiModel(value = "Teacher查询对象",description = "讲师查询对象封装")
@Data
public class TeacherQuery {
@ApiModelProperty(value = "教师名称,模糊查询")
private String name;
@ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
private Integer level;
@ApiModelProperty(value = "查询开始时间", example = "2019-01-01 10:10:10")
private String gmtCreate;
@ApiModelProperty(value = "查询结束时间", example = "2020-12-01 10:10:10")
private String gmtModified;
}
Controller
层
/**
* 带条件的讲师分页查询方法
* @return
*/
@ApiOperation("带条件的讲师分页查询")
@GetMapping("pageListTeacherCondition/{current}/{limit}")
public ResponseResult<Page<EduTeacher>> pageListTeacherCondition(@PathVariable long current,
@PathVariable Long limit,
TeacherQuery teacherQuery){
Page<EduTeacher> eduTeacherPage = eduTeacherService.pageListTeacherCondition(current, limit, teacherQuery);
return new ResponseResult(ResponseResult.CodeStatus.OK,"讲师条件方法查询成功",eduTeacherPage);
}
servicei
层
/**
* 带条件的讲师分页查询
* @param current
* @param limit
* @param teacherQuery
* @return
*/
Page<EduTeacher> pageListTeacherCondition(long current, Long limit, TeacherQuery teacherQuery);
serviceimpl
层
/**
* 带条件的分页查询
* @param current
* @param limit
* @param teacherQuery
* @return
*/
@Override
public Page<EduTeacher> pageListTeacherCondition(long current, Long limit, TeacherQuery teacherQuery){
//创建Page对象
Page<EduTeacher> eduTeacherPage = new Page<>(current,limit);
//构建条件
QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
//获取传入讲师的条件是否为空
//讲师名
String name = teacherQuery.getName();
//讲师级别
Integer level = teacherQuery.getLevel();
//开始时间
String gmtCreate = teacherQuery.getGmtCreate();
//结束时间
String gmtModified = teacherQuery.getGmtModified();
//多条件组合查询
//判断条件值是否为空,如果不为空拼接条件
if (!StringUtils.isEmpty(name)){
//构建条件 模糊查询
wrapper.like("name",name);
}
if (!StringUtils.isEmpty(level)){
//等于
wrapper.eq("level",level);
}
if (!StringUtils.isEmpty(gmtCreate)){
//大于等于
wrapper.ge("gmt_create",gmtCreate);
}
if (!StringUtils.isEmpty(gmtModified)){
//小于等于
wrapper.le("gmt_modified",gmtModified);
}
//调用mybatis plus分页方法进行查询
eduTeacherMapper.selectPage(eduTeacherPage,wrapper);
//通过Page对象获取分页信息
//long current = page.getCurrent();//当前页
List<EduTeacher> records = eduTeacherPage.getRecords(); //每页的数据 list集合
//long size = eduTeacherPage.getSize(); //每页显示的条数
long total = eduTeacherPage.getTotal(); //总记录数
long pages = eduTeacherPage.getPages(); //总页数
boolean pagehasNext = eduTeacherPage.hasNext(); //下一页
boolean pagehasPrevious = eduTeacherPage.hasPrevious(); //上一页
return eduTeacherPage;
}
访问方式GET
请求路径:/eduservice/teacher/pageListTeacherCondition/1/5?current=1&limit=5&gmtCreate=2019-01-01 10:10:10&gmtModified=2020-02-01 10:10:10&level=1&name=宋
响应结果