重点概念
多数据源在配置之后是根据service的实现类上面的注解来确定生效范围的,一个实现类可以根据注解代表一个数据库
ServiceImpl
import com.baomidou.dynamic.datasource.annotation.DS;
//决定用哪个数据源
@DS("slave")
@Service
public class OracleFileBrowsingServiceImpl extends ServiceImpl<FileBrowsingMapper, FileBrowsing> implements FileBrowsingService {
@Override
public Map<String, Object> queryList(String name, String resultType, String startTimeStr, String endTimeStr, Integer page, Integer size) {
return null;
}
}
使用部分
@Primary //代表本实现类中或者本实现类被ioc获取后,使用的是默认的数据源
@Service
public class FileBrowsingServiceImpl extends ServiceImpl<FileBrowsingMapper, FileBrowsing> implements FileBrowsingService {
@Resource
@Qualifier(value = "oracleFileBrowsingServiceImpl")//根据实现类的首字母小写类名注入
private FileBrowsingService oracleFileBrowsingService;
public Map<String, Object> queryList(String name, String approvalType, String startTimeStr, String endTimeStr, Integer page, Integer size) {
//此处使用oracle的实现类,所保存的数据源为oracle实现类中注解的数据源
oracleFileBrowsingService.saveOrUpdateBatch(fileBrowsingList);
return resultMap;
}
}
配置
application.properties
# ======================= DataSource start==========================
spring.datasource.dynamic.primary=master
# 设置第一个数据库
#spring.datasource.dynamic.datasource.master.driver-class-name=org.postgresql.Driver
#spring.datasource.dynamic.datasource.master.url=jdbc:postgresql://192.9.30.225:5432/diitmap_yt
#spring.datasource.dynamic.datasource.master.username=postgres
#spring.datasource.dynamic.datasource.master.password=postgres
spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/test
#spring.datasource.dynamic.datasource.master.url=jdbc:mysql://10.224.130.130:3306/gjxq_prod
spring.datasource.dynamic.datasource.master.username=admin
spring.datasource.dynamic.datasource.master.password=123456
spring.datasource.dynamic.datasource.master.initial-size=1
spring.datasource.dynamic.datasource.master.max-active=10
spring.datasource.dynamic.datasource.master.min-idle=1
spring.datasource.dynamic.datasource.master.max-wait=60000
spring.datasource.dynamic.datasource.master.max-pool-prepared-statement-per-connection-size=5
spring.datasource.dynamic.datasource.master.time-between-eviction-runs-millis=60000
spring.datasource.dynamic.datasource.master.min-evictable-idle-time-millis=300000
spring.datasource.dynamic.datasource.master.stat-view-servlet.url-pattern=/druid/*
spring.datasource.dynamic.datasource.master.filter.stat.slow-sql-millis=1000
#从连接池中借用连接时是否要进行测试
spring.datasource.dynamic.datasource.master.test-on-borrow=true
#执行连接验证的间隔时间,单位为毫秒
spring.datasource.dynamic.datasource.master.validation-interval=10000
#测试链接的sql语句
spring.datasource.dynamic.datasource.master.validation-query=SELECT 1
spring.datasource.dynamic.datasource.master.connection-test-query=SELECT 1
#在连接空闲时是否要进行测试
spring.datasource.dynamic.datasource.master.test-while-idle=true
#在使用 Tomcat 连接池时是否要记录验证错误
spring.datasource.dynamic.datasource.master.log-validation-errors=true
# 设置第二个数据库
spring.datasource.dynamic.datasource.slave.username=FILE_BROWSE
spring.datasource.dynamic.datasource.slave.password=FILE_BROWSE
spring.datasource.dynamic.datasource.slave.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.dynamic.datasource.slave.url=jdbc:oracle:thin:@192.9.30.229:1521/orcl
spring.datasource.dynamic.datasource.slave.initial-size=1
spring.datasource.dynamic.datasource.slave.max-active=10
spring.datasource.dynamic.datasource.slave.min-idle=1
spring.datasource.dynamic.datasource.slave.max-wait=60000
spring.datasource.dynamic.datasource.slave.max-pool-prepared-statement-per-connection-size=5
spring.datasource.dynamic.datasource.slave.time-between-eviction-runs-millis=60000
spring.datasource.dynamic.datasource.slave.min-evictable-idle-time-millis=300000
spring.datasource.dynamic.datasource.slave.stat-view-servlet.url-pattern=/druid/*
spring.datasource.dynamic.datasource.slave.filter.stat.slow-sql-millis=1000
#从连接池中借用连接时是否要进行测试
spring.datasource.dynamic.datasource.slave.test-on-borrow=true
#执行连接验证的间隔时间,单位为毫秒
spring.datasource.dynamic.datasource.slave.validation-interval=10000
#测试链接的sql语句
spring.datasource.dynamic.datasource.slave.validation-query=SELECT 1
spring.datasource.dynamic.datasource.slave.connection-test-query=SELECT 1
#在连接空闲时是否要进行测试
spring.datasource.dynamic.datasource.slave.test-while-idle=true
#在使用 Tomcat 连接池时是否要记录验证错误
spring.datasource.dynamic.datasource.slave.log-validation-errors=true
# ======================= DataSource end==========================
标签:slave,SpringBoot,spring,dynamic,master,datasource,数据源
From: https://www.cnblogs.com/ideaAI/p/17141858.html