一、分页

1、配置分页插件

  1. service-edu模块下的EduConfig
  1. package com.atguigu.eduservice.config;
  2. @EnableTransactionManagement
  3. @Configuration
  4. @MapperScan("com.atguigu.eduservice.mapper")
  5. public class EduConfig {
  6. /**
  7. * 分页插件
  8. */
  9. @Bean
  10. public PaginationInterceptor paginationInterceptor() {
  11. return new PaginationInterceptor();
  12. }
  13. }

2、分页Controller方法

  1. EduTeacherController中添加分页方法
  1. @ApiOperation(value = "分页讲师列表")
  2. @GetMapping("pageTeacher/{current}/{limit}")
  3. public R pageListTeacher(
  4. @ApiParam(name = "page", value = "当前页码", required = true)
  5. @PathVariable Long page,
  6. @ApiParam(name = "limit", value = "每页记录数", required = true)
  7. @PathVariable Long limit){
  8. //创建page对象
  9. Page<Teacher> pageParam = new Page<>(page, limit);
  10. //调用方法实现分页
  11. //调用方法时候,底层封装,把分页所有数据封装到pageTeacher对象里面
  12. teacherService.page(pageParam, null);
  13. List<Teacher> records = pageParam.getRecords(); //数据list集合
  14. long total = pageParam.getTotal(); //总记录数
  15. //Map map = new HashMap();
  16. //map.put("total",total);
  17. //map.put("rows",records);
  18. //return R.ok().data(map);
  19. return R.ok().data("total", total).data("rows", records);
  20. }

3、Swagger中测试

二、条件查询

  1. 根据讲师名称name,讲师头衔level、讲师入驻时间gmt_create(时间段)查询<br />![图片.png](https://cdn.nlark.com/yuque/0/2021/png/22523384/1635951303433-62365708-ae65-4974-972d-fcb18e1c368a.png#clientId=u29d9fa0e-fa8d-4&from=paste&height=510&id=u1c21ae08&margin=%5Bobject%20Object%5D&name=%E5%9B%BE%E7%89%87.png&originHeight=510&originWidth=1232&originalType=binary&ratio=1&size=24904&status=done&style=none&taskId=ub7a6dcd3-3d54-4a46-9aab-2c238e44da4&width=1232)

1、创建查询对象

创建com.atguigu.eduservice.entity.vo包,创建TeacherQuery.java查询对象

  1. package com.atguigu.eduservice.entity.vo;
  2. import io.swagger.annotations.ApiModelProperty;
  3. import lombok.Data;
  4. @ApiModel(value = "Teacher查询对象", description = "讲师查询对象封装")
  5. @Data
  6. public class TeacherQuery {
  7. @ApiModelProperty(value = "教师名称,模糊查询")
  8. private String name;
  9. @ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
  10. private Integer level;
  11. @ApiModelProperty(value = "查询开始时间", example = "2019-01-01 10:10:10")
  12. private String begin;//注意,这里使用的是String类型,前端传过来的数据无需进行类型转换
  13. @ApiModelProperty(value = "查询结束时间", example = "2019-12-01 10:10:10")
  14. private String end;
  15. }

2、service

2.1 接口

  1. package com.guli.edu.service;
  2. public interface TeacherService extends IService<Teacher> {
  3. void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery);
  4. }

2.2 实现

  1. package com.guli.edu.service.impl;
  2. @Service
  3. public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, EduTeacher> implements TeacherService {
  4. @Override
  5. public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
  6. QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
  7. queryWrapper.orderByAsc("gmt_create");
  8. if (teacherQuery == null){
  9. baseMapper.selectPage(pageParam, queryWrapper);
  10. return;
  11. }
  12. String name = teacherQuery.getName();
  13. Integer level = teacherQuery.getLevel();
  14. String begin = teacherQuery.getBegin();
  15. String end = teacherQuery.getEnd();
  16. if (!StringUtils.isEmpty(name)) {
  17. queryWrapper.like("name", name);
  18. }
  19. if (!StringUtils.isEmpty(level) ) {
  20. queryWrapper.eq("level", level);
  21. }
  22. if (!StringUtils.isEmpty(begin)) {
  23. queryWrapper.ge("gmt_create", begin);
  24. }
  25. if (!StringUtils.isEmpty(end)) {
  26. queryWrapper.le("gmt_create", end);
  27. }
  28. baseMapper.selectPage(pageParam, queryWrapper);
  29. }
  30. }

3、controller

EduTeacherController中新增pageTeacherCondition方法:
增加参数TeacherQuery,非必选

  1. @ApiOperation(value = "分页讲师列表")
  2. @GetMapping("{page}/{limit}")
  3. public R pageTeacherCondition(
  4. @ApiParam(name = "page", value = "当前页码", required = true)
  5. @PathVariable Long page,
  6. @ApiParam(name = "limit", value = "每页记录数", required = true)
  7. @PathVariable Long limit,
  8. @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
  9. TeacherQuery teacherQuery){
  10. //创建page对象
  11. Page<EduTeacher> pageParam = new Page<>(page, limit);
  12. //调用方法实现条件查询分页
  13. teacherService.pageQuery(pageParam, teacherQuery);
  14. List<EduTeacher> records = pageParam.getRecords();
  15. long total = pageParam.getTotal();
  16. return R.ok().data("total", total).data("rows", records);
  17. }
  1. package com.atguigu.eduservice.controller;
  2. @Api(description="讲师管理")
  3. @RestController
  4. @RequestMapping("/eduservice/teacher")
  5. public class EduTeacherController {
  6. //把service注入
  7. @Autowired
  8. private EduTeacherService teacherService;
  9. //4 条件查询带分页的方法
  10. @PostMapping("pageTeacherCondition/{current}/{limit}")
  11. public R pageTeacherCondition(@PathVariable long current, //当前页码
  12. @PathVariable long limit, //每页记录数
  13. @RequestBody(required = false) TeacherQuery teacherQuery) {
  14. //创建page对象
  15. Page<EduTeacher> pageTeacher = new Page<>(current,limit);
  16. //构建条件
  17. QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
  18. // 多条件组合查询
  19. // mybatis学过 动态sql
  20. String name = teacherQuery.getName();
  21. Integer level = teacherQuery.getLevel();
  22. String begin = teacherQuery.getBegin();
  23. String end = teacherQuery.getEnd();
  24. //判断条件值是否为空,如果不为空拼接条件
  25. if(!StringUtils.isEmpty(name)) {
  26. //构建条件
  27. wrapper.like("name",name);
  28. }
  29. if(!StringUtils.isEmpty(level)) {
  30. wrapper.eq("level",level);
  31. }
  32. if(!StringUtils.isEmpty(begin)) {
  33. wrapper.ge("gmt_create",begin);
  34. }
  35. if(!StringUtils.isEmpty(end)) {
  36. wrapper.le("gmt_create",end);
  37. }
  38. //排序
  39. wrapper.orderByDesc("gmt_create");
  40. //调用方法实现条件查询分页
  41. teacherService.page(pageTeacher,wrapper);
  42. long total = pageTeacher.getTotal(); //总记录数
  43. List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
  44. return R.ok().data("total",total).data("rows",records);
  45. }
  46. }

4、Swagger中测试