mybatisplus 怎么配置多数据源
时间: 2023-06-11 08:10:24 浏览: 165
Mybatis-Plus 支持多数据源配置。可以使用 Spring Boot 的多数据源配置,也可以通过 Mybatis-Plus 提供的 `MybatisSqlSessionFactoryBean` 实现多数据源配置。
下面是使用 `MybatisSqlSessionFactoryBean` 实现多数据源配置的示例:
1. 创建数据源配置类
```java
@Configuration
public class DataSourceConfig {
@Bean("db1")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1() {
return DataSourceBuilder.create().build();
}
@Bean("db2")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db2() {
return DataSourceBuilder.create().build();
}
}
```
2. 创建 Mybatis Sql Session Factory Bean
```java
@Configuration
public class MybatisConfig {
@Bean
@Primary
public MybatisSqlSessionFactoryBean db1SqlSessionFactory(@Qualifier("db1") DataSource dataSource) {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
return sqlSessionFactoryBean;
}
@Bean
public MybatisSqlSessionFactoryBean db2SqlSessionFactory(@Qualifier("db2") DataSource dataSource) {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
return sqlSessionFactoryBean;
}
@Bean
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
```
3. 配置多数据源事务管理器
```java
@Configuration
@EnableTransactionManagement
public class TransactionConfig {
@Bean
public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
```
4. 配置数据源选择切换器
```java
@Configuration
public class DataSourceSwitchConfig {
@Bean
@Primary
public DataSource dataSource(@Qualifier("db1") DataSource db1, @Qualifier("db2") DataSource db2) {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.DB1.name(), db1);
targetDataSources.put(DataSourceType.DB2.name(), db2);
dynamicDataSource.setTargetDataSources(targetDataSources);
dynamicDataSource.setDefaultTargetDataSource(db1);
return dynamicDataSource;
}
@Bean
public DataSourceContextHolder dataSourceContextHolder() {
return new DataSourceContextHolder();
}
@Bean
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public AspectJExpressionPointcutAdvisor dataSourceSwitchAdvisor(@Qualifier("dataSource") DataSource dataSource) {
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
advisor.setExpression("execution(public * com.example.demo.*.service.*.*(..))");
advisor.setAdvice(new DataSourceSwitchAdvice(dataSource));
return advisor;
}
}
```
其中,`DynamicDataSource` 是继承自 `AbstractRoutingDataSource` 的动态数据源,`DataSourceType` 是枚举类型,用于定义数据源类型,`DataSourceContextHolder` 是用于存储当前数据源类型的上下文,`DataSourceSwitchAdvice` 是用于切换数据源的 AOP 切面。
阅读全文