Springboot mybatis pagehelper分页不生效的问题

本文记录了在SpringBoot项目中使用PageHelper进行分页查询时遇到的问题及解决过程。通过调整依赖版本,最终成功实现了分页功能。

springboot版本

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->

按照正常的代码:

        PageHelper.startPage(pageNum,4);
        WechatAccountExample example = new WechatAccountExample();
        List<WechatAccount> accounts = wechatAccountMapper.selectByExample(example);
        PageInfo<WechatAccount>  pageInfo = new PageInfo<>(accounts);
        return pageInfo;

pagehelper配置

#pagehelper分页插件配置
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSqle

pom依赖

		<!--分页插件-->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>4.1.0</version>
		</dependency>

结果查出来还是没暖用,全部给查出来了。后来查了很多博客有人说加上

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

还是没用,有人说用一个依赖就行了,

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

还是不行!!!!!

无意中把版本改为了1.2.10,妈的出来了。

记录一下。。。

### SpringBootPageHelper 分页失效的原因及解决方案 #### 1. **依赖版本冲突** 如果 `pom.xml` 文件中引入的依赖存在版本不兼容的情况,可能会导致分页功能无法正常工作。例如,当 MyBatisMyBatis-Plus 同时被引入项目时,可能存在重复定义拦截器的现象,从而影响到 PageHelper 的正常使用[^3]。 解决方案是在引入 PageHelper 依赖时,通过 `<exclusions>` 排除可能引起冲突的组件: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> </exclusions> </dependency> ``` #### 2. **未正确配置拦截器** 在某些情况下,即使引入了正确的依赖,如果未在全局范围内注册 PageHelper 拦截器,则可能导致分页逻辑未能生效。通常可以通过以下方式完成拦截器的自动注入: 配置类中启用 PageInterceptor 注册: ```java @Configuration public class MyBatisConfig { @Bean public PageInterceptor pageInterceptor() { PageInterceptor interceptor = new PageInterceptor(); Properties properties = new Properties(); properties.setProperty("supportMethodsArguments", "true"); properties.setProperty("reasonable", "true"); interceptor.setProperties(properties); return interceptor; } } ``` #### 3. **SQL 查询语句不符合要求** PageHelper 插件仅支持特定形式的查询语句。如果 SQL 编写不当(如缺少必要的返回字段或条件),则可能导致分页失败。确保查询方法遵循标准结构并调用了对应的分页接口函数[^2]。 使用示例代码如下所示: ```java import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; List<User> userList = PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> userMapper.selectAll()); PageInfo<User> pageInfo = new PageInfo<>(userList); System.out.println("总记录数:" + pageInfo.getTotal()); ``` #### 4. **事务管理问题** 当数据库操作涉及分布式事务或者嵌套事务场景下,可能出现缓存机制干扰实际执行计划的情形,进而引发分页异常现象。此时建议调整隔离级别或将敏感部分单独封装成无状态服务单元运行[^1]。 --- ### 总结 综上所述,Spring Boot 应用程序中的 PageHelper 分页失效主要源于以下几个方面:依赖冲突、缺乏必要配置项以及潜在业务流程设计缺陷等问题。针对上述情况逐一排查可以有效定位具体故障所在位置,并采取相应措施加以修复。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值