在SpringBoot项目里加入PageHelper遇到的问题及其解决办法

分页插件PageHelper使用踩的坑,分页不成功,jar包冲突

首先引入依赖:

<!-- http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.4</version>
</dependency>

测试:

@Override
    public Result searchBolgList(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Blog> blogs = blogMapper.selectList(new QueryWrapper<Blog>().lambda().isNotNull(Blog::getId));
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        return Result.succ(pageInfo);
    }

通过Swagger查看结果并没有分页成功:
请添加图片描述

可能是没有配置,在application.yml加上如下配置后:

pagehelper:
  helper-dialect: mysql
  #设置为true时,pageNum<=0时会查询第一页,pageNum>pages时会查询最后一页
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

结果还是这样:
请添加图片描述
配置,代码写法是没有问题的,那问题就出在:

1:有其他分页插件在

2:jar包,也就是引入的依赖问题

我这里是没有配置其他分页插件的,那就是问题2了,经过一番搜查,找到如下:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</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>

导入jar包后,配置,代码不变

测试:代码运行报错:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.getLanguageDriver(MybatisMapperAnnotationBuilder.java:369)

The following method did not exist:

    com.baomidou.mybatisplus.core.MybatisConfiguration.getLanguageDriver(Ljava/lang/Class;)Lorg/apache/ibatis/scripting/LanguageDriver;

The method's class, com.baomidou.mybatisplus.core.MybatisConfiguration, is available from the following locations:

    jar:file:/C:/Users/00/.m2/repository/com/baomidou/mybatis-plus-core/3.2.0/mybatis-plus-core-3.2.0.jar!/com/baomidou/mybatisplus/core/MybatisConfiguration.class

It was loaded from the following location:

    file:/C:/Users/00/.m2/repository/com/baomidou/mybatis-plus-core/3.2.0/mybatis-plus-core-3.2.0.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of com.baomidou.mybatisplus.core.MybatisConfiguration

看这信息像是jar包冲突了,通过点击右侧Maven查看冲突:
请添加图片描述
请添加图片描述
这里只查看与分页插件冲突的jar,发现了两处冲突,秉承代码能运行就不要去动他的思想,我们原来的jar包不要变,只排除分页插件中这几个包(使用exclusions),做出如下修改:

 <!--  分页插件  -->
        <!-- http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</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>
            <exclusions>
                <exclusion>
                    <artifactId>mybatis</artifactId>
                    <groupId>org.mybatis</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>mybatis-spring</artifactId>
                    <groupId>org.mybatis</groupId>
                </exclusion>
            </exclusions>
        </dependency>

运行不报错了,测试分页,分页成功:
请添加图片描述
nice! 完美!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值