序文
MyBatis-Plus (新しいウィンドウで開きます) (以下、MP) は、MyBatis (新しいウィンドウで開きます) の拡張ツールであり、MyBatis をベースに、変更を加えることなく拡張のみを行い、開発の簡素化と効率化を目的として生まれました。
このブログ シリーズでは、実践的なアプリケーションのシナリオを組み合わせて、実践的なアプリケーションにおける MyBatis-Plus の問題点と使用方法を説明します。
このブログでは、mybatisPlus に基づいたプロジェクトで複数のデータ ソースを使用する方法を紹介します。
目次
導き出す
1. mybatisPlus の複数データ ソースの使用シナリオ;
2. mybatisPlus に基づく 1 つのプロジェクトで複数のデータ ソースを使用する; 3.
1. 複数のデータ ソース アプリケーションのシナリオ
1. マスタとスレーブの同期、読み出しと書き込みの分離
マスターとスレーブの同期、読み出しと書き込みの分離
データユーザーを作成し、その権限を制御する
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. データは異なるデータ ソースに保存される場合があります
異なるデータソースを含むデータベースを確認する必要がある場合があります
2. 春に複数のデータソースを使用する
1.依存関係を導入する
<!-- mybatis多数据源-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.6</version>
</dependency>
2. 複数のデータソースを構成する
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自动配置
3. @DS アノテーション識別を使用する
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.デフォルトデータベース
設定 yml ファイルのプライマリ
: mysql_centos #プライマリ データ ソースの設定には
次の機能があります。デフォルトのデータベース
4. 注釈に基づいてどのデータベースに移動するかを決定します
@DS("mysql_yun") は、
アノテーションに基づいて検索するデータ ソースを決定します。
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();
}
}
要約する
1. mybatisPlus の複数データ ソースの使用シナリオ;
2. mybatisPlus に基づく 1 つのプロジェクトで複数のデータ ソースを使用する; 3.