chenzhao

  • java
  • iOS
  • IT
知识积累
不积跬步无以至千里
  1. 首页
  2. java
  3. 正文

Springboot集成MybatisPlus使用Pagehelper分页问题

2022年 11月 25日 163点热度 0人点赞 0条评论

注: MybatisPlusConfig SpringBootApplication 中的排除也可以在pom.xml 中排除分页模块

<exclusions>
    <exclusion>
       <groupId>com.github.jsqlparser</groupId>
       <artifactId>jsqlparser</artifactId>
       </exclusion>
</exclusions>

然后 MybatisPlusConfig 中只注入

    /**
     * page helper分页插件
     *
     * @return
     */
    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

为什么使用了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();
    }
} 

本文转自 https://blog.csdn.net/Let_me_tell_you/article/details/125199048,如有侵权,请联系删除。

标签: 暂无
最后更新:2022年 11月 25日

陈昭

IT 程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2022 chenzhao. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang