{T}

SpringBoot 整合 mybatis-pagehelper

版本提示pagehelper-spring-boot-starter 1.2.x 仅支持 Spring Boot 1.x/2.x(javax);Boot 3.x(jakarta)需使用 1.4.7+,JDK 17/21 支持需 1.4.7+,最新 4.x 系列(如 4.1.1)已适配 Boot 3.x。

1.引入分页插件依赖

xml
<!--pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>4.1.1</version>
</dependency>

2.配置 yml

yml
# 分页插件配置
pagehelper:
  helperDialect: mysql
  supportMethodsArguments: true

3.使用分页插件,在查询前使用分页插件,原理:统一拦截sql,为其提供分页功能

java
/**
 * page: 第几页
 * pageSize: 每页显示条数
 */
PageHelper.startPage(page, pageSize);

4.分页数据封装到PagedGridResult.java传给前端

java
PageInfo<?> pageList = new PageInfo<>(list);
PagedGridResult grid = new PagedGridResult();
grid.setPage(page);
grid.setRows(list);
grid.setTotal(pageList.getPages());
grid.setRecords(pageList.getTotal());

版本差异(pagehelper → 4.x)

特性旧版(1.2.12)当前(4.x)
Spring Boot 兼容仅 Boot 1.x/2.x(javax)Boot 3.x(jakarta),2.x 亦可
JDK 支持JDK 8JDK 8/11/17/21
配置项helperDialect/supportMethodsArguments新增 autoDialect、count 相关增强配置
与 MyBatis 版本mybatis 3.4.x 时代支持 mybatis 3.5.x

配置项在 yml 中仍使用 pagehelper 前缀(kebab-case:helper-dialectauto-dialect 亦可);PageHelper.startPage(page, pageSize) 的用法完全不变,注意分页线程安全:startPage 后必须紧跟查询语句。