```
/**
* @author 張躍帥
* @Description: 數據源操作-工具類
* @date 2020/08/12
*/
public class DatasourceUtil {
/**
* 通用的數據源
*/
private static final DataSource commonDb = SpringUtil.getBean(DataSource.class);
/**
* 主數據源-客戶端
*/
private static final MasterDbClient masterDbClient = SpringUtil.getBean(MasterDbClient.class);
/**
* 獲取-當前使用中-數據源
*/
public static DataSource getCurrDb() {
// 動態路由數據源
DynamicRoutingDataSource commDb = (DynamicRoutingDataSource) commonDb;
// 獲取數據源
return commDb.determineDataSource();
}
/**
* 獲取-當前使用中-數據源名稱
*/
public static String getCurrDbName() {
// 獲取當前線程數據源名稱
return DynamicDataSourceContextHolder.peek();
}
/**
* 切換-主數據源
*/
public static void changeMasterDb() {
/**
* 強制清空本地線程
* 備注:當強制清空本地線程后,會自動使用默認數據源,默認數據源就是主庫
*/
DynamicDataSourceContextHolder.clear();
}
/**
* 切換-指定數據源
*/
public static void changeDb(String dbName) {
// 判斷
if (StrUtil.isNotBlank(dbName)) {
// 切換-目標數據源
DynamicDataSourceContextHolder.push(dbName);
}
}
/**
* 獲取-主數據源名稱
*/
public static String getMasterDbName() {
// 獲取主數據源URL
String masterDbUrl = masterDbClient.getUrl();
// 變量-數據源名稱
String dbName = null;
// 判斷
if (StrUtil.isNotBlank(masterDbUrl)) {
// 判斷-數據源類型
if (masterDbUrl.contains(DbTypeEnum.MYSQL.getCode())) {
// 定義正則表達式
Pattern pattern = Pattern.compile("jdbc:(?<db>\\w+):.*((//)|@)(?<host>.+):(?<port>\\d+)(/|(;DatabaseName=)|:)(?<dbName>\\w+)\\??.*");
// 匹配
Matcher matcher = pattern.matcher(masterDbUrl);
// 判斷
if (matcher.find()) {
// 獲取-數據源名稱
dbName = matcher.group("dbName");
}
}
}
// 返回
return dbName;
}
/**
* 獲取-指定的數據源
*/
public static DataSource getDbByDbName(String dbName) {
// 判斷
if (StrUtil.isNotBlank(dbName)) {
// 動態路由數據源
DynamicRoutingDataSource commDb = (DynamicRoutingDataSource) commonDb;
// 返回-數據源
return commDb.getDataSource(dbName);
}
// 返回
return null;
}
/**
* 獲取-所有的數據源名稱
*/
public static List<String> getAllDbName() {
// 變量創建ArrayList
List<String> dbNameList = CollectionUtil.newArrayList();
// 動態路由數據源
DynamicRoutingDataSource commDb = (DynamicRoutingDataSource) commonDb;
// 獲取Bean中所有數據源
Set<String> dbNameArray = commDb.getDataSources().keySet();
// 遍歷
for (String dbName : dbNameArray) {
// 獲取Bean中數據源名稱
String beanDbName = dbName.toString();
// 添加到List
dbNameList.add(beanDbName);
}
// 返回
return dbNameList;
}
}
- Jump簡介
- 技術架構
- 代碼規范
- 規范導讀
- JAVA規范
- 數據庫表設計規范
- 集成項目
- JDK1.8-pom.xml
- JDK21-pom.xml
- 項目結構
- 業務模塊-方法名稱規范
- 跨域配置
- License授權配置
- 公共字段自動填充
- 全局異常處理器
- PageOffice配置
- Beetl模板引擎配置
- application.properties
- application-prod.yml
- banner.txt
- logback-spring.xml
- jump-core (核心組件)
- Maven依賴
- 通用枚舉
- 公共數據狀態 - 枚舉
- 公共邏輯刪除 - 枚舉
- 公共操作編碼類型 - 枚舉
- 公共tree父節點 - 枚舉
- 公共是或否 - 枚舉
- 工具Util
- AopTargetUtil
- DownloadUtil
- GenerateNumUtil
- HttpServletUtil
- IpUtil
- JoinPointUtil
- MacUtil
- NetworkUtil
- ParamToUtil
- ResponseUtil
- TimeZoneDateUtil
- UaUtil
- 統一返回
- 結果對象
- 如何使用
- jump-cahche (緩存組件)
- Maven依賴
- Redis配置
- 緩存常量
- 工具Util
- RedisCacheUtil
- jump-idempotent (幕等組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- AOP參數
- AOP使用方法
- jump-lock (分布式鎖組件)
- Maven依賴
- 枚舉
- AOP參數
- AOP使用方法
- 工具Util
- RedissonLockUtil
- Util使用方法
- jump-mybatis (mybatis組件)
- Maven依賴
- 基礎Entity
- 枚舉
- 查詢類型 - 枚舉
- 條件查詢
- search
- service
- 分頁結果集
- Mapper
- MyMapper
- 使用方法
- DDL操作
- DML操作
- 工具Util
- EntityUtil
- PageUtil
- TableUtil
- jump-dynamic-datasource (多數據源組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- 工具Util
- DatasourceUtil
- 如何使用
- jump-satoken (satoken組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- Satoken配置信息
- SatokenUser信息
- Redis緩存操作
- SatokenRedisCache
- SatokenUserRedisCache
- 放行白名單
- jump-oss (OSS組件)
- Maven依賴
- 工具Util
- OssFileUtil
- OssPlatformUtil
- 如何使用
- jump-xss (XSS組件)
- Maven依賴
- 白名單放行
- jump-email (郵件組件)
- Maven依賴
- Email客戶端信息
- Email發送參數
- 工具Util
- jump-websocket (WebSocket組件)
- Maven依賴
- 消息對象
- 工具Util
- 如何使用
- jump-weixin (微信組件)
- Maven依賴
- jump-system (系統管理組件)
- Maven依賴
- AOP
- 系統操作日志AOP
- 系統數據日志AOP
- 系統操作權限AOP
- 字典轉文本AOP
- Redis緩存操作
- SystemConfigRedisCache
- 工具Util
- LoginUserUtil
- SystemAreaUtil
- SystemHomeUtil
- SystemMenuUtil
- SystemOrgAdminUtil
- SystemOrgTypeUtil
- SystemRoleUtil
- SystemUserLoginAreaUtil
- SystemUserUtil
- jump-timer(定時器組件)
- Maven依賴
- 枚舉
- Api接口
- 工具Util
- ActionClassUtil
- TimerTaskUtil
- 如何使用
- jump-ueditor (富文本組件)
- Maven依賴
- 如何使用
- 配置 ueditor.config.js
- 后端 application.properties
- 前端 vue3
- vue-codemirror (代碼編譯器)
- npm依賴
- PageOffice整合
- Maven依賴
- License授權配置
- 枚舉
- 文件來源 - 枚舉
- 預覽文件類型 - 枚舉
- 文件預覽參數
- 下載文件
- 預覽文件
- 工具Util