1、引入jar包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
2、yml配置动态数据源
spring:
datasource:
dynamic:
datasource:
master:
url: jdbc:mysql://172.50.80.180:3306/fj_dev_to_pre?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: eapdm
password: eapdm@2021Y
driver-class-name: com.mysql.cj.jdbc.Driver
tradectr:
url: jdbc:mysql://172.50.80.180:3306/fj_dev_to_pre?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: eapdm
password: eapdm@2021Y
driver-class-name: com.mysql.cj.jdbc.Driver
其中配置了两个数据源,master
与tradectr
,其中master
作为默认数据源
在Service中用@DS注解,使用数据源
@Service
@Transactional(rollbackFor = Exception.class)
@DS("tradectr")
@Async("tradeExecutor")
public class CustDlvTradeService extends CustDlvService {
}