关于“SpringBoot整合Mybatis实现多数据源配置与跨数据源事务实例”的完整攻略,我们可以分以下几个步骤来进行讲解:
- 添加依赖:在
pom.xml
中添加多数据源、 Mybatis 等相关依赖,例如:
<!-- Spring Boot 多数据源依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<!-- MyBatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
- 数据源配置:在
application.yml
中配置数据源信息,例如:
spring:
datasource:
master:
url: jdbc:mysql://localhost:3306/db_master?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
slave:
url: jdbc:mysql://localhost:3306/db_slave?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
- 配置 Mybatis:在
application.yml
中配置 Mybatis 相关信息,例如:
mybatis:
mapper-locations: classpath:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
- 多数据源配置:创建多数据源配置类,例如:
@Configuration
@MapperScan(basePackages = "com.example.mapper")
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 = "dynamicDataSource")
public DataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
@Qualifier("slaveDataSource") DataSource slaveDataSource){
DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put(DatabaseType.MASTER, masterDataSource);
dataSourceMap.put(DatabaseType.SLAVE, slaveDataSource);
dynamicRoutingDataSource.setDefaultTargetDataSource(masterDataSource);
dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);
return dynamicRoutingDataSource;
}
}
- 配置事务:在需要跨多个数据源的事务方法上添加
@Transactional(rollbackFor = Exception.class)
注解,并创建TransactionConfig
类进行事务管理,例如:
@Configuration
public class TransactionConfig {
@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) {
return new DataSourceTransactionManager(dynamicDataSource);
}
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dynamicDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/**/*.xml"));
return sessionFactory.getObject();
}
@Bean(name = "sqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
至此,就完成了 SpringBoot 整合 Mybatis 实现多数据源配置与跨数据源事务实例的配置。
下面展示两个示例:
- 多数据源查询
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private SlaveUserMapper slaveUserMapper;
public List<User> getAllUsers() {
List<User> users = userMapper.getAllUsers();
List<User> userFromSlave = slaveUserMapper.getAllUsers();
users.addAll(userFromSlave);
return users;
}
}
- 跨数据源事务
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private OrderMapper orderMapper;
@Transactional(rollbackFor = Exception.class)
public void updateUserInfoAndOrder(Order order, User user) {
userMapper.updateUserInfo(user);
orderMapper.updateOrder(order);
if (1 == 1) {
throw new RuntimeException("Exception");
}
}
}
以上这些就是 SpringBoot 整合 Mybatis 实现多数据源配置与跨数据源事务实例的详细步骤和示例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot整合Mybatis实现多数据源配置与跨数据源事务实例 - Python技术站