引入maven依赖
按照惯例,需要引入SpringBoot的pagehelper依赖。
复制代码 com.github.pagehelper pagehelper-spring-boot-starter 1.2.3
请注意版本号,与Mybatis不兼容的版本号可能会导致异常
配置pagehelper参数
#mybatis配置mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: online.suiyu.mybatisdemo.entityserver: port: 8083#配置pagehelperpagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql复制代码
详细配置参数参考
依次修改Controller、Service,添加分页的参数
- Controller层
@ApiOperation(value = "getAllUser", notes = "获取所有的内容", tags = "user")@RequestMapping(value = "/getalluser", method = RequestMethod.GET)public ListgetAllUser(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "4") int pageSize) { return userService.getAllUser(pageNum, pageSize);}复制代码
- Service层
@Servicepublic class UserService { @Autowired private UserMapper userMapper; public ListgetAllUser(int pageNum, int pageSize) { //这是整个过程中最核心的一条语句 PageHelper.startPage(pageNum, pageSize); return userMapper.getAllUser(); }}复制代码
到此完成,顺便一提,当pageNum<=0
时,加载第一页,当pageNum>最后一页
则加载最后一页。这是有配置文件中的reasonable: true
决定的!
到此,整合完成!