MP集成多数据源的官方文档:多数据源 | MyBatis-Plus
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druid-spring-boot-starterartifactId>
- <version>1.2.11version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.21version>
- dependency>
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.3.1version>
- dependency>
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>dynamic-datasource-spring-boot-starterartifactId>
- <version>3.3.1version>
- dependency>
- spring:
- datasource:
- dynamic:
- primary: master #设置默认的数据源或者数据源组, 默认值即为master
- strict: false #严格匹配数据源,默认false,true未匹配到指定数据源时抛异常,false使用默认数据源
- datasource:
- # 主数据源
- master:
- url: jdbc:mysql://localhost:3306/table1?serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true
- username: root
- password: "password"
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.cj.jdbc.Driver
- # 从数据源
- slave_1:
- url: jdbc:mysql://localhost:3306/table2?serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true
- username: root
- password: "password"
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.cj.jdbc.Driver
- # 多主多从 纯粹多库(记得设置primary) 混合配置
- spring: spring: spring:
- datasource: datasource: datasource:
- dynamic: dynamic: dynamic:
- datasource: datasource: datasource:
- master_1: mysql: master:
- master_2: oracle: slave_1:
- slave_1: sqlserver: slave_2:
- slave_2: postgresql: oracle_1:
- slave_3: h2: oracle_2:
在ServiceImpl类/ServiceImpl类的方法上加@DS注解
注解加到ServiceImpl类上 --> 类里的方法都使用此数据源
注解加到ServiceImpl的方法上 --> 使用方法上的注解的数据源(方法上的注解 优先于 类上注解)
- @Service
- @DS("slave_1")
- public class UserServiceImpl extends ServiceImpl
implements UserService { -
- @Resource
- private UserMapper userMapper;
-
- @Override
- @DS("master")
- public List
getUser() { - return userMapper.selectList(null);
- }
- }
不加@DS注解,使用默认数据源,默认值master数据源
启动主类需要排除Druid相关依赖
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
否则会出现如下错误
- ***************************
- APPLICATION FAILED TO START
- ***************************
-
- Description:
-
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
-
- Reason: Failed to determine a suitable driver class
- @Service("serviceA")
- public class ServiceImpl implements Service{
-
- @Override
- @DS("database1")
- public void functionA() {
- functionB(); //调用B方法且切换为B方法的所需要的数据源
- }
-
- @Override
- @DS("database2")
- public void function B() {}
- }
A方法调用B方法时,B方法的@DS注解不生效,会使用A方法配置的数据源,B方法的@DS("database2")将完全被A方法的@DS("database1")所覆盖。
解决: 在事务内是无法使用@DS进行切换数据源,在ServiceImpl的方法上使用下面注解,在执行此方法时,挂起外部事务,创建一个新的内部事务。
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)