分页插件

  • 自定义查询语句分页(自己写sql/mapper)
  • spring 注入 mybatis 配置分页插件
  1. @EnableTransactionManagement
  2. @Configuration
  3. @MapperScan("com.baomidou.cloud.service.*.mapper*")
  4. /**
  5. */
  6. @Bean
  7. public PaginationInterceptor paginationInterceptor() {
  8. return new PaginationInterceptor();
  9. }
  10. }

方式一 、传参区分模式【推荐】

  • UserServiceImpl.java 调用翻页方法,需要 page.setRecords 回传给页面
  1. // 不进行 count sql 优化,解决 MP 无法自动优化 SQL 问题
  2. // page.setOptimizeCountSql(false);
  3. // 不查询总记录数
  4. // page.setSearchCount(false);
  5. // 注意!! 分页 total 是经过插件自动 回写 到传入 page 对象
  6. return page.setRecords(userMapper.selectUserList(page, state));

方式二、ThreadLocal 模式【容易出错,不推荐】

  • PageHelper 使用方式如下:
// 开启分页
PageHelper.startPage(1, 2);
List<User> data = userService.findAll(params);
// 获取总条数
int total = PageHelper.getTotal();
// 获取总条数,并释放资源
int total = PageHelper.freeTotal();

JSON 序列化移除 transient 修饰的 Page 无关紧要的返回属性

page total 为 0 检查下是否加入 com.github.pagehelper 插件,有网友反馈它会干扰 MP 分页,请移除。