springboot-多数据源
1.yml配置
spring:
datasource:
dynamic:
primary: master #默认数据库配置
strict: false #设置严格模式
datasource:
master:
#主数据库:存放数据索引和配置数据
type: org.apache.commons.dbcp2.BasicDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:3306/lcm
username: 'lcm'
password: 'lcm'
dbcp2:
validation-query: select 1 from dual
default-auto-commit: false
initial-size: 1
max-total: 70 # 生产环境建议最大连接数为50~150之间
max-idle: 20 # 生产环境建议配置最大空闲连接数5
min-idle: 10 # 生产环境建议配置最小空闲连接数5
max-wait-millis: 10000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true #Statement缓存
max-open-prepared-statements: 300
db2:
#主数据库:存放数据索引和配置数据
type: org.apache.commons.dbcp2.BasicDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:3306/lcm
username: 'lcm2'
password: 'lcm2'
dbcp2:
validation-query: select 1 from dual
default-auto-commit: false
initial-size: 1
max-total: 70 # 生产环境建议最大连接数为50~150之间
max-idle: 20 # 生产环境建议配置最大空闲连接数5
min-idle: 10 # 生产环境建议配置最小空闲连接数5
max-wait-millis: 10000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true #Statement缓存
max-open-prepared-statements: 300
2.@DS注解使用
@Mapper
@DS("db2")
public class TestDataSource{
@Select("select * from test")
void test();
}
@Mapper
public class TestDataSource{
@DS("db2")
@Select("select * from test")
void test();
@Select("select * from test")
void test();
}