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

环境:

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

yaml配置:

XML 复制代码
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

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

解决方式:

java 复制代码
    @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修改:

java 复制代码
@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;
    }
}
相关推荐
敲个大西瓜12 天前
mybatis拦截器插件实现数据库字段加解密
mybatis
武子康12 天前
Java-28 深入浅出 Spring 实现简易Ioc-04 在上节的业务下手动实现AOP
java·后端·mybatis
一条泥憨鱼12 天前
苍穹外卖【day6|微信登录与商品浏览功能】
后端·mybatis·苍穹外卖
vx-Biye_Design12 天前
springboot安阳地区研学旅游服务小程序-计算机毕业设计源码12785
java·vue.js·windows·spring boot·tomcat·maven·mybatis
摇滚侠12 天前
MyBatis+Spring+SpringMVC SSM 整合 179-185
java·spring·mybatis
摇滚侠12 天前
MyBatis+Spring+SpringMVC SSM ContextLoaderListener 177-178
java·spring·mybatis
Spring小子13 天前
【Spring Boot + Vue + DeepSeek】从零打造一个AI驱动的智能健康分析系统
java·spring boot·mybatis
武子康13 天前
Java-27 深入浅出 Spring - 实现简易Ioc-03 在上节的业务下手动实现IoC 从 XML 配置到 BeanFactory 反射注入
java·后端·mybatis
柏舟飞流13 天前
Spring Boot 进阶实战:整合 MyBatis、Redis、JWT,搭一个更像真实项目的后端服务
spring boot·redis·mybatis