1、写一个配置类,配置分页插件
@Configuration
@MapperScan("com.atguigu.eduservice.mapper") //路径为mapper文件所在的包
public class EduConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
2、编写分页方法
一:不带条件
@Autowired
private EduTeacherService eduTeacherService;
//分页查询讲师的方法
@GetMapping("/pageTeacher/{current}/{limit}")
public R pageListTeacher(@PathVariable long current,
@PathVariable long limit) {
Page<EduTeacher> pageTeacher = new Page<>(current, limit);
//调用方法实现分页
eduTeacherService.page(pageTeacher, null);
long total = pageTeacher.getTotal();//总记录数
List<EduTeacher> records = pageTeacher.getRecords();
return R.ok().data("total", total).data("row", records);
}
带条件:(建议建一个条件的实体类)
@ApiModel(value = "Teacher查询对象", description = "讲师查询对象封装")
@Data
public class TeacherQuery {
private static final long serialVersionUID = 1L;
@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;
}