SpringBoot2.1.9 多数据源JDBC配置

一、配置文件
pom.xm


   org.springframework
   spring-jdbc
   5.1.10.RELEASE


   mysql
   mysql-connector-java
   5.1.48
   runtime

application.yml

spring:
  application:
    name: double-datasource-jdbc
  profiles:
    active: dev
 
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
 
  server:
    port: 8008
 
sys:
  jdbc:
    one:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.1.2:3307/testdb01?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: admin123
      druid:
        initial-size: 10
        max-active: 100
        min-idle: 10
        max-wait: 60000
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: SELECT 1 FROM DUAL
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        filter:
          stat:
            log-slow-sql: true
            slow-sql-millis: 1000
            merge-sql: true
            enabled: true
          wall:
            config:
              multi-statement-allow: true
        stat-view-servlet:
          enabled: false
    two:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.1.2:3307/testdb02?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: admin123
      druid:
        initial-size: 10
        max-active: 100
        min-idle: 10
        max-wait: 60000
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: SELECT 1 FROM DUAL
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        filter:
          stat:
            log-slow-sql: true
            slow-sql-millis: 1000
            merge-sql: true
            enabled: true
          wall:
            config:
              multi-statement-allow: true
        stat-view-servlet:
          enabled: false

二、配置Configuration Bean

(1)第一个jdbc配置(主)

public class OneJdbcConfig {

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "sys.jdbc.one")
    public DataSourceProperties oneDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Primary
    @Bean
    public JdbcTemplate oneJdbcTemplate(@Autowired @Qualifier("oneDataSourceProperties") DataSourceProperties dataSourceProperties){
        DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();
        return new JdbcTemplate(dataSource);
    }
}

(2)第二个jdbc配置

public class TwoJdbcConfig {

    @Bean
    @ConfigurationProperties(prefix = "sys.jdbc.two")
    public DataSourceProperties twoDataSourceProperties(){
        return new DataSourceProperties();
    }

    @Bean
    public JdbcTemplate twoJdbcTemplate(@Autowired @Qualifier("twoDataSourceProperties") DataSourceProperties dataSourceProperties){
        DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().build();
        return new JdbcTemplate(dataSource);
    }
}

(3)导入配置

@Configuration
@Import({OneJdbcConfig.class, TwoJdbcConfig.class})
public class JdbcConfig {
 
}

注意:去掉默认自动配置

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

 

你可能感兴趣的:(Spring,Boot)