1. 程式人生 > 其它 >springboot+druid+mybatisplus使用註解整合配置多資料來源

springboot+druid+mybatisplus使用註解整合配置多資料來源

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>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <
groupId>com.example</groupId> <artifactId>mutipledatasource2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mutipledatasource2</name> <description>Demo project for Spring Boot</description> <properties> <java.version
>1.8</java.version> </properties> <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> </plugin> </plugins> </build> <profiles> <profile> <id>local1</id> <properties> <profileActive>local1</profileActive> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>local2</id> <properties> <profileActive>local2</profileActive> </properties> </profile> </profiles> </project>

application.yml 配置檔案:

server:
  port: 8080
spring:
  datasource:
    dynamic:
      primary: db1 # 配置預設資料庫
      datasource:
        db1: # 資料來源1配置
          url: jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
        db2: # 資料來源2配置
          url: jdbc:mysql://localhost:3306/db2?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
          username: root
          password: root
          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會注入一個DataSourceWrapper,其會在原生的spring.datasource下找 url, username, password 等。動態資料來源 URL 等配置是在 dynamic 下,因此需要排除,否則會報錯。排除方式有兩種,一種是上述配置檔案排除,還有一種可以在專案啟動類排除:

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

2.3 給使用非預設資料來源添加註解@DS

@DS可以註解在方法上和類上,同時存在方法註解優先於類上註解。
註解在 service 實現或 mapper 介面方法上,不要同時在 service 和 mapper 註解。

Mapper介面類註解:

@DS("db2") 
@Mapper
public interface UserMapper extends BaseMapper<User> { }

Service實現類註解:

@Service 
@DS("db2") 
public class ModelServiceImpl extends ServiceImpl<ModelMapper, Model> implements IModelService {}

方法類:

@Select("SELECT * FROM user") 
@DS("db2") 
List<User> selectAll();