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

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐