使用mybatis配置多数据源
一、查阅官方文档
Mybatis-多数据源: https://baomidou.com/pages/a61e1b/#%E6%96%87%E6%A1%A3-documentation
- 引入dynamic-datasource-spring-boot-starter
- 配置数据源
- 使用@DS切换数据源
当然在这之前需要做一些准备工作
二、准备工作
1. 创建数据库
- mybatis_plus
CREATE DATABASE mybatis_plus;
USE `mybatis_plus`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- mybatis_plus_1
CREATE DATABASE mybatis_plus_1;
USE `mybatis_plus_1`;
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` bigint(20) NOT NULL COMMENT '主键ID',
`NAME` varchar(30) DEFAULT NULL COMMENT '商品名称',
`price` int(11) DEFAULT '0' COMMENT '价格',
`VERSION` int(11) DEFAULT '0' COMMENT '乐观锁版本号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2. 新建项目
三、引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo03</name>
<description>demo03</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- MySql -->
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四、配置文件
配置多数据源
spring:
# 配置数据源信息
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
slave_1:
url: jdbc:mysql://localhost:3306/mybatis_plus_1?characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
五、编写测试代码
1. 实体类
Product
import lombok.Data;
@Data
public class Product {
private Long id;
private String name;
private Integer price;
}
User
@Data
public class User{
private Long id;
private String name;
private Integer age;
private String email;
}
2. service
- UserService
public interface UserService extends IService<User> {
}
- UserServiceImpl
// 注意这里写配置文件中数据源的名称
@DS("master")
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
- ProductService
public interface ProductService extends IService<Product> {
}
- ProductServiceImpl
//数据源名称
@DS("slave_1")
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
3. mapper
- UserMapper
public interface UserMapper extends BaseMapper<User> {
}
- ProductMapper
public interface ProductMapper extends BaseMapper<Product> {
}
4. 启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//这里配置包扫描的路径 mapper
@MapperScan("com.example.demo03.mapper")
public class Demo03Application {
public static void main(String[] args) {
SpringApplication.run(Demo03Application.class, args);
}
}
5. 测试类
@SpringBootTest
class Demo03ApplicationTests {
@Autowired
private UserService userService;
@Autowired
private ProductService productService;
@Test
public void testDynamicDataSource() {
System.out.println(userService.getById(1L));
System.out.println(productService.getById(1L));
}
}
测试结果
配置多数据源成功