Use mabatis_plus expansion package multi-data source configuration in Springboot

1. Demand

When using Springboot for project development, you need to access multiple databases, each of which has its own role, and you need to separate the data into tables for storage.

1) Multiple data source configuration: the two databases are irrelevant to each other, method a uses the data of database a, and method b uses the data of database b;

2) Dynamic data source configuration: The two database services are related, such as read-write separation library.

2. Solve

There are two ways: 1) Use @MapperScan(basePackages = {"mapper file path method"}, sqlSessionFactoryRef = "sqlSessionFactory"). For specific methods and steps, see https://www.cnblogs.com/niumoo/p/ 14209663.htmlb

This article recommends the second method, and you can choose freely according to your own situation.

2) Using the extended multi-data source package of Mabatis-plus can easily realize the configuration of multi-data sources, and there is no need to change the already built project directory structure and configration.

3.Mabatis-plus multiple data source configuration

1) About Mybatis-plus

MyBatis-Plus (opens new window) (MP for short) is an  enhancement tool for MyBatis (opens new window) . On the basis of MyBatis, only enhancements are made without changes, and it was born to simplify development and improve efficiency. So there is no need to worry that the use of Mabatis-plus in already built projects will affect the original method.

2) steps

2.1 Dependency removal:

MyBatis-Plus After importing  MyBatis and  re- introducing  MyBatis-Spring, there will be problems caused by version differences, so first delete the Mabatis dependency in the dependent library.

2.1 Dependency introduction:

1) mabatis-plus dependency

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.0</version>
</dependency>

The version number is the latest version, here I use 3.3.0;

2) Introduction of multi-data source expansion package

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  <version>3.3.0</version>
</dependency>

2.2 Create a configuration class:

@Configuration
@MapperScan("com.example.demo.mapper")
public class MyBatisConfiguration {

}

Let Springboot scan to the mapper interface folder of the project. If there is a configuration class before adding dependencies, there is no need to change it, just keep it unchanged.

2.3 Configure the datasource connection in the application.yml file

1) Create a new application.yml file

If the properties file has been used before, create a new application.yml file, and both files will configure the project.

2) Add the following data source configuration in the yml file

spring:
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        master:
          url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
        slave_1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
        slave_2:
          url: ENC(xxxxx) # 内置加密,使用请查看详细文档
          username: ENC(xxxxx)
          password: ENC(xxxxx)
          driver-class-name: com.mysql.jdbc.Driver
       #......省略
       #以上会配置一个默认库master,一个组slave下有两个子库slave_1,slave_2

3) Configuration mode

# 多主多从                      纯粹多库(记得设置primary)                   混合配置
spring:                               spring:                              spring:
  datasource:                           datasource:                           datasource:
    dynamic:                              dynamic:                              dynamic:
      datasource:                           datasource:                           datasource:
        master_1:                             mysql:                                master:
        master_2:                             oracle:                               slave_1:
        slave_1:                              sqlserver:                            slave_2:
        slave_2:                              postgresql:                           oracle_1:
        slave_3:                              h2:                                   oracle_2:

2.4 Use @DS to switch data sources .

 Example: Annotate the mapper interface for database operations to implement the corresponding database operations.

@Repository
public interface IExhibitContentMapper {
    
    @DS("exhibit_content")
    int createExhibitContentTable(String tableName);

    @DS("exhibit_content")
    int insertExhibitContent(@Param("tableName") String table,@Param("part") String content);

    @DS("exhibit_content")
    int deleteExhibitContent(@Param("tableName") String table);

    @DS("exhibit_content")
    List<ExhibitContentVO> getExhibitContent(@Param("tableName") String table);
}

4. Mabatis-plus expands the usage specification of multiple data sources

1) This framework only does the core thing of switching data sources, and does not limit your specific operations. You can do any CRUD after switching data sources.

_ 2) The header of all data sources separated by underscores in the configuration file  is the name of the group, and data sources with the same group name will be placed under one group.

3) Switch data source can be a group name or a specific data source name. When the group name is switched, the load balancing algorithm is used to switch.

4) The default data source name is master, you can  spring.datasource.dynamic.primary modify it.

5) Annotations on methods take precedence over annotations on classes.

6) DS supports inheritance of DS on abstract classes, but does not support inheritance of DS on interfaces.

Guess you like

Origin blog.csdn.net/qq_43780761/article/details/126535591