基于springBoot项目如何配置多数据源

前言

有时,在一个项目中会用到多数据源,现在对自己在项目中多数据源的操作总结如下,有不到之处敬请批评指正!

1.pom.xml的依赖引入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mysql驱动包的引入-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
      <!-- 苞米豆的dynamic-datasource-spring-boot-starter:-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
    <!-- mybatisPlus包的导入-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
       <!-- 引入swagger-bootstrap-ui包 /doc.html-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.22</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.22</version>
        </dependency>
        <!-- 引入oracle包 /doc.html-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.1.0.7.0</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>

2.在Springboot的application.yml中进行配置:

server:
  port: 8081
  servlet:
    context-path: /

spring:
  application:
    name: spring-boot-dynamic-demo
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
      datasource:
        master:
          url: jdbc:oracle:thin:@localhost:1521/orcl
          username: scott
          password: 123456
          driver-class-name: oracle.jdbc.driver.OracleDriver
        slave_1:
          url: jdbc:mysql://localhost:3306/lzsszhyjpt?useUnicode=true&characterEncoding=utf-8&useSSL=false
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0
      logic-delete-field: deleted
  mapper-locations: classpath:/mapper/**.xml

3.多个数据源如何在业务接口中配置和使用

其中配置了两个数据源,master与sub,其中选择master作为默认数据源(对应primary配置);
若想使用sub作为部分代码的数据源,可在ServiceImpl做如下配置:
@DS("sub")
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}

4.在使用@DS注解时,有如下注意事项:

  (1)不能使用事务,否则数据源不会切换,使用的还是第一次加载的数据源;
  (2)第一次加载数据源之后,第二次、第三次…操作其它数据源,如果数据源不存在,使用的还是第一次加载的数据源;
  (3)数据源名称不要包含下划线,否则不能切换。
posted @ 2021-12-11 15:27  [奋斗]  阅读(2806)  评论(0编辑  收藏  举报