动态数据源自定义SqlSessionFactoryBean时mybatis plus配置失效

环境:

  • 动态数据源
  • spring-boot 2.7.15
  • mybatis-plus 3.5.2

yaml配置:

spring:
  datasource:
    db100:
      username: xxx
      password: xxx
      jdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/100
      driver-class-name: com.kingbase8.Driver
      # url: jdbc:postgresql://xxx.xxx.xxx.xxx:54321/100?serverTimezone=Asia/Shanghai&useSSL=false
      # driverClassName: org.postgresql.Driver
    db101:
      username: xxx
      password: xxx
      jdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/101
      driver-class-name: com.kingbase8.Driver
    db104:
      username: xxx
      password: xxx
      jdbc-url: jdbc:kingbase8://xxx.xxx.xxx.xxx:54321/104
      driver-class-name: com.kingbase8.Driver

mybatis-plus:
  mapper-locations: classpath*:**mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

实际运行的时候,发现mybatis-plus相关的配置都失效了。

这是因为我们自定义SqlSessionFactoryBean

    @Bean
    public MybatisSqlSessionFactoryBean dynamicDataSourceSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dynamicDataSource);
        return sqlSessionFactoryBean;
    }

解决方式:

    @Bean
    @Scope("prototype")
    @ConfigurationProperties(prefix = "mybatis-plus.global-config")
    public GlobalConfig globalConfig(){
        return new GlobalConfig();
    }



    @Bean
    //@Scope("prototype")
    @ConfigurationProperties(prefix = "mybatis-plus.configuration")
    public MybatisConfiguration mybatisConfiguration(){
        return new MybatisConfiguration();
    }

SqlSessionFactoryBean修改:

@MapperScan(basePackages = "com.etoak.wsdhla.mapper", sqlSessionFactoryRef = "dynamicDataSourceSqlSessionFactory")
@Configuration
public class DataSourceConfig {
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.db100")
    public DataSource dataSource100() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.db101")
    public DataSource dataSource101() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.db104")
    public DataSource dataSource104() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Scope("prototype")
    @ConfigurationProperties(prefix = "mybatis-plus.global-config")
    public GlobalConfig globalConfig(){
        return new GlobalConfig();
    }

    @Bean
    //@Scope("prototype")
    @ConfigurationProperties(prefix = "mybatis-plus.configuration")
    public MybatisConfiguration mybatisConfiguration(){
        return new MybatisConfiguration();
    }

    /**
     * 将动态代理数据源对象放入Spring容器中
     */
    @Bean
    public DynamicDataSource dynamicDataSource(@Qualifier("dataSource100") DataSource dataSource100, @Qualifier("dataSource101") DataSource dataSource101, @Qualifier("dataSource104") DataSource dataSource104) {
        // 这个地方是比较核心的targetDataSource 集合是我们数据库和名字之间的映射
        Map<Object, Object> targetDataSource = new HashMap<>();
        targetDataSource.put("db100", dataSource100);
        targetDataSource.put("db101", dataSource101);
        targetDataSource.put("db104", dataSource104);

        DynamicDataSource dataSource = new DynamicDataSource();
        // 设置所有的数据源
        dataSource.setTargetDataSources(targetDataSource);
        // 设置默认使用的数据源对象
        dataSource.setDefaultTargetDataSource(dataSource100);

        return dataSource;
    }

    @Bean
    public MybatisSqlSessionFactoryBean dynamicDataSourceSqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dynamicDataSource);
        // 设置数据库mapper的xml文件路径
        sqlSessionFactoryBean .setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration());
        sqlSessionFactoryBean.setGlobalConfig(globalConfig());
        return sqlSessionFactoryBean;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsdhla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值