多租户配置排除表: 增加以注解方式排除

This commit is contained in:
gracecampo 2025-10-01 21:27:00 +08:00
parent 5ccb511064
commit 0234e63923
3 changed files with 45 additions and 2 deletions

View File

@ -0,0 +1,16 @@
package org.dromara.common.tenant.annotation;
import java.lang.annotation.*;
/**
* 忽略租户限制注解
* 用于标记实体类不需要进行租户数据隔离
* 限制: 仅允许标注在实体类上 mybatis-plus @TableName 标记的类
* @author 金鹏
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface IgnoreTenant {
}

View File

@ -83,4 +83,5 @@ public class TenantConfig {
return new TenantSaTokenDao();
}
}

View File

@ -1,6 +1,8 @@
package org.dromara.common.tenant.handle;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -8,6 +10,7 @@ import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.tenant.annotation.IgnoreTenant;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.common.tenant.properties.TenantProperties;
@ -48,9 +51,32 @@ public class PlusTenantLineHandler implements TenantLineHandler {
"gen_table_column"
);
tables.addAll(excludes);
return StringUtils.equalsAnyIgnoreCase(tableName, tables.toArray(new String[0]));
// 检查表是否通过注解标记为忽略租户
boolean ignoreByAnnotation = isIgnoreTenantByAnnotation(tableName);
return StringUtils.equalsAnyIgnoreCase(tableName, tables.toArray(new String[0]))|| ignoreByAnnotation;
}
return true;
}
/**
* 通过实体类注解判断是否忽略租户限制
* @param tableName 表名
* @return 是否忽略租户限制
*/
private boolean isIgnoreTenantByAnnotation(String tableName) {
try {
// 获取所有已注册的实体类
List<TableInfo> tableInfos = TableInfoHelper.getTableInfos();
for (TableInfo tableInfo : tableInfos) {
// 比较表名是否匹配
if (tableName.equalsIgnoreCase(tableInfo.getTableName())) {
Class<?> entityType = tableInfo.getEntityType();
// 检查实体类是否有 @IgnoreTenant 注解
return entityType.isAnnotationPresent(IgnoreTenant.class);
}
}
} catch (Exception e) {
log.warn("检查表{}是否忽略租户时发生异常", tableName, e);
}
return false;
}
}