The foreword 0.

At present springBoot + Mybatis – Plus framework combination is more and more widely used in enterprises. Recently, a requirement was encountered to integrate multiple data sources.

To explain what multi-data sources are, multi-data sources means that I need to connect two or more databases in a project. It is common for a project to connect to one database, but there are special cases where multiple databases need to be connected. It is important to note that the data sources are defined by libraries, and as long as the libraries are different, they represent different data sources, regardless of whether they are on the same machine. The introduction of multiple data sources is generally accompanied by the problem of distributed transactions, which will not be discussed here.

The use of multiple data sources was implemented a long time ago, but at that time it was a bit of a hassle because the frameworks were not that mature. However, this time in the research, found that Mybatis – Plus has encapsulated the relevant operations, only a few steps can be easily realized, it is not too cool.

First give reference address: multiple source | MyBatis – Plus mp.baomidou.com/guide/dynam…

It’s very simple. It’s actually very easy to follow the steps. Here are some simple steps.

1. Introducing dependencies:

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.5.0</version>
</dependency>
Copy the code

Here we use 3.5.0, the latest version of the current period.

2. Add configurations

Next comes configuring the data source: directly in the configuration file

server:
  port: 9999
spring:
  datasource:
    dynamic:
      primary: master Set the default data source or data source group to master
      strict: false If the specified data source is not matched, the default value is false
      datasource:
        master:
          url: jdbc:mysql://localhost:3306/ds0? serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver # 3.2.0 Starting SPI support can omit this configuration
        slave_1:
          url: jdbc:mysql://localhost:3306/ds1? serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
Copy the code

There are two configurations: master and Slave_1. If you need to configure anything else, just do it below. For example, slave_2. Note that if slave_2 is added, slave_1 and slave_2 will be grouped according to the prefix. You can specify the value based on the group name. Officially as follows;

3. Modify the specified data source

@Service
@DS("master")
public class UserServiceImpl extends ServiceImpl<UserMapper.User> implements UserService {}Copy the code
@Service
@DS("slave")
public class AddressServiceImpl extends ServiceImpl<AddressMapper.Address> implements AddressService {}Copy the code

For my two tables, the User table corresponds to the master data source, and the address table corresponds to the slave data source.

4. Test

Create a Controller and test it with a browser request.

/ * * *@className: MultiDatasourceController
 * @description:
 * @author: sh.Liu
 * @date: the 2021-12-03 15:20 * /
@RestController
@RequestMapping("/")
public class MultiDatasourceController {
 
    private final UserService userService;
    private final AddressService addressService;
 
    public MultiDatasourceController(UserService userService, AddressService addressService) {
        this.userService = userService;
        this.addressService = addressService;
    }
 
    @RequestMapping("test")
    public String test(a) {
        User user= new User();
        user.setUserId(3L);
        user.setUserName("aaaa");
        userService.save(user);
 
        Address address = new Address();
        address.setAddressId(3L);
        address.setAddressName("hello");
        addressService.save(address);
        return "success"; }}Copy the code

Simply write a controller, one to save User, one to save address, just like we normally use Mybatis – Plus, the result is that the User is saved in the master data source DS0 library, address is saved in the slave data source DS1.

Verification results:

All right, that’s it. How about it? It’s pretty simple.