springboot+druid+mybatisplus使用注解整合配置多数据源

dynamic-datasource完美解决这件事,只需要添加注解即可。

步骤总结:

1.加入依赖

2.yml配置数据源,配置默认数据库

3.启动类添加(exclude = DruidDataSourceAutoConfigure.class)

4.Mapper接口添加@DS注解

红色标记部分为重点! 

 主要依赖包

  • spring-boot-starter-web
  • mybatis-plus-boot-starter
  • dynamic-datasource-spring-boot-starter # 配置动态数据源
  • druid-spring-boot-starter # 阿里的数据库连接池
  • mysql-connector-java
  • lombok

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配置mysql
          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配置mysql
          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
        db3: # 数据源3配置oracle
          type: com.alibaba.druid.pool.DruidDataSource
          platform: oracle
          driver-class-name: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@127.0.0.1:8081:orcl
          username: business
          password: business
          initialSize: 5
          minIdle: 5
          maxActive: 20
          maxWait: 60000
          timeBetweenEvictionRunsMillis: 60000
          minEvictableIdleTimeMillis: 300000
          validationQuery: SELECT 1 FROM DUAL
          testWhileIdle: true
          testOnBorrow: false
          testOnReturn: false
          filters: stat,wall,log4j
          logSlowSql: true
      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);
  }
}

给使用非默认数据源添加注解@DS

@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。
注解在 service 实现或 mapper 接口方法上,不要同时在 service 和 mapper 注解。

Mapper接口类注解:

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

Service实现类注解:

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

Mapper接口方法中:

不在类上注解,在方法中注解,可实现一个类调用不同的数据库。

@Mapper
public interface UserMapper extends BaseMapper<User> {

// 调用默认数据库 在yml配置好默认库后 @DS("db1") 可写可不写 
List<Map<String,Object>> getData();

@Select("SELECT * FROM user") 
// 调用配置的第二个数据库
@DS("db2") 
List<User> selectAll();

}

原文地址:https://www.cnblogs.com/wanlige/p/15018898.html