Skip to content

Commit d640762

Browse files
committed
refactor: 修改数据源配置文件模板,优化数据源配置类
1 parent 2aca778 commit d640762

2 files changed

Lines changed: 54 additions & 32 deletions

File tree

simple-common/src/main/java/top/cadecode/common/config/DataSourceConfig.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.springframework.context.annotation.Configuration;
1010
import org.springframework.core.env.Environment;
1111
import org.springframework.core.io.ClassPathResource;
12+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
13+
import org.springframework.transaction.PlatformTransactionManager;
1214
import top.cadecode.common.datasource.DynamicDataSource;
1315
import top.cadecode.common.datasource.DynamicDataSource.DynamicDataSourceHolder;
1416

@@ -48,16 +50,12 @@ public DynamicDataSource dynamicDataSource() {
4850
Map<String, Object> configMap = this.checkDataSourceMap(dataSourceMap, key);
4951
// 取出数据源类型和 isDefault
5052
String type = (String) configMap.get("type");
51-
Boolean isDefault = (Boolean) configMap.get("isDefault");
52-
// 生成数据源实例
53-
DataSource dataSource = (DataSource) Binder.get(environment)
54-
.bind(key + ".config", this.checkDataSourceType(type, key)).get();
53+
// 配置数据源
54+
DataSource dataSource = this.setTargetDataSourceMap(key, type);
5555
targetDataSourceMap.put(key, dataSource);
5656
// 判断是否是默认数据源
57-
if (isDefault != null && isDefault.equals(true) && !dynamicDataSource.hasDefaultDataSource()) {
58-
dynamicDataSource.setDefaultTargetDataSource(dataSource);
59-
log.info("设置默认数据源为 " + key);
60-
}
57+
Boolean isDefault = (Boolean) configMap.get("isDefault");
58+
this.setDefaultDataSource(dynamicDataSource, isDefault, dataSource, key);
6159
});
6260
// 判断是否设置默认数据源
6361
if (!dynamicDataSource.hasDefaultDataSource()) {
@@ -68,6 +66,12 @@ public DynamicDataSource dynamicDataSource() {
6866
return dynamicDataSource;
6967
}
7068

69+
@Bean
70+
public PlatformTransactionManager transactionManager() {
71+
return new DataSourceTransactionManager(dynamicDataSource());
72+
}
73+
74+
7175
/**
7276
* 校验 dataSourceMap
7377
*/
@@ -79,19 +83,38 @@ private Map<String, Object> checkDataSourceMap(Map<String, Object> dataSourceMap
7983
}
8084

8185
/**
82-
* 获取数据源 class
86+
* 设置数据源
8387
*/
84-
private Class<?> checkDataSourceType(String type, String key) {
85-
Class<?> dataSourceClass;
88+
private DataSource setTargetDataSourceMap(String key, String type) {
89+
Class<DataSource> dataSourceClass;
8690
try {
87-
dataSourceClass = Class.forName(type);
91+
dataSourceClass = (Class<DataSource>) Class.forName(type);
8892
// 判断是否是 DataSource 类型
8993
if (!DataSource.class.isAssignableFrom(dataSourceClass)) {
9094
throw new IllegalArgumentException("数据源 " + key + " 的类型 " + type + " 不合适");
9195
}
96+
// 生成数据源实例
97+
DataSource dataSource = (DataSource) Binder.get(environment)
98+
.bind(key + ".config", dataSourceClass).get();
99+
return dataSource;
92100
} catch (ClassNotFoundException e) {
93101
throw new IllegalArgumentException("数据源 " + key + " 的类型 " + type + " 不存在");
102+
} catch (Exception e) {
103+
throw new IllegalArgumentException("创建数据源 " + key + " 时出现绑定错误");
104+
}
105+
}
106+
107+
/**
108+
* 配置默认数据源
109+
*/
110+
private void setDefaultDataSource(DynamicDataSource dynamicDataSource, Boolean isDefault,
111+
DataSource dataSource, String key) {
112+
if (dynamicDataSource.hasDefaultDataSource()) {
113+
return;
114+
}
115+
if (isDefault != null && isDefault.equals(true)) {
116+
dynamicDataSource.setDefaultTargetDataSource(dataSource);
117+
log.info("设置默认数据源为 " + key);
94118
}
95-
return dataSourceClass;
96119
}
97120
}
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
# 配置数据源
2-
dataSource:
3-
db1:
4-
# 指定 dataSource 种类
5-
type: com.zaxxer.hikari.HikariDataSource
6-
# 指定连接配置
7-
config:
8-
jdbc-url: jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
9-
driver-class-name: com.mysql.cj.jdbc.Driver
10-
username: root
11-
password: xxxx
12-
db2:
13-
type: com.zaxxer.hikari.HikariDataSource
14-
config:
15-
jdbc-url: jdbc:mysql://localhost:3306/demo_cluster?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
16-
driver-class-name: com.mysql.cj.jdbc.Driver
17-
username: root
18-
password: xxxx
19-
# 默认数据源
20-
default: db2
2+
db1:
3+
# 指定 dataSource 种类
4+
type: com.zaxxer.hikari.HikariDataSource
5+
# 指定连接配置
6+
isDefault: true
7+
config:
8+
jdbc-url: jdbc:mysql://localhost:3306/demo?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
9+
driver-class-name: com.mysql.cj.jdbc.Driver
10+
username: root
11+
password: xxxx
12+
db2:
13+
type: com.zaxxer.hikari.HikariDataSource
14+
# 指定是否为默认数据源
15+
config:
16+
jdbcUrl: jdbc:mysql://localhost:3306/demo_cluster?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
17+
driverClassName: com.mysql.cj.jdbc.Driver
18+
username: root
19+
password: xxxx

0 commit comments

Comments
 (0)