pom.xml
<!--主从配置依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.1</version>
</dependency>
application-dev.yml
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
datasource:
master:
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
url: xxxx
slave_1:
username: scott
password: scott
driver-class-name: oracle.jdbc.driver.OracleDriver
url: xxxx
默认数据源:master
@DS(value = “数据源名称”)
注解在类上或方法上来切换数据源
测试代码:
@Test
public void testIworker() {
System.out.println(testMapper .selectList(null));
}
mapper类:
@Mapper
@DS("slave_1")
public interface testMapper extends BaseMapper <User > {
}