9.Mybatis-plus 多数据源环境
参考资料:https://www.bilibili.com/video/BV12R4y157Be?p=52
模拟多数据源环境
1.设置配置文件
spring:
# 配置数据源信息
datasource:
dynamic:
# 设置默认的数据源或数据源组,默认值为master
primary: master
# 严格匹配数据源,默认为false;为true时未匹配到指定数据源会抛出异常
strict: false
datasource:
# 主数据源
master:
url: jdbc:mysql://localhost:3306/db_mp?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
# 从数据源
slave_1:
url: jdbc:mysql://localhost:3306/db_mp1?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
假设现在有两个数据源一个为db_mp,其中包含员工表t_employee;另一个为db_mp1,其中包含商品信息表t_product
2.搭建环境
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
entity
Employee
package com.example.mybatisplus_datasource.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName("t_employee")
@Data
public class Employee {
private Integer id;
private String name;
private String gender;
private String email;
private String phoneNumber;
@TableField(value = "departmentId")
private Integer departmentId;
private Integer salary;
}
Product
package com.example.mybatisplus_datasource.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import lombok.Data;
@TableName("t_product")
@Data
public class Product {
private Integer id;
private String name;
private Integer price;
@Version//标识乐观锁版本号字段
private Integer version;
}
mapper
EmployeeMapper
package com.example.mybatisplus_datasource.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus_datasource.entity.Employee;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeMapper extends BaseMapper<Employee> {
}
ProductMapper
package com.example.mybatisplus_datasource.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus_datasource.entity.Product;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductMapper extends BaseMapper<Product> {
}
service
EmployeeService
package com.example.mybatisplus_datasource.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.mybatisplus_datasource.entity.Employee;
public interface EmployeeService extends IService<Employee> {
}
ProductService
package com.example.mybatisplus_datasource.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.mybatisplus_datasource.entity.Product;
public interface ProductService extends IService<Product> {
}
Impl
EmployeeServiceImpl
注解DS中的值和yml配置文件中的值相对应
package com.example.mybatisplus_datasource.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus_datasource.entity.Employee;
import com.example.mybatisplus_datasource.mapper.EmployeeMapper;
import com.example.mybatisplus_datasource.service.EmployeeService;
import org.springframework.stereotype.Service;
@Service
@DS("master")
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService {
}
ProductServiceImpl
package com.example.mybatisplus_datasource.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus_datasource.entity.Product;
import com.example.mybatisplus_datasource.mapper.ProductMapper;
import com.example.mybatisplus_datasource.service.EmployeeService;
import com.example.mybatisplus_datasource.service.ProductService;
import org.springframework.stereotype.Service;
@Service
@DS("slave_1")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
测试类
package com.example.mybatisplus_datasource;
import com.example.mybatisplus_datasource.service.EmployeeService;
import com.example.mybatisplus_datasource.service.ProductService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MybatisplusDatasourceApplicationTests {
@Test
void contextLoads() {
}
@Autowired
private EmployeeService employeeService;
@Autowired
private ProductService productService;
@Test
public void test(){
System.out.println(employeeService.getById(1));
System.out.println(productService.getById(1));
}
}
在测试类中使用getById方法查询数据库中的数据,根据返回结果判断是否成功配置多数据源环境,运行发现查询成功,多数据源环境成功配置。