SpringBoot2.1.9 多数据源JDBC配置

送分小仙女□ 提交于 2020-01-22 16:52:04

一、配置文件
pom.xm

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>5.1.10.RELEASE</version>
</dependency>
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.48</version>
   <scope>runtime</scope>
</dependency>

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 {
 
}

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!