直接上代码:
pom:
<!--JTA组件核心依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
config:
@Configuration
@MapperScan(basePackages = {"com.bs.dao.iadquery"},sqlSessionTemplateRef = "iadquerySqlSessionTemplate")
public class DruidIadqueryConfig {
@Autowired
private IadqueryConfig iadqueryConfig;
@Bean("dataSourceTwo")
public DataSource dataSourceOne () throws SQLException {
// 设置数据库连接
// MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
OracleXADataSource oracleXADataSource = new OracleXADataSource();
oracleXADataSource.setURL(iadqueryConfig.getUrl());
oracleXADataSource.setUser(iadqueryConfig.getUsername());
oracleXADataSource.setPassword(iadqueryConfig.getPassword());
oracleXADataSource.setDriverType(iadqueryConfig.getDriverClassName());
// 事务管理器
AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
atomikosDataSourceBean.setXaDataSource(oracleXADataSource);
atomikosDataSourceBean.setUniqueResourceName("dataSourceTwo");
return atomikosDataSourceBean;
}
@Bean(name = "sqlSessionFactoryTwo")
public SqlSessionFactory sqlSessionFactoryOne(
@Qualifier("dataSourceTwo") DataSource dataSourceOne) throws Exception{
// 配置Session工厂
// SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSourceOne);
return sessionFactory.getObject();
}
@Bean(name = "iadquerySqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("sqlSessionFactoryTwo") SqlSessionFactory sqlSessionFactory) {
// 配置Session模板
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = {"com.bs.dao.master"},sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class DruidMasterConfig {
@Autowired
private MasterConfig masterConfig ;
@Primary //
@Bean("dataSourceOne")
public DataSource dataSourceOne () throws SQLException {
// 设置数据库连接
// MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
OracleXADataSource oracleXADataSource = new OracleXADataSource();
oracleXADataSource.setURL(masterConfig.getUrl());
oracleXADataSource.setUser(masterConfig.getUsername());
oracleXADataSource.setPassword(masterConfig.getPassword());
oracleXADataSource.setDriverType(masterConfig.getDriverClassName());
// 事务管理器
AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
atomikosDataSourceBean.setXaDataSource(oracleXADataSource);
atomikosDataSourceBean.setUniqueResourceName("dataSourceOne");
return atomikosDataSourceBean;
}
@Primary
@Bean(name = "sqlSessionFactoryOne")
public SqlSessionFactory sqlSessionFactoryOne(
@Qualifier("dataSourceOne") DataSource dataSourceOne) throws Exception{
// 配置Session工厂
// SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSourceOne);
return sessionFactory.getObject();
}
@Primary
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("sqlSessionFactoryOne") SqlSessionFactory sqlSessionFactory) {
// 配置Session模板
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@Data
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.iadquery")
public class IadqueryConfig {
private String url;
private String username;
private String password;
private String driverClassName;
}
@Configuration
@Data
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
public class MasterConfig {
private String url;
private String username;
private String password;
private String driverClassName;
}
yml:
pring:
jta:
transaction-manager-id: jtaManager
main:
allow-bean-definition-overriding: true
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
type: com.alibaba.druid.pool.DruidDataSource
dynamic:
druid: #以下是全局默认值,可以全局更改
#监控统计拦截的filters
filters: stat,wall
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
datasource:
master:
url: jdbc:oracle:thin:@10.22.32.17:1521/midtdb
username: esi_comm
password: esi_test_2022
driverClassName: oracle.jdbc.driver.OracleDriver
iadquery:
url: jdbc:oracle:thin:@10.22.13.71:1521/yaetdb
username: iadsmpm_query
password: iadsmpm_query_test2022
driverClassName: oracle.jdbc.driver.OracleDriver