分页插件
- 自定义查询语句分页(自己写sql/mapper)
- spring 注入 mybatis 配置分页插件
@EnableTransactionManagement
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
/**
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
方式一 、传参区分模式【推荐】
- UserServiceImpl.java 调用翻页方法,需要 page.setRecords 回传给页面
// 不进行 count sql 优化,解决 MP 无法自动优化 SQL 问题
// page.setOptimizeCountSql(false);
// 不查询总记录数
// page.setSearchCount(false);
// 注意!! 分页 total 是经过插件自动 回写 到传入 page 对象
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 分页,请移除。