适用于多种场景:纯粹多库、 读写分离、 一主多从、 混合模式等
目前来模拟一个纯粹多库的一个场景,其他场景类似
场景说明:
创建两个库,分别为:study(以前的库不动)与study_1(新建),将
study库的t_product表移动到study_1库,这样每个库一张表,通过一个测试用例
分别获取用户数据与商品数据,如果获取到说明多库模拟成功
1、创建数据库及表
创建数据库study_1和表t_product
CREATE DATABASE `study_1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `study_1`;
CREATE TABLE t_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)
);
添加测试数据
INSERT INTO t_product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
删除study库t_product表
use study;
DROP TABLE IF EXISTS t_product;
2、引入依赖,建议建个新项目
<!--mybatis-plus启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
<!--lombok用于简化实体类开发-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>4.3.1</version>
</dependency>
3、配置多数据源
说明:注释掉之前的数据库连接,添加新配置,也可以建个新项目
spring:
# 配置数据源信息
datasource:
dynamic:
# 设置默认的数据源或者数据源组,默认值即为master
primary: master
# 严格匹配数据源,默认false.true未匹配到指定数据源时抛异常,false使用默认数据源
strict: false
datasource:
master:
url: jdbc:mysql://localhost:3306/study?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver
slave_1:
url: jdbc:mysql://localhost:3306/study_1?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password:
driver-class-name: com.mysql.cj.jdbc.Driver
4、创建用户service
User
@Data
@TableName("t_user")
public class User {
@TableId
private Integer uid;
private String userName;
private Integer age;
private Integer sex;
private String email;
private Integer isDeleted;
}
UserMapper
@Repository
public interface UserMapper extends BaseMapper<User> {
}
UserService
public interface UserService extends IService<User> {
}
UserServiceImpl
@Service
@DS("master")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
5、创建商品service
Product
@Data
@TableName("t_product")
public class Product {
private Integer id;
private String name;
private Integer price;
private Integer version;
}
ProductMapper
@Repository
public interface ProductMapper extends BaseMapper<Product> {
}
ProductService
public interface ProductService extends IService<Product> {
}
ProductServiceImpl
@Service
@DS("slave_1")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
6、启动类扫描mapper包
@SpringBootApplication
@MapperScan("top.qaqaq.mybatisplus.mapper")
public class MybatisplusDatasourceApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusDatasourceApplication.class, args);
}
}
7、测试
@Autowired
private UserService userService;
@Autowired
private ProductService productService;
@Test
public void test() {
System.out.println(userService.getById(1));
System.out.println(productService.getById(1));
}
结果:
1、都能顺利获取对象,则测试成功
2、如果我们实现读写分离,将写操作方法加上主库数据源,读操作方法加上从库数据源,自动切
换,是不是就能实现读写分离?