一 查询条件VO
/**
* @className: TeacherQueryVo
* @description: 讲师查询条件封装
* @date: 2020/11/18
* @author: cakin
*/
@ApiModel("Teacher查询对象")
@Data
public class TeacherQueryVo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "讲师姓名")
private String name;
@ApiModelProperty(value = "讲师级别")
private Integer level;
@ApiModelProperty(value = "开始时间")
private String joinDateBegin;
@ApiModelProperty(value = "结束时间")
private String joinDateEnd;
}
二 讲师控制器中的分页和条件查询
/**
* 功能描述:分页和条件查询
*
* @param page 当前页码
* @param limit 每页记录数
* @param teacherQueryVo 查询条件封装
* @return R 返回给前端的数据
* @author cakin
* @date 2020/11/18
*/
@ApiOperation("讲师分页列表")
@GetMapping("list/{page}/{limit}")
public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
@ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
@ApiParam("讲师列表查询对象") TeacherQueryVo teacherQueryVo) {
// 分页参数
Page<Teacher> pageParam = new Page<>(page, limit);
IPage<Teacher> pageModel = teacherService.selectPage(pageParam, teacherQueryVo);
List<Teacher> records = pageModel.getRecords();
long total = pageModel.getTotal();
return R.ok().data("total", total).data("rows", records);
}
三 讲师服务层实现
/**
* @className: TeacherServiceImpl
* @description: 讲师 服务实现类
* @date: 2020/11/18
* @author: cakin
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
/**
* 功能描述:显示分页查询列表
*
* @author cakin
* @date 2020/11/18
* @param pageParam 分页查询参数
* @param teacherQueryVo 条件查询参数
* @return IPage<Teacher> 分页信息
*/
@Override
public IPage<Teacher> selectPage(Page<Teacher> pageParam, TeacherQueryVo teacherQueryVo) {
// 1 排序:按照sort字段排序
QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("sort");
// 2 有分页查询 无条件查询
if (teacherQueryVo == null) {
return baseMapper.selectPage(pageParam, queryWrapper);
}
// 3 构造条件查询
String name = teacherQueryVo.getName();
Integer level = teacherQueryVo.getLevel();
String joinDateBegin = teacherQueryVo.getJoinDateBegin();
String joinDateEnd = teacherQueryVo.getJoinDateEnd();
if (!StringUtils.isEmpty(name)) {
queryWrapper.likeRight("name", name);
}
if (level != null) {
queryWrapper.eq("level", level);
}
if (!StringUtils.isEmpty(joinDateBegin)) {
queryWrapper.ge("join_date", joinDateBegin);
}
if (!StringUtils.isEmpty(joinDateEnd)) {
queryWrapper.le("join_date", joinDateEnd);
}
// 2 有分页查询 有条件查询
return baseMapper.selectPage(pageParam, queryWrapper);
}
四 Swagger测试
查询结果
{
"success": true,
"code": 20000,
"message": "成功",
"data": {
"total": 2,
"rows": [{
"id": "2",
"gmtCreate": "2018-03-30 18:28:26",
"gmtModified": "2020-04-14 17:40:36",
"name": "周润发",
"intro": "中国人民大学附属中学数学一级教师",
"career": "中国科学院数学与系统科学研究院应用数学专业博士,研究方向为数字图像处理,中国工业与应用数学学会会员。参与全国教育科学“十五”规划重点课题“信息化进程中的教育技术发展研究”的子课题“基与课程改革的资源开发与应用”,以及全国“十五”科研规划全国重点项目“掌上型信息技术产品在教学中的运用和开发研究”的子课题“用技术学数学”。",
"level": 2,
"avatar": "https://guli-file-helen.oss-cn-beijing.aliyuncs.com/avatar/2020/04/14/f606ed5b-1d46-43a1-945c-3e1b3b58fc0a.jpg",
"sort": 1,
"joinDate": "2019-10-28",
"deleted": false
},
{
"id": "4",
"gmtCreate": "2018-04-03 14:13:51",
"gmtModified": "2019-10-29 19:52:46",
"name": "周杰伦",
"intro": "长期从事考研政治课讲授和考研命题趋势与应试对策研究。考研辅导新锐派的代表。",
"career": "政治学博士、管理学博士后,北京师范大学马克思主义学院副教授。多年来总结出了一套行之有效的应试技巧与答题方法,针对性和实用性极强,能帮助考生在轻松中应考,在激励的竞争中取得高分,脱颖而出。",
"level": 1,
"avatar": "https://online-teach-file-helen.oss-cn-beijing.aliyuncs.com/avatar/2019/11/25/fee1e99f-8852-4da0-a256-9732e55bb608.jpg",
"sort": 1,
"joinDate": "2019-10-27",
"deleted": false
}
]
}
}