Spring Boot 整合Pagehelper(为什么PageHelper分页不生效)

pom.xml文件中分页依赖使用:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

 

不能直接使用ssm框架中

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

<version>5.1.4</version>

</dependency>

--------------------- 
作者:Z先生~ 
来源:优快云 
原文:https://blog.youkuaiyun.com/double7z/article/details/80339925 
版权声明:本文为博主原创文章,转载请附上博文链接!

### PageHelper 分页查询不生效的原因及解决方案 #### 1. 检查依赖配置 确保 `pom.xml` 中包含了正确的依赖项。对于 Spring Boot 环境,推荐使用 `pagehelper-spring-boot-starter` 而不是旧版本的 `pagehelper`[^1]。 ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>最新版本号</version> </dependency> ``` #### 2. 配置文件设置 确认 `application.yml` 或者 `application.properties` 文件中的配置正确无误: ```yaml # application.yml 示例 mybatis: configuration: map-underscore-to-camel-case: true pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql ``` #### 3. 插件注册验证 在启动类或 MyBatis 的配置类中确保已经启用了 PageHelper 插件。通常情况下不需要手动添加此部分代码,因为 starter 已经自动处理了这一点;但如果遇到问题可以尝试显式声明: ```java import com.github.pagehelper.PageInterceptor; import org.apache.ibatis.plugin.Interceptor; import org.springframework.context.annotation.Bean; public class MyBatisConfig { @Bean public Interceptor pageHelper() { return new PageInterceptor(); } } ``` #### 4. SQL语句位置调整 当调用分页方法前执行其他SQL操作可能会导致PageHelper失效。应保证分页逻辑紧随其后的第一个select语句被执行[^3]。 #### 5. 缓存机制影响 如果开启了MyBatis的一级/二级缓存,在某些特定条件下也可能干扰到PageHelper的工作效果。可以通过禁用缓存来测试是否为此原因引起的问题[^2]: ```sql <select id="findUsers" resultType="User"> <!-- 关闭当前statement级别的cache --> ${_databaseId}_NO_CACHE_ SELECT * FROM users WHERE ... </select> ``` 以上措施有助于排查并修复PageHelper分页功能无法正常工作的情况。建议逐一检验上述各项设定直至找到根本所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值