TenantContext.java
968 字节
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
package com.aigeo.common.context;
/**
* 租户上下文 - 多租户架构的核心组件
*
* @author AIGEO Team
* @since 1.0.0
*/
public class TenantContext {
private static final ThreadLocal<Integer> CURRENT_TENANT = new ThreadLocal<>();
/**
* 设置当前租户ID
*/
public static void setCurrentTenant(Integer tenantId) {
CURRENT_TENANT.set(tenantId);
}
/**
* 获取当前租户ID
*/
public static Integer getCurrentTenant() {
return CURRENT_TENANT.get();
}
/**
* 获取当前租户ID(别名方法)
*/
public static Integer getCurrentTenantId() {
return getCurrentTenant();
}
/**
* 检查是否有设置租户
*/
public static boolean hasTenant() {
return getCurrentTenant() != null;
}
/**
* 清除当前租户
*/
public static void clear() {
CURRENT_TENANT.remove();
}
}