1.前情提要:
项目之前使用的分页是Mybatis-plus再带的,现在想加入mybatis+PageHelper插件,进行分页
2.开始实施
首先添加分页插件的maven
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
<exclusions>
<exclusion>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
</exclusion>
</exclusions>
</dependency>
PageHelper.startPage(sysLog.getPageNo(),sysLog.getPageSize());
List<SysLog> pageList = logMapper.logPageList(sysLog);
PageInfo<SysLog> info = new PageInfo<SysLog>(pageList);
实现效果:
启动项目没有报错,但是分页不生效
其中看过博客日志无数来处理这个问题,最多的就是
PageHelper.startPage这个方法要在查询条件之前,但是对于我这个项目分页无效
3.继续操作
将分页的pom maven换成
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
换了pom 文件后分页确实可以使用了,本来感觉已经解决问题,但是另一哥们说他的mybatisplus自带分页报错失效了
报错问题:
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: net.sf.jsqlparser.statement.select.PlainSelect.getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: net.sf.jsqlparser.statement.select.PlainSelect.getGroupByColumnReferences()Ljava/util/List;
这两个问题都是因为jsqparser包版本问题导致的
4.继续处理版本问题
查看自己项目的mybtisplus中引用的是4.0的jsqparser包
所以在引用分页插件的时候要处理好分页的maven包版本
经过不断的测试,终于找到了分页插件1.3.1版本对应的4.0 jsqparser包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.1</version>
<!--使用spring boot2整合 pagehelper-spring-boot-starter必须排除一下依赖
因为pagehelper-spring-boot-starter也已经在pom依赖了mybatis与mybatis-spring
所以会与mybatis-plus-boot-starter中的mybatis与mybatis-spring发生冲突
-->
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
同时将mybatis的包也给去除了,防止有冲突
启动正常,测试mybatisplus自带分页显示正常,mybatis+分页插件也正常
感受:重要的是两者的版本要一致!!!