SpringBoot2 多数据源、多事务配置

添加事务支持

1
@EnableTransactionManagement

添加数据源配置

1
2
3
4
5
6
7
8
#master
spring.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/
spring.datasource.master.username=root
spring.datasource.master.password=root
#slave
spring.datasource.slave.jdbc-url=jdbc:mysql://localhost:3306/
spring.datasource.slave.username=root
spring.datasource.slave.password=root

加载配置

DataSourceConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Created by xiehui1956(@)gmail.com on 2020/4/26
*/
@Configuration
public class DataSourceConfig {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Primary
@Bean(name = "masterDataSource")
@Qualifier(value = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource getDefaultDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "slaveDataSource")
@Qualifier(value = "slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource getServiceClickHouseDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "masterJdbcTemplate")
public JdbcTemplate masterJdbcTemplate(@Qualifier(value = "masterDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

@Bean(name = "slaveJdbcTemplate")
public JdbcTemplate slaveJdbcTemplate(@Qualifier(value = "slaveDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

添加事务管理支持

DataSourceMasterManager.java
DataSourceSlaveManager.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Created by xiehui1956(@)gmail.com on 2020/4/26
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryMaster"
, transactionManagerRef = "transactionManagerMaster"
, basePackages = {"com.dao"})
public class DataSourceMasterManager {

@Autowired
@Qualifier("masterDataSource")
private DataSource masterDataSource;

@Primary
@Bean(name = "entityManagerFactoryMaster")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(masterDataSource)
.packages("com.entity")
.persistenceUnit("masterDataPersistenceUnit")
.build();
}

@Bean("transactionManagerMaster")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryBean(builder).getObject());
}
}

/**
* Created by xiehui1956(@)gmail.com on 2020/4/26
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySlave"
, transactionManagerRef = "transactionManagerSlave"
, basePackages = {"com.dto"})
public class DataSourceSlaveManager {

@Autowired
@Qualifier("slaveDataSource")
private DataSource slaveDataSource;

@Bean(name = "entityManagerFactorySlave")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(slaveDataSource)
.packages("com.deo")
.persistenceUnit("slaveDataPersistenceUnit")
.build();
}

@Bean("transactionManagerSlave")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryBean(builder).getObject());
}
}

使用

1
2
3
4
5
6
7
8
9
@Resource
@Qualifier(value = "masterJdbcTemplate")
private JdbcTemplate jdbcTemplate;

@GetMapping("/tt")
public CommonResult test(CommonResult commonResult) {
List<Map<String, Object>> show_tables = jdbcTemplate.queryForList("show tables");
return commonResult.setData(show_tables);
}

返回结果

图片