MybatisPlus和pagehelper分页冲突—关于jsqlparser、pagehelper、MybatisPlus三者的版本兼容问题
1 问题描述
今天在集成pagehelper依赖时,遇到问题,程序查询数据库时报错找不到jsqlparser类,下面是项目相关依赖:
<properties> <mybatis-plus.version>3.5.7</mybatis-plus.version><pagehelper.boot.version>2.1.1</pagehelper.boot.version></properties>
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>${mybatis-plus.version}</version></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>${pagehelper.boot.version}</version></dependency>
2 解决方法
2.1 问题原因
使用Maven Helper插件查看pom文件的依赖,发现依赖冲突:
pagehelper-spring-boot-starter依赖集成的jsqlparser是4.7版本(这也是目前pagehelper-spring-boot-starter最新版本2.1.1的支持依赖,截止2025-10-5)。
而mybatis-plus-spring-boot3-starter集成的jsqlparser是4.9版本,因此低版本无法向高版本兼容。
对于pagehelper-spring-boot-starter内部也集成了mybatis-plus-spring-boot-starter的依赖,但是版本较低:
2.2 解决方案
2.2.1 方案1
放弃使用pagehelper,mybatisplus的分页功能也很强大,直接使用mybatisplus即可。
2.2.2 方案2
在发生依赖冲突的地方,右键,选择Exclude排除冲突依赖:
只能通过这种方式排除pagehelper-spring-boot-starter依赖集成的mybatis-plus依赖与项目中集成的mybatis-plus依赖版本兼容冲突。
由于pagehelper-spring-boot-starter依赖目前最高只支持4.7版本的jsqlparser,因此不能只排除pagehelper-spring-boot-starter依赖的jsqlparser冲突,同时需要排除mybatis-plus依赖的jsqlparser冲突:
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>${mybatis-plus.version}</version><exclusions><exclusion><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>${pagehelper.boot.version}</version><exclusions><exclusion><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId></exclusion><exclusion><artifactId>mybatis</artifactId><groupId>org.mybatis</groupId></exclusion><exclusion><artifactId>mybatis-spring</artifactId><groupId>org.mybatis</groupId></exclusion></exclusions></dependency><!-- 此处需要指定jsqlparser版本为4.7,防止pagehelper集成的jsqlparser版本与mybatisplus集成的jsqlparser版本冲突 --><dependency><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId><version>${jsqlparser.version}</version></dependency>
<properties> <jsqlparser.version>4.7</jsqlparser.version><mybatis-plus.version>3.5.7</mybatis-plus.version><pagehelper.boot.version>2.1.1</pagehelper.boot.version></properties>
2.2.3 方案3
不使用pagehelper-spring-boot-starter依赖,直接使用pagehelper依赖,排除mybatis-plus集成的jsqlparser,并引入jsqlparser依赖与mybatis-plus、pagehelper兼容的版本:
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>${mybatis-plus.version}</version><exclusions><exclusion><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>${pagehelper.version}</version></dependency><!-- 此处需要指定jsqlparser版本为4.7,防止pagehelper集成的jsqlparser版本与mybatisplus集成的jsqlparser版本冲突 --><dependency><groupId>com.github.jsqlparser</groupId><artifactId>jsqlparser</artifactId><version>${jsqlparser.version}</version></dependency>
<properties> <jsqlparser.version>4.7</jsqlparser.version><mybatis-plus.version>3.5.7</mybatis-plus.version><pagehelper.version>6.1.1</pagehelper.version></properties>