mybatisplus事务管理器_SpringBoot解决多数据源分布式事务
的问题
Spring Boot解决多数据源分布式事务的问题
⼀、问题产⽣背景
Spring Boot处理多数据源可以采⽤MyBatis-Plus解决⽅案,多数据源处理数据时引发分布式事务的问题,本⽂讲解如何处理多数据源分布式事务。
⼆、构建多数据源
步骤⼀、构建Spring Boot⼯程
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
步骤⼆、引⼊Spring Boot切⼊坐标
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
步骤三、引⼊MyBatis-Plus
com.baomidou
mybatis-plus-generator
3.3.0
com.baomidou
dynamic-datasource-spring-boot-starter
3.2.0
步骤四、引⼊连接池
com.alibaba
druid-spring-boot-starter
1.1.10
步骤五、引⼊分布式事务开源插件XA
org.springframework.boot
spring-boot-starter-jta-atomikos
2.1引⼊多数据源l配置
spring:
aop:
proxy-target-class: true
auto: true
datasource:
druid:
crk:
url: jdbc:mysql://192.168.1.26:3306/cas_dev?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: zhet
password: zhlc123
driver-class-name: sql.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
yw:
url: jdbc:mysql://192.16.1.17:3306/base-quartz-dev?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: aiclouddev
password: 123456
driver-class-name: sql.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
这样就构建了2个数据源,下⾯开始代码的配置
2.2配置主数据源
构建主数据源及扫描包
@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = "vv.k",
sqlSessionFactoryRef = "priSqlSessionFactory")
public class MybatisPlusConfig {
@Bean("primaryInterceptor")
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
@Primary
@Bean(name = "crk")
@ConfigurationProperties(prefix = "spring.k")
public DataSource crk() {
ate().build();
}
/**
* 动态数据源配置
*
* @return
*/
@Bean
public DataSource multipleDataSource(@Qualifier("crk") DataSource crk,
@Qualifier("yw") DataSource yw) {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
Map targetDataSources = new HashMap<>();
targetDataSources.k.getValue(), crk);
targetDataSources.put(Value(), yw);
dynamicDataSource.setTargetDataSources(targetDataSources);
dynamicDataSource.setDefaultTargetDataSource(crk);
return dynamicDataSource;
}
@Primary
@Bean("priSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("crk")DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(new
PathMatchingResourcePatternResolver().getResources("classpath:/mybatis/crk/*l")); MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(false);
sqlSessionFactory.setConfiguration(configuration);
sqlSessionFactory.setPlugins(paginationInterceptor());
Object();
}
这样主数据源就构建成功了
2.3配置从数据源
@EnableTransactionManagement
@Configuration
@MapperScan(basePackages = "vv.up.dao.yw",
sqlSessionFactoryRef = "ywSqlSessionFactory")
public class MybatisPlusConfig2 {
@Bean("secondInterceptor")
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
@Bean(name = "yw")
@ConfigurationProperties(prefix = "spring.datasource.druid.yw")
public DataSource yw() {
ate().build();
}
@Bean("ywSqlSessionFactory")
springboot aoppublic SqlSessionFactory ywSqlSessionFactory(@Qualifier("yw")DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(new
PathMatchingResourcePatternResolver().getResources("classpath:/mybatis/yw/*l")); Myb
atisConfiguration configuration = new MybatisConfiguration();
configuration.setJdbcTypeForNull(JdbcType.NULL);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。