多数据源配置 springboot+druid+mybatisplus使用注解整合
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sunny</groupId> <artifactId>multipledatabase</artifactId> <version>0.0.1-SNAPSHOT</version> <name>multipledatabase</name> <description>多数据源</description> <properties> <java.version>8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.20</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
application.yml 配置文件
server: port: 8080 spring: datasource: dynamic: primary: db1 # 配置默认数据库 datasource: db1: # 数据源1配置 url: jdbc:mysql://192.168.119.130:3306/sycdemo_1?serverTimezone=GMT%2B8 username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver db2: # 数据源2配置 url: jdbc:mysql://192.168.119.130:3306/sycdemo_2?serverTimezone=GMT%2B8 username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver durid: initial-size: 1 max-active: 20 min-idle: 1 max-wait: 60000 autoconfigure: exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 去除druid配置
配置包扫描以及排除DruidDataSourceAutoConfigure
@MapperScan(value = {"com.sunny.mapper"}) @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class) public class MultipledatabaseApplication { public static void main(String[] args) { SpringApplication.run(MultipledatabaseApplication.class, args); } }
使用@DS区分数据源
@DS
可以注解在 方法
上和 类
上,同时存在方法注解优先于类上注解。
注解在 service
实现或 mapper
接口方法上,不要同时在 service
和 mapper
注解
@DS("db1") public interface AccountMapper extends BaseMapper<Account> { } @DS("db2") public interface DictMapper extends BaseMapper<Dict> { }
测试
@SpringBootTest class MultipledatabaseApplicationTests { @Autowired private AccountMapper accountMapper; @Autowired private DictMapper dictMapper; @Test void db1Test() { QueryWrapper<Account> wrapper = new QueryWrapper<Account>(); wrapper.eq("uid",1596416686955790338L); Account account = accountMapper.selectOne(wrapper); System.out.println("account " + account); } @Test void db2Test() { QueryWrapper<Dict> wrapper = new QueryWrapper<Dict>(); List<Dict> dicts = dictMapper.selectList(wrapper); System.out.println("dicts " + dicts); }
https://blog.csdn.net/u012060033/article/details/123759694