1.引入对应的maven依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
2.配置application.yml文件
spring:
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.100:3306/shop?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 111111
type: com.alibaba.druid.pool.DruidDataSource
db_1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.101:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 111111
type: com.alibaba.druid.pool.DruidDataSource
3.指定数据源进行数据库DML或DQL操作
@DS("db_1")
public interface RateMapper extends BaseMapper<Rate> {}
@GetMapping("/save")
@DSTransactional
public void save(){
数据源master的insert操作
数据源db_1的insert操作
}
|