基于SpringBoot的多数据源配置

文章优先发表在个人博客:https://www.xdx97.com/article/736976551236599808

多数据源:就是可以同时操作多个数据库,之前我们的增删改查都是在一个数据库上面操作,现在可以同时操作多个数据库。

一、基于SpringBoot+MybatisPlus实现多数据源

基于MybatisPlus是最简单的一种实现方式了
为了代码的简介,直接使用controller去调用mapper

在这里插入图片描述


1-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 http://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>

1-2:application.yml

server:
  port: 8080
spring:
  datasource:
    dynamic:
      primary: db1 # 配置默认数据库
      datasource:
        db1: # 数据源1配置
          url: jdbc:mysql://xxxxx:123/test?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
          username: 123
          password: 123
          driver-class-name: com.mysql.cj.jdbc.Driver
        db2: # 数据源2配置
          url: jdbc:mysql://xxxx:123/wangshuai?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
          username: 123
          password: 123
          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配置

需要在上面的 yml 里面配置的数据库地址


1-3:TestController

import com.xdx97.test.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    
    

    @Autowired
    TestMapper testMapper;

    @GetMapping("/xdx/one")
    public String one(){
    
    
        return testMapper.getDBOne();
    }

    @GetMapping("/xdx/two")
    public String two(){
    
    
        return testMapper.getDBTwo();
    }
}

1-4:TestMapper

下面的这个 @DS注解就是配置数据源的注解,可以配置在类上面也可以配置在这个方法上面,优先使用方法上面的。

import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper

public interface TestMapper {
    
    

    @Select("SELECT name FROM xdx_test LIMIT 1")
    @DS("db1")
    String getDBOne();

    @Select("SELECT user_name FROM user LIMIT 1")
    @DS("db2")
    String getDBTwo();
}

二:其它

  • 配置多数据源,技术不同也会有所差别,像Mybatis需要配置对应的配置。
  • 当然了我们知道有一个sessionFactory,也可以在获取这个sessionfactory的时候去指定数据库配置,然后再用这个sessionfactory来获取mapper

猜你喜欢

转载自blog.csdn.net/Tomwildboar/article/details/107595393