From a8394df1176a9444a5cf6f84dac535f842e8c1b5 Mon Sep 17 00:00:00 2001
From: xiejun <xiejun@vci-tech.com>
Date: 星期三, 08 一月 2025 18:36:21 +0800
Subject: [PATCH] 产品型号集成,类型转换,人员组织加日志,申请接口引用码段值校验
---
Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 118 insertions(+), 0 deletions(-)
diff --git a/Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java b/Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java
new file mode 100644
index 0000000..74c11bd
--- /dev/null
+++ b/Source/BladeX-Tool/blade-starter-tenant/src/main/java/org/springblade/core/tenant/dynamic/TenantDataSourceHolder.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the dreamlu.net developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: Chill 搴勯獮 (smallchill@163.com)
+ */
+package org.springblade.core.tenant.dynamic;
+
+import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
+import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
+import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
+import lombok.AllArgsConstructor;
+import org.springblade.core.cache.utils.CacheUtil;
+import org.springblade.core.tool.utils.StringUtil;
+import org.springframework.beans.BeanUtils;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import javax.sql.DataSource;
+import java.util.Set;
+
+import static org.springblade.core.tenant.constant.TenantBaseConstant.*;
+
+/**
+ * 绉熸埛鏁版嵁婧愭牳蹇冨鐞嗙被
+ *
+ * @author Chill
+ */
+@AllArgsConstructor
+public class TenantDataSourceHolder {
+
+ private final DataSource dataSource;
+ private final DataSourceCreator dataSourceCreator;
+ private final JdbcTemplate jdbcTemplate;
+
+ /**
+ * 鏁版嵁婧愮紦瀛樺鐞�
+ *
+ * @param tenantId 绉熸埛ID
+ */
+ public void handleDataSource(String tenantId) {
+ // 鑾峰彇鍌ㄥ瓨鐨勬暟鎹簮闆嗗悎
+ DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
+ Set<String> keys = ds.getCurrentDataSources().keySet();
+ // 閰嶇疆涓嶅瓨鍦ㄥ垯鍔ㄦ�佹坊鍔犳暟鎹簮锛屼互鎳掑姞杞界殑妯″紡瑙e喅鍒嗗竷寮忓満鏅殑閰嶇疆鍚屾
+ // 涓轰簡淇濊瘉鏁版嵁瀹屾暣鎬э紝閰嶇疆鍚庣敓鎴愭暟鎹簮缂撳瓨锛屽悗鍙颁究鏃犳硶淇敼鏇存崲鏁版嵁婧愶紝鑻ヤ竴瀹氳淇敼璇疯縼绉绘暟鎹悗閲嶅惎鏈嶅姟鎴栬嚜琛屼慨鏀瑰簳灞傞�昏緫
+ if (!keys.contains(tenantId)) {
+ TenantDataSource tenantDataSource = getDataSource(tenantId);
+ if (tenantDataSource != null) {
+ // 鍒涘缓鏁版嵁婧愰厤缃�
+ DataSourceProperty dataSourceProperty = new DataSourceProperty();
+ // 鎷疯礉鏁版嵁婧愰厤缃�
+ BeanUtils.copyProperties(tenantDataSource, dataSourceProperty);
+ // 鍏抽棴鎳掑姞杞�
+ dataSourceProperty.setLazy(Boolean.FALSE);
+ // 鍒涘缓鍔ㄦ�佹暟鎹簮
+ DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
+ // 娣诲姞鏈�鏂版暟鎹簮
+ ds.addDataSource(tenantId, dataSource);
+ }
+ }
+ }
+
+ /**
+ * 鍒ゆ柇绉熸埛鏄惁鏈夋暟鎹簮閰嶇疆
+ *
+ * @param tenantId 绉熸埛ID
+ */
+ private Boolean existDataSource(String tenantId) {
+ // 灏嗙鎴锋槸鍚﹂厤缃暟鎹簮杩涜缂撳瓨锛岃嫢閲嶆柊閰嶇疆浼氬皢姝ょ紦瀛樻竻绌哄苟鍦ㄤ笅娆¤姹傜殑鏃跺�欐噿鍔犺浇
+ // 鑻ョ鎴锋病鏈夐厤缃暟鎹簮鍒欎細鑷姩浣跨敤master鏁版嵁婧愶紝姝や妇鏄负浜嗛伩鍏嶅湪娌℃湁鏁版嵁搴撶殑鏃跺�欓绻佹煡璇㈠鑷寸紦瀛樺嚮绌�
+ Boolean exist = CacheUtil.get(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_EXIST_KEY, tenantId, Boolean.class, Boolean.FALSE);
+ if (exist == null) {
+ TenantDataSource tenantDataSource = jdbcTemplate.queryForObject(TENANT_DATASOURCE_EXIST_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper<>(TenantDataSource.class));
+ if (tenantDataSource != null && StringUtil.isNotBlank(tenantDataSource.getDatasourceId())) {
+ exist = Boolean.TRUE;
+ } else {
+ exist = Boolean.FALSE;
+ }
+ CacheUtil.put(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_EXIST_KEY, tenantId, exist, Boolean.FALSE);
+ }
+ return exist;
+ }
+
+ /**
+ * 鑾峰彇瀵瑰簲鐨勬暟鎹簮閰嶇疆
+ *
+ * @param tenantId 绉熸埛ID
+ */
+ private TenantDataSource getDataSource(String tenantId) {
+ // 涓嶅瓨鍦ㄧ鎴锋暟鎹簮鍒欒繑鍥炵┖锛岄槻姝㈢紦瀛樺嚮绌�
+ if (!existDataSource(tenantId)) {
+ return null;
+ }
+ // 鑾峰彇绉熸埛鏁版嵁婧愪俊鎭�
+ TenantDataSource tenantDataSource = CacheUtil.get(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_KEY, tenantId, TenantDataSource.class, Boolean.FALSE);
+ if (tenantDataSource == null) {
+ tenantDataSource = jdbcTemplate.queryForObject(TENANT_DATASOURCE_SINGLE_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper<>(TenantDataSource.class));
+ if (tenantDataSource != null && StringUtil.isNoneBlank(tenantDataSource.getTenantId(), tenantDataSource.getDriverClass(), tenantDataSource.getUrl(), tenantDataSource.getUsername(), tenantDataSource.getPassword())) {
+ CacheUtil.put(TENANT_DATASOURCE_CACHE, TENANT_DATASOURCE_KEY, tenantId, tenantDataSource, Boolean.FALSE);
+ } else {
+ tenantDataSource = null;
+ }
+ }
+ return tenantDataSource;
+ }
+
+}
--
Gitblit v1.9.3