一、简述
dynamic-datasource-spring-boot-starter 是一个基于springboot的快速集成多数据源的启动器。其支持 Jdk 1.7+, SpringBoot 1.4.x 1.5.x 2.x.x。
二、准备数据
多数据源适用于多种场景例如:纯粹多库、 读写分离、 一主多从、 混合模式等。这里模拟一个多个库的一个场景,其他场景类似,场景说明:
- 创建两个库,分别为:mydb(以前的库不动)与mybatis_plus(新建)
- 在mydb库创建t_user表,在mybatis_plus库创建表t_product,这样保持每个库一张表,通过一个测试用例分别获取用户数据与商品数据,如果获取到说明多库模拟成功
创建表:
CREATE TABLE `t_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=utf8;
CREATE TABLE `t_user` (
`uid` bigint(20) NOT NULL DEFAULT '0' COMMENT '主键ID',
`user_name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`sex` int(11) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
`is_delete` int(255) DEFAULT '0',
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
三、环境搭建
创建一个新的springboot项目
3.1.在pom.xml中引入依赖
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!--引入多数据源驱动-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<!--mybatis-plus启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!--MySQL数据库连接的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
3.2.在application.yml中配置多数据源
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
url: jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
slave_1:
url: jdbc:mysql://127.0.0.1:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
3.3.在com.augus.pojo下创建实体类
package com.augus.pojo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
@Data
@TableName("t_user")
public class User implements Serializable {
//指定id对应的表中主键字段为uid,主键策略为自增
//@TableId(value = "uid", type = IdType.AUTO)
@TableId(value = "uid")
private Long id;
@TableField(value = "user_name")
private String name;
private Integer age;
private Integer sex;
private String email;
private Integer is_delete;
}
package com.augus.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
@Data
@TableName("t_product")
public class Product implements Serializable {
private Long id;
private String name;
private Integer price;
private Integer version;
}
3.4.在com.augus.service下创建接口和实现类
userservice
package com.augus.service;
import com.augus.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;
//这里接口需要基础IService,泛型需要指定对应的实体类
public interface UserSerivce extends IService<User> {
}
package com.augus.service.impl;
import com.augus.mapper.UserMapper;
import com.augus.pojo.User;
import com.augus.service.UserSerivce;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
@DS("master") //指定所操作的数据源
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserSerivce {
}
ProductService
package com.augus.service;
import com.augus.pojo.Product;
import com.baomidou.mybatisplus.extension.service.IService;
public interface ProductService extends IService<Product> {
}
package com.augus.service.impl;
import com.augus.mapper.ProductMapper;
import com.augus.pojo.Product;
import com.augus.service.ProductService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
@DS("slave_1")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}
四、创建测试类
创建测试,代码如下:
package com.augus;
import com.augus.service.ProductService;
import com.augus.service.UserSerivce;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MybatisPlusDynamicDataSource {
@Autowired
private UserSerivce userSerivce;
@Autowired
private ProductService productService;
@Test
public void testDynamicDataSource(){
//分别从user表和product表中获取数据
System.out.println(userSerivce.getById(1598884966689693697L));
System.out.println(productService.getById(1));
}
}
执行后,结果如下,可以使用多个数据源从两个表中读取数据: