一、配置文件
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 {
}
来源:CSDN
作者:茅坤宝骏氹
链接:https://blog.csdn.net/moakun/article/details/104069652