官网地址:多数据源 | MyBatis-Plus (baomidou.com)
支持 数据源分组 ,适用于多种场景、纯粹多库、读写分离、一主多从、混合模式。
目前来模拟一个纯粹多库的一个场景,其他场景类似;场景说明:
创建两个库,分别为:mybatis_plus与mybatis_plus_1,使每个库一张表,通过一个测试用例分别获取用户数据与商品数据,如果获取到说明多库模拟成功。
创建数据库mybatis_plus和表user
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
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和表product
CREATE DATABASE `mybatis_plus_1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus_1`;
CREATE TABLE product
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名称',
price INT(11) DEFAULT 0 COMMENT '价格',
version INT(11) DEFAULT 0 COMMENT '乐观锁版本号',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
添加测试数据
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
INSERT INTO product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
重新创建SpringBoot项目,参考MyBatisPlus–入门案例_杼蛘的博客-CSDN博客
在pom.xml
中新引入如下依赖:
<dependency>
<groupId>com.baomidougroupId>
<artifactId>dynamic-datasource-spring-boot-starterartifactId>
<version>3.5.0version>
dependency>
说明:注释掉之前的数据库连接,添加新配置
spring:
# 配置数据源信息
datasource:
dynamic:
# 设置默认的数据源或者数据源组,默认值即为master
primary: master
# 严格匹配数据源,默认false.true未匹配到指定数据源时抛异常,false使用默认数据源
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf8&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=utf8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
创建User
类,路径为src/main/java/com/aiw/mybatisplus_datasourse/pojo/User.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User {
@TableId
private Integer id;
private String name;
private Integer age;
private String email;
}
创建Product
类,路径为src/main/java/com/aiw/mybatisplus_datasourse/pojo/Product.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("product")
public class Product {
@TableId
private Integer id;
private String name;
private Integer price;
private Integer version;
}
创建UserMapper
接口,路径为src/main/java/com/aiw/mybatisplus_datasourse/mapper/UserMapper.java
public interface UserMapper extends BaseMapper<User> {
}
创建ProductMapper
接口,路径为src/main/java/com/aiw/mybatisplus_datasourse/mapper/ProductMapper.java
public interface ProductMapper extends BaseMapper<Product> {
}
创建UserService
接口,路径为src/main/java/com/aiw/mybatisplus_datasourse/service/UserService.java
public interface UserService extends IService<User> {
}
创建ProductService
接口,路径为src/main/java/com/aiw/mybatisplus_datasourse/service/ProductService.java
public interface ProductService extends IService<Product> {
}
创建UserServiceImpl
类,路径为src/main/java/com/aiw/mybatisplus_datasourse/service/Impl/UserServiceImpl.java
@DS("master") //指定所操作的数据源
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
创建ProductServiceImpl
类,路径为src/main/java/com/aiw/mybatisplus_datasourse/service/Impl/ProductServiceImpl.java
@DS("slave_1") //指定所操作的数据源
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
在启动类上添加@MapperScan
注解,路径为src/main/java/com/aiw/mybatisplus_datasourse/MyBatisPlusDatasourseApplication.java
@SpringBootApplication
@MapperScan("com.aiw.mybatisplus_datasourse.mapper")
public class MyBatisPlusDatasourseApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisPlusDatasourseApplication.class, args);
}
}
在MyBatisPlusDatasourseApplicationTests.java
中写如下测试方法:
@SpringBootTest
class MyBatisPlusDatasourseApplicationTests {
@Autowired
private UserService userService;
@Autowired
private ProductService productService;
@Test
public void testDynamicDataSource(){
System.out.println(userService.getById(1));
System.out.println(productService.getById(1));
}
}
结果:
1、都能顺利获取对象,则测试成功
2、如果要实现读写分离,将写操作方法加上主库数据源,读操作方法加上从库数据源,自动切换,是不是就能实现?