Error querying database. Cause: java.lang.RuntimeException: 在系统中发现了多个分页插件,请检查系统配置! 问题处理

看到这个提示bug,大概就明白是分页插件进行了2次注入原因导致的。所以分析项目内为什么会出现2次分页相关的对象进行2次注入导致的。全局搜索相关注入配置发现了下面的代码:

    @PostConstruct
    public void addPageInterceptor() {
        ...... 
        PageInterceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.putAll(this.pageHelperProperties());
        interceptor.setProperties(properties);
        ......
    }

这段代码你就会发现进行注入了一次PageInterceptor对象,但是了解大概分页注入方式的同学都知道,mybatis分页组件依赖jar包内已经进行注入过PageInterceptor对象了,源代码如下:

public class PageHelperAutoConfiguration implements EnvironmentAware {
    @Autowired
    private List sqlSessionFactoryList;
    @Autowired
    private PageHelperProperties pageHelperProperties;
    private RelaxedPropertyResolver resolver;

    public PageHelperAutoConfiguration() {
    }

    public void setEnvironment(Environment environment) {
        this.resolver = new RelaxedPropertyResolver(environment, "pagehelper.");
    }

    @PostConstruct
    public void addPageInterceptor() {
        PageInterceptor interceptor = new PageInterceptor();
        Properties properties = this.pageHelperProperties.getProperties();
        Map subProperties = this.resolver.getSubProperties("");
        Iterator var4 = subProperties.keySet().iterator();
    ....
   }

所以去掉相关的注入即可解决报错现象。但是可能碰到相关问题的同学并不全是因为简单的PageInterceptor注入2次导致的bug出现。小编此次碰到的是同组开发人员是想要添加自定义的mybatis的拦截器:

for (Object sqlSessionObject : this.sqlSessionFactoryList) {
            SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) sqlSessionObject;
//         permissionInterceptor自定义的拦截器
sqlSessionFactory.getConfiguration().addInterceptor(permissionInterceptor);
 }

所以很简单的修改方式,就是不需要再次注入PageInterceptor,tomcat启动,spring已经帮助我们进行自动注入相关分页组件,我们只需要通过自动装配注解,装配好SqlsessionFactory,然后注入到相应的Interceptor即可:

@Resource
    private List sqlSessionFactoryList;

    @PostConstruct
    public void addPageInterceptor() {
        //数据权限拦截器
        PermissionInterceptor PermissionInterceptor = new PermissionInterceptor();
        for (Object sqlSessionObject : this.sqlSessionFactoryList) {
            SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) sqlSessionObject;
            sqlSessionFactory.getConfiguration().addInterceptor(permissionInterceptor);
        }
    }

欢迎各位评论,一起进步。

你可能感兴趣的:(springboot,mybatis,pagerHelper)