版本提示:
pagehelper-spring-boot-starter1.x 版本线(最新 1.4.7)仅支持 Spring Boot 1.x/2.x(javax);Spring Boot 3.x(jakarta)需使用 2.x 版本线(2.0.0 起适配,当前最新 2.1.1)。
1.引入分页插件依赖
<!--pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>2.配置 yml
# 分页插件配置
pagehelper:
helperDialect: mysql
supportMethodsArguments: true3.使用分页插件,在查询前使用分页插件,原理:统一拦截sql,为其提供分页功能
/**
* page: 第几页
* pageSize: 每页显示条数
*/
PageHelper.startPage(page, pageSize);4.分页数据封装到PagedGridResult.java传给前端
PageInfo<?> pageList = new PageInfo<>(list);
PagedGridResult grid = new PagedGridResult();
grid.setPage(page);
grid.setRows(list);
grid.setTotal(pageList.getPages());
grid.setRecords(pageList.getTotal());| 特性 | 旧版(1.2.x) | 当前(2.x,如 2.1.1) |
|---|---|---|
| Spring Boot 兼容 | 仅 Boot 1.x/2.x(javax) | Boot 3.x(jakarta);Boot 1.x/2.x 项目请继续用 1.x 版本线 |
| JDK 支持 | JDK 8 | JDK 17+(Boot 3 基线) |
| 配置项 | helperDialect/supportMethodsArguments | 配置项保持兼容,新增 autoRuntimeDialect 等增强 |
| 与 MyBatis 版本 | mybatis 3.4.x 时代 | 支持 mybatis 3.5.x |
配置项在 yml 中仍使用
pagehelper前缀(kebab-case:helper-dialect、auto-dialect亦可);PageHelper.startPage(page, pageSize)的用法完全不变,注意分页线程安全:startPage后必须紧跟查询语句。