mirror of
https://github.com/chillzhuang/blade-tool
synced 2024-12-23 01:29:29 +08:00
✨ 添加 TenantIdUtil 方便在非 web 场景使用
This commit is contained in:
parent
de41acdd51
commit
8286b9f6ea
@ -42,7 +42,7 @@ public class BladeTenantHandler implements TenantLineHandler {
|
||||
*/
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
return new StringValue(Func.toStr(SecureUtil.getTenantId(), TenantConstant.DEFAULT_TENANT_ID));
|
||||
return new StringValue(Func.toStr(TenantIdUtil.get(), properties.getDefaultTenantId()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,10 +65,10 @@ public class BladeTenantHandler implements TenantLineHandler {
|
||||
public boolean ignoreTable(String tableName) {
|
||||
return !(
|
||||
(
|
||||
(properties.getTables().size() > 0 && properties.getTables().contains(tableName))
|
||||
(!properties.getTables().isEmpty() && properties.getTables().contains(tableName))
|
||||
|| properties.getBladeTables().contains(tableName)
|
||||
)
|
||||
&& StringUtil.isNotBlank(SecureUtil.getTenantId())
|
||||
&& StringUtil.isNotBlank(TenantIdUtil.get())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +38,11 @@ public class BladeTenantProperties {
|
||||
*/
|
||||
private String column = "tenant_id";
|
||||
|
||||
/**
|
||||
* 默认的租户id,默认为:000000
|
||||
*/
|
||||
private String defaultTenantId = TenantConstant.DEFAULT_TENANT_ID;
|
||||
|
||||
/**
|
||||
* 多租户数据表
|
||||
*/
|
||||
|
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springblade.core.boot.tenant;
|
||||
|
||||
import org.springblade.core.secure.utils.SecureUtil;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* TenantId 工具
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class TenantIdUtil {
|
||||
private static final ThreadLocal<String> tl = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 获取租户id
|
||||
*
|
||||
* @return 租户id
|
||||
*/
|
||||
public static String get() {
|
||||
String tenantId = tl.get();
|
||||
if (tenantId != null) {
|
||||
return tenantId;
|
||||
}
|
||||
return SecureUtil.getTenantId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用租户 id 执行函数
|
||||
*
|
||||
* @param tenantId tenantId
|
||||
* @param supplier supplier
|
||||
* @param <R> 泛型
|
||||
* @return R 函数返回
|
||||
*/
|
||||
public static <R> R use(String tenantId, Supplier<R> supplier) {
|
||||
Assert.hasText(tenantId, "参数 tenantId 为空");
|
||||
tl.set(tenantId);
|
||||
try {
|
||||
return supplier.get();
|
||||
} finally {
|
||||
tl.remove();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user