SpringBoot3中PageHelper分页不生效问题

今天在做一个小项目,引入 PageHelper 时踩了一个坑,记录一下。

解决方案参考:SpringBoot+MyBatis使用pagehelper分页插件及其注意事项(含解决分页不生效问题)

环境:

  • SpringBoot 3.2.0
  • JDK 17
  • Postgresql 15
  • PageHelper 1.2.12

依赖

1
2
3
4
5
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>

使用

1
2
3
4
5
 public Response<PageInfo<User>> queryAllUser(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.selectAll();
return ResponseUtil.success(new PageInfo<>(userList));
}

全部数据返回到前端,没有产生分页的效果。

看了大多数不生效的情况就是以下几种:

  1. 分页和真正的数据查询之间夹杂了其他操作
  2. 先执行了查询然后再设置的分页
  3. 依赖导入错误,导入的不是 pagehelper-spring-boot-starter

但是从自己的代码确认不是这类问题。

找了好久,看到 SpringBoot+MyBatis使用pagehelper分页插件及其注意事项(含解决分页不生效问题) 这篇博客末尾提到了使用分页拦截器,抱着试试看的心态,加了个一个配置类,确实生效了,原因未知。

1
2
3
4
5
6
7
8
9
@Configuration
public class PageHelperConfig {

@Bean
public Interceptor[] plugins() {
return new Interceptor[]{new PageInterceptor()};
}

}