项目使用mybatis-plus分页插件和pageHelper分页插件引起失效问题!

一、背景

项目中以前的源码是使用pageHelper分页插件来实现。涉及到的sql代码还是要手写部分。而目前在Springboot项目中,使用的主流的方式就是一个基于mybatis-plus的.page()的分页,当然这种分页方式是适用于一些简单的查询和简单场景下。对于多表联查等场景,可能还是需要通过手写sql来实现复杂查询。这时候就可以使用pageHelper的 分页插件。

二、 问题描述

在同时使用mybatis-plus和pageHelper时,导致了pageHelper分页失效。
首先我的项目中引入的相关依赖如下:

pageHelper 版本是5.3.2

mybatis-plus的版本是3.4.3

使用maven Helper工具可以查看到,这两个工具的版本上其实会存在sqlparser依赖版本冲突问题。pageHelper引用的是sqlparser4.5版本,mybatis-plus引用的是sqlparser4.0版本,但是也不是导致分页失效的主要原因。

三、解决方案

原来只是配置MyBatisPlus 的MybatisPlusInterceptor 。

@Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }

需要增加com.github.pagehelper.PageInterceptor;的配置,直接给完整的配置类代码。


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Properties;

/**
 *
 */
/***
 * @Author: javinlu
 * @Description: MyBatis plus 相关配置
 * @Date: 2024/11/18 11:35
 **/
@Configuration
@EnableTransactionManagement
@MapperScan({"com.macro.mall.mapper", "com.macro.mall.dao"})
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }


    @Bean
    public PaginationInnerInterceptor paginationInterceptor() {
        return new PaginationInnerInterceptor();
    }


    @Bean
    PageInterceptor pageInterceptor() {
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        pageInterceptor.setProperties(properties);
        return pageInterceptor;
    }

}

那么加上这一配置类之后,mybatis-plus的分页和pageHelper的分页就都会会生效了。

四、注意事项

由于两者都需要用到page,需要在使用的时候区分使用的page是哪个下面的。别import错了也会导致失效。

例如:使用mybatis-plus的分页时,确保page是com.baomidou.mybatisplus.extension.plugins.pagination下的,

在使用pageHelper的时候确保page是在 com.github.pagehelper.PageInterceptor。

在同一个项目中同时使用 MyBatis-Plus PageHelper分页功能,需要解决两者分页拦截器的冲突问题。由于两者都通过拦截器实现分页逻辑,如果配置不当,可能会导致分页查询失效或返回结果异常(如 `total` 值为 0)。 ### 配置依赖版本 首先,确保使用兼容性较好的版本。PageHelper 的版本建议不低于 1.4.2,而 MyBatis-Plus 的版本建议不低于 3.5.0。以下是 `pom.xml` 中的依赖配置示例: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.9</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.2</version> </dependency> ``` ### 配置拦截器顺序 为了避免分页拦截器之间的冲突,可以通过自定义 `MyBatis` 配置类,调整拦截器的执行顺序,确保 `MyBatis-Plus` 的分页拦截器优先于 `PageHelper` 执行: ```java @Configuration public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } @Bean public PageInterceptor pageInterceptor() { return new PageInterceptor(); } } ``` 上述配置中,`MybatisPlusInterceptor` 是 MyBatis-Plus 的核心分页拦截器,`PageInterceptor` 是 PageHelper分页拦截器。通过 `MyBatis` 配置类,可以明确指定拦截器的执行顺序。 ### 禁用 PageHelper 的自动分页逻辑 如果项目中主要使用 MyBatis-Plus 进行分页查询,可以通过配置禁用 PageHelper 的自动分页逻辑,避免其干扰: ```yaml pagehelper: auto-dialect: false auto-runtime-dialect: false ``` ### 分页使用方式 #### 使用 MyBatis-Plus 分页 MyBatis-Plus分页功能通过传入 `Page` 对象实现,插件会自动拦截并处理分页逻辑: ```java Page<User> page = new Page<>(1, 10); IPage<User> userPage = userMapper.selectPage(page, null); ``` #### 使用 PageHelper 分页 PageHelper分页功能需要在查询前显式调用 `PageHelper.startPage()` 方法: ```java PageHelper.startPage(1, 10); List<User> users = userMapper.selectAll(); PageInfo<User> pageInfo = new PageInfo<>(users); ``` ### 兼容性注意事项 - **SQL 改写**:PageHelper 通过拦截器动态修改 SQL,加入 `LIMIT` 实现分页,而 MyBatis-Plus 也是通过类似机制实现分页逻辑。两者共存时,需确保 SQL 被正确改写。 - **COUNT 查询**:两者都会自动执行 `COUNT(*)` 查询来获取总记录数,但 PageHelper 可以通过配置关闭此功能,以避免重复执行。 - **多数据源支持**:PageHelper 对多数据源的支持较为成熟,而 MyBatis-Plus 的多数据源配置需要额外处理。 通过上述配置使用方式,可以在同一个项目中同时使用 MyBatis-Plus PageHelper分页功能,并有效避免两者之间的冲突[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JavinLu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值