SqlSessionFactory多数据源配置案例(springBoot)

1、项目目录图示:

SqlSessionFactory多数据源配置案例(springBoot)_第1张图片

2、案例依赖:


    spring-boot-parent
    org.springframework.boot
    2.2.12.RELEASE



    
        org.springframework.boot
        spring-boot-starter
    

    
        org.springframework.boot
        spring-boot-starter-web
    

    
        com.baomidou
        mybatis-plus-boot-starter
        3.5.2
    


    
        mysql
        mysql-connector-java
    

    
        org.projectlombok
        lombok
    

    
        com.alibaba
        druid-spring-boot-starter
        1.2.14
    



    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

3、案例配置文件yml:

server:
  port: 8099

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    db1:
      jdbc-url: jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=true
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver

    db2:
      jdbc-url: jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=true
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver

    druid: # druid数据库连接池的基本初始化属性
      initial-size: 5 # 连接池初始化的大小
      min-idle: 1 # 最小空闲的线程数
      max-active: 20 # 最大活动的线程数


mybatis-plus:
  mapper-locations: classpath:/mapper/*/*.xml # 配置MyBatis-Plus扫描Mapper文件的位置
  type-aliases-package: com.hua.bean # 创建别名的类所在的包
  configuration:
    map-underscore-to-camel-case: true # 开启驼峰命名

#sql语句打印
logging:
  level:
    #路径
    com.hua: debug

4、重点 ,SqlSessionFactory的配置如下:(MySQLDataSourceConfig1类

@Configuration
@MapperScan(basePackages = "com.hua.mapper.db1", sqlSessionFactoryRef = "MySQLSqlSessionFactory1")
public class MySQLDataSourceConfig1 {

    @Bean(name = "MySQLDataSource1")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "MySQLSqlSessionFactory1")
    @Primary
    public SqlSessionFactory test1SqlSessionFactory(
            @Qualifier("MySQLDataSource1") DataSource datasource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean ();
        bean.setDataSource(datasource);
        bean.setMapperLocations(// 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
        return bean.getObject();
    }


    @Bean("MySQLSqlSessionTemplate1")
    @Primary
    public SqlSessionTemplate test1SqlSessionTemplate(
            @Qualifier("MySQLSqlSessionFactory1") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

    @Bean
    public PlatformTransactionManager transactionManager1(@Qualifier("MySQLDataSource1")DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}

4.5、第二个(多个数据源则多个这种配置类) ,SqlSessionFactory的配置如下:(MySQLDataSourceConfig2类 

@Configuration
@MapperScan(basePackages = "com.hua.mapper.db2", sqlSessionFactoryRef = "MySQLSqlSessionFactory2")
public class MySQLDataSourceConfig2 {

    @Bean(name = "MySQLDataSource2")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "MySQLSqlSessionFactory2")
    public SqlSessionFactory test2SqlSessionFactory(
            @Qualifier("MySQLDataSource2") DataSource datasource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean ();
        bean.setDataSource(datasource);
        bean.setMapperLocations(// 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
        return bean.getObject();
    }


    @Bean("MySQLSqlSessionTemplate2")
    public SqlSessionTemplate test2SqlSessionTemplate(
            @Qualifier("MySQLSqlSessionFactory2") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

//    此处根据数据库的类型来配置,在MySQLDataSourceConfig1类中已经配置了mysql的事务类型,如此处数据库类型也是mysql则不用配置,否则将注释放开正常配置即可
//    @Bean
//    public PlatformTransactionManager transactionManager2(@Qualifier("MySQLDataSource2")DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }

}

 相同的数据库类型PlatformTransactionManager事务管理器只需配置一次加入ioc即可不用重复配置,不同数据库类型则需要再次配置bean加入容器(放开上面代码注释即可)。

补充:启动类上加入注解取消datasource的自动配置

SqlSessionFactory多数据源配置案例(springBoot)_第2张图片

 

5、测试代码:

SqlSessionFactory多数据源配置案例(springBoot)_第3张图片

 SqlSessionFactory多数据源配置案例(springBoot)_第4张图片

 注解方式Spring Boot配置多数据源操作参考:

注解方式Spring Boot配置多数据源操作_O_G的博客-CSDN博客

你可能感兴趣的:(java,mybatis,开发语言)