forked from APIJSON/APIJSON-Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseUtil.java
54 lines (45 loc) · 1.55 KB
/
DataBaseUtil.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
package apijson.demo;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class DataBaseUtil {
/**
* 根据url获取库名
* @param url
* @return
*/
public static String getLibname(String url) {
Pattern p = Pattern.compile("jdbc:(?<db>\\w+):.*((//)|@)(?<host>.+):(?<port>\\d+)(/|(;DatabaseName=)|:)(?<dbName>\\w+)\\??.*");
Matcher m = p.matcher(url);
if(m.find()) {
return m.group("dbName");
}
return null;
}
/***
* primary: master
* strict: false
* @param datasource: 匹配不成功, 自动匹配默认数据库
* @return
*/
public static javax.sql.DataSource getDataSource(String datasource) {
try {
return DynamicDataSource.getDetail(datasource).getDataSource(); // 数据源
} catch (Exception e) {
throw new IllegalArgumentException("动态数据源配置错误 " + datasource);
}
}
public static String getDruidUrl(String datasource) {
return DynamicDataSource.getDetail(datasource).getUrl(); // 数据库连接url
}
public static String getDruidSchema(String datasource) {
return getLibname(DynamicDataSource.getDetail(datasource).getUrl()); // 数据库名;
}
public static String getDruidDBAccount(String datasource) {
return DynamicDataSource.getDetail(datasource).getDbAccount(); // 数据库用户名
}
public static String getDruidDBPassword(String datasource) {
return DynamicDataSource.getDetail(datasource).getDbPassword(); // 数据库密码
}
}