分页插件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! 完美!