mybatisplus 多数据源,多个sqlSessionFactory 导致 savebatch 执行不正确

文章讲述了在项目中,原本计划将数据保存到数据库A,但实际存储在了sharding配置的分表数据源B。原因在于mybatis的SqlSessionFactory配置错误。通过将mybatis配置改为mybatis-plus,正确指定mapper资源,解决了这个问题。
摘要由CSDN通过智能技术生成

        项目中有2个数据源,1个是单独的数据库A,1个是sharding配置的分表数据源B;

        本来是想保存数据到A数据库,结果是执行的sharding的分表数据库B,其实是因为 SqlSessionFactory 错误导致的。

之前的配置文件信息,这是mybatis的配置信息

    @Bean(name = "dictSqlSessionFactory")
    public SqlSessionFactory rdsSqlSessionFactory() throws Exception {
       SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(rdsDataSource());
        sqlSessionFactoryBean.setMapperLocations(new     
        PathMatchingResourcePatternResolver().getResources(mybatisMapperLocations));
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        GlobalConfig globalConfig=new GlobalConfig();
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        mybatisConfiguration.setCacheEnabled(false);
        mybatisConfiguration.setCallSettersOnNulls(true);
        mybatisConfiguration.setJdbcTypeForNull( JdbcType.NULL);
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
        return sqlSessionFactoryBean.getObject();
	 }

修改成mybatis-plus的配置信息

    @Bean(name = "dictSqlSessionFactory")
    public SqlSessionFactory rdsSqlSessionFactory() throws Exception {
         MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(rdsDataSource());
        sqlSessionFactoryBean.setMapperLocations(new 
        PathMatchingResourcePatternResolver().getResources("classpath:mapper/dict/*.xml"));
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        GlobalConfig globalConfig=new GlobalConfig();
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);
        mybatisConfiguration.setCacheEnabled(false);
        mybatisConfiguration.setCallSettersOnNulls(true);
        mybatisConfiguration.setJdbcTypeForNull(JdbcType.NULL);
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);
        return sqlSessionFactoryBean.getObject();
	}

 修改成这个即可成功

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值