MyBatis-Plus multiple data sources - how to use multiple MySQL databases in one project

Insert image description here

Preface

MyBatis-Plus (opens new window) (referred to as MP) is an enhancement tool for MyBatis (opens new window). Based on MyBatis, it only enhances without making changes. It is born to simplify development and improve efficiency.

This series of blogs combines practical application scenarios to explain the problems and usage methods of MyBatis-Plus in practical applications.

This blog introduces how to use multiple data sources in a project based on mybatisPlus.

Official website: https://baomidou.com/

lead out


1. Usage scenarios of mybatisPlus multiple data sources;
2. Using multiple data sources in one project based on mybatisPlus;

1. Multiple data source application scenarios

1. Master-slave synchronization, separation of reading and writing

Master-slave synchronization, separation of reading and writing

Insert image description here

Create a data user and control its permissions

mysql> create user 'slave01'@'%' identified WITH mysql_native_password by '123';
Query OK, 0 rows affected (0.00 sec)
mysql> grant select ON *.* to 'slave01'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

2. The data may be stored in different data sources

You may need to check databases with different data sources

Insert image description here

2. Using multiple data sources in spring

1.Introduce dependencies

<!--        mybatis多数据源-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>

2. Configure multiple data sources

spring:
  main:
    allow-circular-references: true
  datasource:
    dynamic:
      primary: mysql_centos #配置主数据源
      datasource:
        mysql_yun: # 数据源的名字,第一个数据源
          url: jdbc:mysql://124.70.138.34:3306/fresh_db_test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true
          username: root
          password: XXXX
          driver-class-name: com.mysql.cj.jdbc.Driver
        mysql_centos: # 数据源的名字,第二个数据源
          url: jdbc:mysql://192.168.111.130:3306/fresh_customer_db?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true
          username: root
          password: 123
          driver-class-name: com.mysql.cj.jdbc.Driver
      druid:
        initial-size: 1
        max-active: 20
        min-idle: 1
        max-wait: 60000
  # 去除一下durid的自动装配
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration   #去除Druid自动配置

Insert image description here

3. Use @DS annotation identification

Insert image description here

package com.tianju.fresh.service;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.tianju.fresh.entity.Customer;
import com.tianju.fresh.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TestManyDatasource {
    
    
    @Autowired
    private CustomerMapper customerMapper;

    @DS("mysql_yun")
    public void findFromHuawei(){
    
    
        System.out.println("云服务器上的数据库");
        List<Customer> all = customerMapper.findAll();
        System.out.println(all);
    }
    @DS("mysql_centos")
    public void findFromLocal(){
    
    
        System.out.println("本地虚拟机的数据库");
        List<Customer> all = customerMapper.findAll();
        System.out.println(all);
    }
}

3.Default database

Primary in the configuration yml file
: mysql_centos #Configuring the primary data source
has the following functions, the default database

Insert image description here

4. Determine which database to go to based on the annotation

@DS("mysql_yun")
determines which data source to search based on the annotation

Insert image description here

package com.tianju.fresh;


import com.tianju.fresh.service.TestManyDatasource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ManyDataSourceTest {
    
    

    @Autowired
    private TestManyDatasource manyDatasource;

    @Test
    public void test2(){
    
    
        manyDatasource.findFromHuawei();
        manyDatasource.findFromLocal();
    }

}

Summarize

1. Usage scenarios of mybatisPlus multiple data sources;
2. Using multiple data sources in one project based on mybatisPlus;

Guess you like

Origin blog.csdn.net/Pireley/article/details/133344286