Springboot集成MybatisPlus使用Pagehelper分页问题

当项目中同时引入了Mybatis-Plus和PageHelper,由于两者都包含分页插件,会导致运行时出现错误。为保持业务代码兼容性,选择继续使用PageHelper进行分页。解决方案是在启动类中排除PageHelperAutoConfiguration,确保只启用PageHelper分页。配置代码展示了如何创建和配置PageHelper及Mybatis-Plus的分页插件。
摘要由CSDN通过智能技术生成

为什么使用了mybatis-plus不直接使用mybatis-plus自带的分页插件呢?因为原有的业务代码都是使用pagehelper来做分页的,且pagehelper封装的数据格式已深度嵌入到业务代码之中,所以改起来很麻烦,只能继续使用pagehelper,保证原有代码的兼容性。

但是在集成的过程中报了如下错误,也就是存在多个分页插件问题,配置了pagehelper,但是mybatis-plus自带的分页插件也生效了,报错信息如下。

image-20220609103132482

解决思路很简单,就是只启用pagehelper来做分页就好了。

解决办法:

在启动类中的 @SpringBootApplication 注解上排除 PageHelperAutoConfiguration 类。

@SpringBootApplication(exclude = PageHelperAutoConfiguration.class)

附上配置类代码,注意这里只贴了分页相关的配置,其他事物,包扫描相关的配置,请自行添加。

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        return interceptor;
    }

    /**
     * 分页插件,自动识别数据库类型
     */
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        // 分页合理化
        paginationInnerInterceptor.setOverflow(true);
        //设置分页插件属性
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("params", "count=countSql");
        paginationInnerInterceptor.setProperties(properties);
        return paginationInnerInterceptor;
    }

    /**
     * page helper分页插件
     *
     * @return
     */
    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值