reactor:@TenantIgnore 添加在 DO 实体类上,该表自动忽略租户
This commit is contained in:
@@ -13,10 +13,11 @@ import java.lang.annotation.*;
|
||||
*
|
||||
* 特殊:
|
||||
* 1、如果添加到 Controller 类上,则该 URL 自动添加到 {@link TenantProperties#getIgnoreUrls()} 中
|
||||
* 2、如果添加到 DO 实体类上,则它对应的表名“相当于”自动添加到 {@link TenantProperties#getIgnoreTables()} 中
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Target({ElementType.METHOD})
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
public @interface TenantIgnore {
|
||||
|
@@ -1,15 +1,17 @@
|
||||
package cn.iocoder.yudao.framework.tenant.core.db;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.tenant.config.TenantProperties;
|
||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.SqlParserUtils;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 基于 MyBatis Plus 多租户的功能,实现 DB 层面的多租户的功能
|
||||
@@ -18,16 +20,21 @@ import java.util.Set;
|
||||
*/
|
||||
public class TenantDatabaseInterceptor implements TenantLineHandler {
|
||||
|
||||
private final Set<String> ignoreTables = new HashSet<>();
|
||||
/**
|
||||
* 忽略的表
|
||||
*
|
||||
* KEY:表名
|
||||
* VALUE:是否忽略
|
||||
*/
|
||||
private final Map<String, Boolean> ignoreTables = new HashMap<>();
|
||||
|
||||
public TenantDatabaseInterceptor(TenantProperties properties) {
|
||||
// 不同 DB 下,大小写的习惯不同,所以需要都添加进去
|
||||
properties.getIgnoreTables().forEach(table -> {
|
||||
ignoreTables.add(table.toLowerCase());
|
||||
ignoreTables.add(table.toUpperCase());
|
||||
addIgnoreTable(table, true);
|
||||
});
|
||||
// 在 OracleKeyGenerator 中,生成主键时,会查询这个表,查询这个表后,会自动拼接 TENANT_ID 导致报错
|
||||
ignoreTables.add("DUAL");
|
||||
addIgnoreTable("DUAL", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,8 +44,40 @@ public class TenantDatabaseInterceptor implements TenantLineHandler {
|
||||
|
||||
@Override
|
||||
public boolean ignoreTable(String tableName) {
|
||||
return TenantContextHolder.isIgnore() // 情况一,全局忽略多租户
|
||||
|| CollUtil.contains(ignoreTables, SqlParserUtils.removeWrapperSymbol(tableName)); // 情况二,忽略多租户的表
|
||||
// 情况一,全局忽略多租户
|
||||
if (TenantContextHolder.isIgnore()) {
|
||||
return true;
|
||||
}
|
||||
// 情况二,忽略多租户的表
|
||||
tableName = SqlParserUtils.removeWrapperSymbol(tableName);
|
||||
Boolean ignore = ignoreTables.get(tableName.toLowerCase());
|
||||
if (ignore == null) {
|
||||
ignore = computeIgnoreTable(tableName);
|
||||
synchronized (ignoreTables) {
|
||||
addIgnoreTable(tableName, ignore);
|
||||
}
|
||||
}
|
||||
return ignore;
|
||||
}
|
||||
|
||||
private void addIgnoreTable(String tableName, boolean ignore) {
|
||||
ignoreTables.put(tableName.toLowerCase(), ignore);
|
||||
ignoreTables.put(tableName.toUpperCase(), ignore);
|
||||
}
|
||||
|
||||
private boolean computeIgnoreTable(String tableName) {
|
||||
// 找不到的表,说明不是 yudao 项目里的,不进行拦截
|
||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(tableName);
|
||||
if (tableInfo == null) {
|
||||
return true;
|
||||
}
|
||||
// 如果继承了 TenantBaseDO 基类,显然不忽略租户
|
||||
if (TenantBaseDO.class.isAssignableFrom(tableInfo.getEntityType())) {
|
||||
return true;
|
||||
}
|
||||
// 如果添加了 @TenantIgnore 注解,显然也不忽略租户
|
||||
TenantIgnore tenantIgnore = tableInfo.getEntityType().getAnnotation(TenantIgnore.class);
|
||||
return tenantIgnore != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user