mybatis plus 多数据源 事物
时间: 2023-09-12 14:11:37 浏览: 121
在 MyBatis Plus 中使用多数据源和事务的步骤如下:
1. 配置多数据源:在 `application.properties` 或 `application.yml` 中配置多个数据源的连接信息,例如:
```yaml
spring:
datasource:
master:
url: jdbc:mysql://localhost:3306/master_db
username: root
password: root123
slave:
url: jdbc:mysql://localhost:3306/slave_db
username: root
password: root123
```
2. 创建数据源和事务管理器:在配置类中定义多个数据源,并创建对应的事务管理器,例如:
```java
@Configuration
public class DataSourceConfig {
@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 = "masterTransactionManager")
public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "slaveTransactionManager")
public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
```
3. 配置 MyBatis Plus 的 SqlSessionFactory 和 MapperScannerConfigurer:在配置类中配置 MyBatis Plus 的 SqlSessionFactory 和 MapperScannerConfigurer,例如:
```java
@Configuration
@MapperScan(basePackages = {"com.example.mapper"})
public class MyBatisConfig {
@Autowired
@Qualifier("masterDataSource")
private DataSource masterDataSource;
@Autowired
@Qualifier("slaveDataSource")
private DataSource slaveDataSource;
@Autowired
@Qualifier("masterTransactionManager")
private DataSourceTransactionManager masterTransactionManager;
@Autowired
@Qualifier("slaveTransactionManager")
private DataSourceTransactionManager slaveTransactionManager;
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
return sessionFactory.getObject();
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(slaveDataSource);
return sessionFactory.getObject();
}
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "slaveSqlSessionTemplate")
public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
scannerConfigurer.setBasePackage("com.example.mapper");
return scannerConfigurer;
}
}
```
4. 使用注解指定数据源和事务:在需要使用多数据源和事务的地方,使用 `@Transactional` 注解指定数据源和事务管理器,例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("masterSqlSessionTemplate")
private SqlSessionTemplate masterSqlSessionTemplate;
@Autowired
@Qualifier("slaveSqlSessionTemplate")
private SqlSessionTemplate slaveSqlSessionTemplate;
@Override
@Transactional(transactionManager = "masterTransactionManager")
public void saveUser(User user) {
masterSqlSessionTemplate.getMapper(UserMapper.class).save(user);
}
@Override
@Transactional(transactionManager = "slaveTransactionManager")
public User getUserById(Long userId) {
return slaveSqlSessionTemplate.getMapper(UserMapper.class).getById(userId);
}
}
```
以上步骤可以帮助你在 MyBatis Plus 中实现多数据源和事务的配置与使用。
阅读全文