【踩坑】mybatisplus3.x 和 pagehelper 结合,分页失效的问题

背景

接手的这个项目, 由于开发的人好几个, 风格不一样, 里面既有mybatis, 也有mybatisplus, 分页大多用的是pagehelper

我一开始是将原本的mybatisplus2.x版本升级了下, 变成``3.x`, 然后写了个测试方法测试分页:

//这个是mybatisplus自带的分页功能
 IPage<Student> page = new Page<>(1,10);
        IPage<Student> page1 = studentService.page(page);
        System.out.println("page1 = " + page1.getRecords().size());
        assert page1.getRecords().size() == 10;

//同时要保证原本pagehelper的分页还能用
 PageHelper.startPage(1,10);
        List<CoursesVO> list = mystudyMapper.findCourses();
        PageInfo<CoursesVO> pageInfo = new PageInfo<>(list);
        System.out.println("pageInfo = " + pageInfo.getList().size());
        assert pageInfo.getList().size() == 10;

结果是, 没有报错, 但是发现list是查回来所有结果, 且total是0

解决

最终各种找资料, 排列组合+枚举, 加每次超长的编译等待时间, 搞了2个下午加1个晚上加1个上午的时间. 总算搞定了

1. 依赖

先说最终可行的方案:
首先依赖:

 <!-- mybatis plus与其 代码生成器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
              <version>3.5.2</version>
                <scope>compile</scope>
        </dependency>
        <!--mybatis-plus分页-->
        <!--pagehelper的1.3.1版本和mybatisplus的3.4.2版本不会有jar冲突, 主要是jsqlparser都会是4.0版本-->

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
		<!-- 下面2个依赖用于声明版本, 可放到parent 的pom的dependencyManagement-->
   		<dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <!--mybatis-plus 3.4.2对应的mybatis是3.5.6-->
                <version>3.5.6</version>
            </dependency>
             <dependency>
                <!--mybatisplus 3.4.2 版本 要和4.0的jsqlparser适配-->
                <groupId>com.github.jsqlparser</groupId>
                <artifactId>jsqlparser</artifactId>
                <version>4.0</version>
            </dependency>

首先是依赖冲突, 将pagehelper里面的mybatis几个依赖排除
其次是jsqlparser, pagehelper里的这个版本很低, 我试了下要匹配mybatisplus的3.4.2版本, 需要pagehelper的1.3.1版本

2. 配置

这个可以说是最蛋疼的了, 因为本身mbplus的高级版本就有不同的分页配置方式, 在我不停的切换mybatisplus版本的时候, 随之带来的是更多的排列组合的配置.

//高版本 3.4.2 我用的这个
    @Bean("mybatisPlusPageInterceptor")
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
//低版本 3.3.0用的下面这个
//    @Bean("mybatisPlusPageInterceptor")
//    public PaginationInterceptor paginationInterceptor() {
//        return new PaginationInterceptor();
//    }

pagehelper的配置不用改

#pagehelper分页插件
pagehelper:
  reasonable: true
  helperDialect: mysql
  supportMethodsArguments: true
  params: count=countSql

接着就是最容易被忽视的, 也是我最后加上才成功的

    @Bean(name = "dataSqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        // 解决 Invalid bound statement 问题
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();

        bean.setDataSource(dataSource);

		//重点加上这个bean的配置
        Interceptor[] plugins = {mybatisPlusPageInterceptor};
        bean.setPlugins(plugins);

        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*.xml"));
        return bean.getObject();
    }
要在Spring Boot项目中导入com.github.pagehelper.PageHelper,你需要按照以下步骤进行操作: 1. 在你的项目的pom.xml文件中,添加以下依赖项: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 2. 在你的Spring Boot应用程序的配置文件(通常是application.properties或application.yml)中,配置PageHelper的属性。例如,你可以添加以下属性: ```properties # 开启PageHelper支持 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql ``` 这些属性可以根据你的具体需求进行配置。 3. 在你的Mapper接口中使用PageHelper来实现分页查询。例如: ```java import com.github.pagehelper.PageHelper; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface YourMapper { // 分页查询方法示例 List<YourEntity> selectByPage(int pageNum, int pageSize); } ``` 4. 在你的Service或Controller层调用Mapper接口中的分页查询方法,并传入页码和每页大小参数。例如: ```java import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class YourService { @Autowired private YourMapper yourMapper; public PageInfo<YourEntity> getEntitiesByPage(int pageNum, int pageSize) { // 使用PageHelper进行分页查询 PageHelper.startPage(pageNum, pageSize); List<YourEntity> entities = yourMapper.selectByPage(pageNum, pageSize); return new PageInfo<>(entities); } } ``` 这样,你就成功地在Spring Boot项目中导入了com.github.pagehelper.PageHelper,并可以使用它进行分页查询了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值