在做项目过程中,难免会遇到需要同时连接多个数据的情况,这时就需要用到多数据源,配合MP自带的组件,可以很方便的将多数据源和IService相结合。
快速上手
示例源码
官方文档
数据库
MySQL
1 | DROP TABLE IF EXISTS `phone`; |
2 | CREATE TABLE `phone` ( |
3 | `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
4 | `name` varchar(50) NOT NULL, |
5 | PRIMARY KEY (`id`) |
6 | ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4; |
7 | |
8 | INSERT INTO `phone` (`id`, `name`) VALUES (1, 'Apple'); |
9 | INSERT INTO `phone` (`id`, `name`) VALUES (2, '小米'); |
10 | INSERT INTO `phone` (`id`, `name`) VALUES (3, '华为'); |
11 | INSERT INTO `phone` (`id`, `name`) VALUES (4, '一加'); |
SQLServer
1 | CREATE TABLE [dbo].[car] ( |
2 | [id] bigint NOT NULL, |
3 | [name] varchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL |
4 | ) |
5 | GO |
6 | ALTER TABLE [dbo].[car] SET (LOCK_ESCALATION = TABLE) |
7 | GO |
8 | BEGIN TRANSACTION |
9 | GO |
10 | INSERT INTO [dbo].[car] ([id], [name]) VALUES (N'1', N'吉利') |
11 | GO |
12 | INSERT INTO [dbo].[car] ([id], [name]) VALUES (N'2', N'福特') |
13 | GO |
14 | INSERT INTO [dbo].[car] ([id], [name]) VALUES (N'3', N'斯巴鲁(杂牌)') |
15 | GO |
16 | COMMIT |
17 | GO |
Maven依赖
1 |
|
2 | <dependency> |
3 | <groupId>com.baomidou</groupId> |
4 | <artifactId>mybatis-plus-boot-starter</artifactId> |
5 | </dependency> |
6 | <dependency> |
7 | <groupId>com.baomidou</groupId> |
8 | <artifactId>dynamic-datasource-spring-boot-starter</artifactId> |
9 | </dependency> |
⚠️根据springboot版本二选一
- 1.5.x~2.x.x使用
dynamic-datasource-spring-boot-starter
- 3及以上使用
dynamic-datasource-spring-boot3-starter
application.yml
1 | spring: |
2 | datasource: |
3 | dynamic: |
4 | primary: mysql |
5 | datasource: |
6 | mysql: |
7 | url: jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8 |
8 | username: root |
9 | password: root |
10 | driver-class-name: com.mysql.cj.jdbc.Driver |
11 | sqlserver: |
12 | url: jdbc:sqlserver://127.0.0.1:1433;databaseName=db2;encrypt=false;trustServerCertificate=true |
13 | username: sa |
14 | password: 123456 |
15 | driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver |
配置里的mysql
、sqlserver
名字可以随便取,如果加下划线,则会对下划线前进行分组
Service
数据源1
1 | @Service |
2 | public class PhoneServiceImpl extends ServiceImpl<PhoneMapper, Phone> implements PhoneService { |
3 | } |
因为不是严格模式,所以在不加@DS
的情况下,默认访问了mysql
这个数据源
数据源2
1 | @DS("sqlserver") |
2 | @Service |
3 | public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService { |
4 | } |
加了@DS("sqlserver")
之后,自动通过sqlserver
这个数据源来访问
使用
1 | @Resource |
2 | private PhoneService phoneService; |
3 | |
4 | @Resource |
5 | private CarService carService; |
6 | |
7 | @PostConstruct |
8 | public void print(){ |
9 | log.info("数据源1"); |
10 | for (Phone phone : phoneService.list()) { |
11 | log.info(phone); |
12 | } |
13 | log.info("数据源2"); |
14 | for (Car car : carService.list()) { |
15 | log.info(car); |
16 | } |
17 | } |
打印两个不同数据源的Service,控制台输出如下
1 | 数据源1 |
2 | Phone(id=1, name=Apple) |
3 | Phone(id=2, name=小米) |
4 | Phone(id=3, name=华为) |
5 | Phone(id=4, name=一加) |
6 | 数据源2 |
7 | Car(id=1, name=吉利) |
8 | Car(id=2, name=福特) |
9 | Car(id=3, name=斯巴鲁(杂牌)) |