mybatis-plus 多数据源
时间: 2023-09-13 18:13:57 浏览: 130
mybatisplus 配置多数据源
mybatis-plus 支持多数据源配置,可以通过配置不同的数据源来连接不同的数据库。下面是一个简单的示例。
1. 配置数据源
在配置文件中添加多个数据源的配置,例如:
```
spring.datasource.master.url=jdbc:mysql://localhost:3306/master?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.master.username=root
spring.datasource.master.password=root
spring.datasource.slave.url=jdbc:mysql://localhost:3306/slave?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
spring.datasource.slave.username=root
spring.datasource.slave.password=root
```
2. 配置多数据源
在 MybatisPlusConfig 类中配置多数据源,例如:
```java
@Configuration
public class MybatisPlusConfig {
@Primary
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
@Qualifier("slaveDataSource") DataSource slaveDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put("master", masterDataSource);
targetDataSources.put("slave", slaveDataSource);
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.setTargetDataSources(targetDataSources);
dynamicDataSource.setDefaultTargetDataSource(masterDataSource);
return dynamicDataSource;
}
@Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dynamicDataSource);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*.xml"));
sqlSessionFactory.setGlobalConfig(globalConfiguration());
return sqlSessionFactory.getObject();
}
@Bean
public GlobalConfiguration globalConfiguration() {
GlobalConfiguration globalConfiguration = new GlobalConfiguration();
globalConfiguration.setSqlInjector(new LogicSqlInjector());
globalConfiguration.setMetaObjectHandler(new MyMetaObjectHandler());
globalConfiguration.setDbType(DbType.MYSQL);
return globalConfiguration;
}
}
```
3. 配置动态数据源
在 DynamicDataSource 类中实现动态数据源,例如:
```java
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSource();
}
}
```
4. 设置数据源
在需要使用数据源的地方,通过设置数据源的方式来使用不同的数据源,例如:
```java
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public List<User> listByDataSource(String dataSource) {
DataSourceContextHolder.setDataSource(dataSource);
List<User> userList = list();
DataSourceContextHolder.clearDataSource();
return userList;
}
}
```
以上就是 mybatis-plus 多数据源的简单示例,实际应用中需要根据具体情况进行配置和调整。
阅读全文