mybatis-plus切换Druid数据源的完整代码和配置文件配置
时间: 2023-06-18 17:03:11 浏览: 162
首先,需要在pom.xml中添加依赖,如下:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
```
然后,需要在application.yml中配置Druid数据源和Mybatis-plus的配置,如下:
```yaml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: root
druid:
max-active: 20
initial-size: 1
max-wait: 60000
min-idle: 1
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
global-config:
db-config:
id-type: auto
field-strategy: not_empty
table-prefix: t_
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
map-underscore-to-camel-case: true
```
接着,我们需要创建一个数据源切换的工具类,如下:
```java
public class DynamicDataSourceContextHolder {
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
public static void setDataSourceType(String dataSourceType) {
CONTEXT_HOLDER.set(dataSourceType);
}
public static String getDataSourceType() {
return CONTEXT_HOLDER.get();
}
public static void clearDataSourceType() {
CONTEXT_HOLDER.remove();
}
}
```
然后,我们需要创建一个切面,用来在方法执行前切换数据源,如下:
```java
@Component
@Aspect
public class DynamicDataSourceAspect {
@Pointcut("@annotation(com.example.demo.annotation.DataSource)")
public void dataSourcePointCut() {}
@Around("dataSourcePointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
DataSource dataSource = signature.getMethod().getAnnotation(DataSource.class);
if (dataSource == null) {
DynamicDataSourceContextHolder.setDataSourceType("db1");
} else {
DynamicDataSourceContextHolder.setDataSourceType(dataSource.value());
}
try {
return point.proceed();
} finally {
DynamicDataSourceContextHolder.clearDataSourceType();
}
}
}
```
在需要使用不同数据源的方法上,我们可以使用@DataSource注解来指定数据源,如下:
```java
@DataSource("db2")
public List<User> selectUserList() {
return userMapper.selectList(null);
}
```
最后,我们需要在配置类中配置多个数据源,如下:
```java
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.db1")
public DataSource db1() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.db2")
public DataSource db2() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public DataSource dataSource(DataSource db1, DataSource db2) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put("db1", db1);
targetDataSources.put("db2", db2);
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
dataSource.setTargetDataSources(targetDataSources);
dataSource.setDefaultTargetDataSource(db1);
return dataSource;
}
}
```
以上就是Mybatis-plus切换Druid数据源的完整代码和配置文件配置。其中,DynamicRoutingDataSource是动态数据源的实现类,需要自行实现。
阅读全文