一、分页
1、配置分页插件
service-edu模块下的EduConfig类
package com.atguigu.eduservice.config;
@EnableTransactionManagement
@Configuration
@MapperScan("com.atguigu.eduservice.mapper")
public class EduConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
2、分页Controller方法
EduTeacherController中添加分页方法
@ApiOperation(value = "分页讲师列表")
@GetMapping("pageTeacher/{current}/{limit}")
public R pageListTeacher(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit){
//创建page对象
Page<Teacher> pageParam = new Page<>(page, limit);
//调用方法实现分页
//调用方法时候,底层封装,把分页所有数据封装到pageTeacher对象里面
teacherService.page(pageParam, null);
List<Teacher> records = pageParam.getRecords(); //数据list集合
long total = pageParam.getTotal(); //总记录数
//Map map = new HashMap();
//map.put("total",total);
//map.put("rows",records);
//return R.ok().data(map);
return R.ok().data("total", total).data("rows", records);
}
3、Swagger中测试
二、条件查询
根据讲师名称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查询对象
package com.atguigu.eduservice.entity.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@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 begin;//注意,这里使用的是String类型,前端传过来的数据无需进行类型转换
@ApiModelProperty(value = "查询结束时间", example = "2019-12-01 10:10:10")
private String end;
}
2、service
2.1 接口
package com.guli.edu.service;
public interface TeacherService extends IService<Teacher> {
void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery);
}
2.2 实现
package com.guli.edu.service.impl;
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, EduTeacher> implements TeacherService {
@Override
public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("gmt_create");
if (teacherQuery == null){
baseMapper.selectPage(pageParam, queryWrapper);
return;
}
String name = teacherQuery.getName();
Integer level = teacherQuery.getLevel();
String begin = teacherQuery.getBegin();
String end = teacherQuery.getEnd();
if (!StringUtils.isEmpty(name)) {
queryWrapper.like("name", name);
}
if (!StringUtils.isEmpty(level) ) {
queryWrapper.eq("level", level);
}
if (!StringUtils.isEmpty(begin)) {
queryWrapper.ge("gmt_create", begin);
}
if (!StringUtils.isEmpty(end)) {
queryWrapper.le("gmt_create", end);
}
baseMapper.selectPage(pageParam, queryWrapper);
}
}
3、controller
EduTeacherController中新增pageTeacherCondition方法:
增加参数TeacherQuery,非必选
@ApiOperation(value = "分页讲师列表")
@GetMapping("{page}/{limit}")
public R pageTeacherCondition(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit,
@ApiParam(name = "teacherQuery", value = "查询对象", required = false)
TeacherQuery teacherQuery){
//创建page对象
Page<EduTeacher> pageParam = new Page<>(page, limit);
//调用方法实现条件查询分页
teacherService.pageQuery(pageParam, teacherQuery);
List<EduTeacher> records = pageParam.getRecords();
long total = pageParam.getTotal();
return R.ok().data("total", total).data("rows", records);
}
package com.atguigu.eduservice.controller;
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
//把service注入
@Autowired
private EduTeacherService teacherService;
//4 条件查询带分页的方法
@PostMapping("pageTeacherCondition/{current}/{limit}")
public R pageTeacherCondition(@PathVariable long current, //当前页码
@PathVariable long limit, //每页记录数
@RequestBody(required = false) TeacherQuery teacherQuery) {
//创建page对象
Page<EduTeacher> pageTeacher = new Page<>(current,limit);
//构建条件
QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
// 多条件组合查询
// mybatis学过 动态sql
String name = teacherQuery.getName();
Integer level = teacherQuery.getLevel();
String begin = teacherQuery.getBegin();
String end = teacherQuery.getEnd();
//判断条件值是否为空,如果不为空拼接条件
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);
}
//排序
wrapper.orderByDesc("gmt_create");
//调用方法实现条件查询分页
teacherService.page(pageTeacher,wrapper);
long total = pageTeacher.getTotal(); //总记录数
List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
return R.ok().data("total",total).data("rows",records);
}
}