application.yml
spring:
# 数据源相关配置
datasource:
dynamic:
# 主数据源
primary: master
datasource:
# 数据源1
master:
url: jdbc:oracle:thin:@localhost:1521:ORCL
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.OracleDriver
username: ***
password: ***
# 数据源2
db2:
url: jdbc:oracle:thin:@localhost:1521:ORCL
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.OracleDriver
username: ***
password: ***
# druid 全局配置
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 30000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: true
test-on-return: false
mybatis-plus:
# mapper文件路径
mapper-locations: classpath:mapper/*/*.xml
# # SQL日志输出
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类
DruidDataSourceAutoConfigure会注入一个DataSourceWrapper,其会在原生的spring.datasource下找url,username,password。而我们动态数据源的配置路径是变化的需要先排除
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class MonitorApplication {
public static void main(String[] args) {
SpringApplication.run(MonitorApplication.class, args);
}
}
@Service
@DS("quartz")
public class TestServiceImpl {
}
默认为主数据源,加注解可以指定到第二数据源。
可以加在服务层和mapper接口上。
最好不要同时加。