Spring/SpringBoot使用多数据源时,导致Mybatis插件PagerHelper失效问题解决方案

   笔者在使用多数据源后,出现MyBatis插件PageHelper完全失效的问题,导致每次查询都不进行翻页,查询处所有的数据。这是因为配置多数据源时,必然要重写SqlSessionFactory(MyBatis的接口),这就导致默认的插件加载失效,此时就需要重新绑定PageHelper插件了。

1. 配置插件

/**
     * 配置插件
     * 
     * @return bean
     */
    @Bean(name = "plugins")
    public Interceptor[] plugins() {

		//org.apache.ibatis.plugin.Interceptor
        Interceptor interceptor = new PageInterceptor();

		//java.util.Properties
        Properties properties = new Properties();

        //是否将参数offset作为pageNum
        properties.setProperty("offsetAsPageNum", "true");

        // 数据库类型
        properties.setProperty("helperDialect", "mysql");

        // 是否返回行数,相当于MySQL的count(*)
        properties.setProperty("rowBoundsWithCount", "true");

        // 是否分页合理化:即翻页小于0时,显示第一页数据,翻页数较大查不到数据时,显示最后一页的数据,默认即为false,本处显示写出,以供参考。
        properties.setProperty("reasonable", "false");

        interceptor.setProperties(properties);

        Interceptor[] plugins = new Interceptor[1];

        plugins[0] = interceptor;

        return plugins;
    }

2.装载插件

    /**
     * sql工厂(通过名称来注入)
     * 
     * @param defaultSource
     *            默认数据源
     * @param otherSource
     *            其他的数据源
     * @return bean
     * @throws Exception
     *             异常
     */
    @Bean(name = "sessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("defaultSource") DataSource defaultSource, @Qualifier("otherSource") DataSource otherSource)
            throws Exception {

        //org.mybatis.spring.SqlSessionFactoryBean
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        sqlSessionFactoryBean.setDataSource(dynamicDataSource(defaultSource, otherSource));

        // 配置xml文件的路径
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*/*.xml"));
       
        //装载/设置插件,本步骤必须在sqlSessionFactoryBean.getObject()之前执行
        sqlSessionFactoryBean.setPlugins(plugins());
        
        //返回工厂
        org.apache.ibatis.session.Configuration configuration = sqlSessionFactoryBean.getObject().getConfiguration();

        //通过set方法配置相关的属性,还可以自己继续补加
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCallSettersOnNulls(true);
       
        return sqlSessionFactoryBean.getObject();
    }

3. 主要事项

  • sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:mapper//.xml”)); 负责加载xml路径下的sql文件,既然自定义了数据源,properties文件中的xml文件同样也不起作用。
  • sqlSessionFactoryBean.setPlugins(plugins()); 是设置插件的步骤,一定要写在sqlSessionFactoryBean.getObject()方法执行之前,不然不起作用,原因很简单,就好比我们的set和get方法,先get后在去set和先set后get得到的值是不一样的。
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值