mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 17:38:48 +08:00
增加TenantIgnore注解,以增加忽略租户的另一种方式
This commit is contained in:
parent
f683ef00b8
commit
008033a77c
@ -0,0 +1,18 @@
|
|||||||
|
package org.dromara.common.core.annotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 租户忽略
|
||||||
|
*
|
||||||
|
* @author Colin
|
||||||
|
* @date 2025/09/01
|
||||||
|
*/
|
||||||
|
public @interface TenantIgnore {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否开启忽略租户,默认为 true 开启
|
||||||
|
* <p>
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则满足条件,进行租户的忽略
|
||||||
|
*/
|
||||||
|
boolean enable() default true;
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,17 +1,22 @@
|
|||||||
package org.dromara.common.tenant.handle;
|
package org.dromara.common.tenant.handle;
|
||||||
|
|
||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.annotation.AnnotationUtil;
|
||||||
|
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.plugins.handler.TenantLineHandler;
|
||||||
import lombok.AllArgsConstructor;
|
import com.baomidou.mybatisplus.extension.toolkit.SqlParserUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.sf.jsqlparser.expression.Expression;
|
import net.sf.jsqlparser.expression.Expression;
|
||||||
import net.sf.jsqlparser.expression.NullValue;
|
import net.sf.jsqlparser.expression.NullValue;
|
||||||
import net.sf.jsqlparser.expression.StringValue;
|
import net.sf.jsqlparser.expression.StringValue;
|
||||||
|
import org.dromara.common.core.annotation.TenantIgnore;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
import org.dromara.common.tenant.helper.TenantHelper;
|
import org.dromara.common.tenant.helper.TenantHelper;
|
||||||
import org.dromara.common.tenant.properties.TenantProperties;
|
import org.dromara.common.tenant.properties.TenantProperties;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义租户处理器
|
* 自定义租户处理器
|
||||||
@ -19,11 +24,25 @@ import java.util.List;
|
|||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@AllArgsConstructor
|
|
||||||
public class PlusTenantLineHandler implements TenantLineHandler {
|
public class PlusTenantLineHandler implements TenantLineHandler {
|
||||||
|
|
||||||
private final TenantProperties tenantProperties;
|
private final TenantProperties tenantProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 忽略的表
|
||||||
|
* <p>
|
||||||
|
* KEY:表名
|
||||||
|
* VALUE:是否忽略
|
||||||
|
*/
|
||||||
|
private final Map<String, Boolean> ignoreTables = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public PlusTenantLineHandler(TenantProperties tenantProperties) {
|
||||||
|
this.tenantProperties = tenantProperties;
|
||||||
|
// 添加忽略的表
|
||||||
|
Optional.ofNullable(tenantProperties.getExcludes()).ifPresent(tables ->
|
||||||
|
tables.forEach(table -> addIgnoreTable(table, true)));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Expression getTenantId() {
|
public Expression getTenantId() {
|
||||||
String tenantId = TenantHelper.getTenantId();
|
String tenantId = TenantHelper.getTenantId();
|
||||||
@ -39,18 +58,36 @@ public class PlusTenantLineHandler implements TenantLineHandler {
|
|||||||
public boolean ignoreTable(String tableName) {
|
public boolean ignoreTable(String tableName) {
|
||||||
String tenantId = TenantHelper.getTenantId();
|
String tenantId = TenantHelper.getTenantId();
|
||||||
// 判断是否有租户
|
// 判断是否有租户
|
||||||
|
Boolean ignore = false;
|
||||||
if (StringUtils.isNotBlank(tenantId)) {
|
if (StringUtils.isNotBlank(tenantId)) {
|
||||||
// 不需要过滤租户的表
|
// 不需要过滤租户的表
|
||||||
List<String> excludes = tenantProperties.getExcludes();
|
tableName = SqlParserUtils.removeWrapperSymbol(tableName);
|
||||||
// 非业务表
|
ignore = ignoreTables.get(tableName.toLowerCase());
|
||||||
List<String> tables = ListUtil.toList(
|
if (ignore == null) {
|
||||||
"gen_table",
|
ignore = computeIgnoreTable(tableName);
|
||||||
"gen_table_column"
|
}
|
||||||
);
|
|
||||||
tables.addAll(excludes);
|
|
||||||
return StringUtils.equalsAnyIgnoreCase(tableName, tables.toArray(new String[0]));
|
|
||||||
}
|
}
|
||||||
return true;
|
return ignore;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addIgnoreTable(String tableName, boolean ignore) {
|
||||||
|
ignoreTables.put(tableName.toLowerCase(), ignore);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean computeIgnoreTable(String tableName) {
|
||||||
|
boolean ignore;
|
||||||
|
TableInfo tableInfo = TableInfoHelper.getTableInfo(tableName);
|
||||||
|
if (tableInfo == null) {
|
||||||
|
//找不到的表,说明不是项目里的或未被mp管理,不进行拦截(忽略租户)
|
||||||
|
ignore = true;
|
||||||
|
} else {
|
||||||
|
// 如果添加了 @TenantIgnore 注解,看是否忽略租户
|
||||||
|
TenantIgnore tenantIgnore = AnnotationUtil.getAnnotation(tableInfo.getEntityType(), TenantIgnore.class);
|
||||||
|
ignore = tenantIgnore != null && tenantIgnore.enable();
|
||||||
|
}
|
||||||
|
// 添加到缓存
|
||||||
|
addIgnoreTable(tableName, ignore);
|
||||||
|
return ignore;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,14 +4,16 @@ import com.baomidou.mybatisplus.annotation.FieldStrategy;
|
|||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
|
||||||
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
|
||||||
import org.dromara.generator.constant.GenConstants;
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
import org.dromara.common.core.annotation.TenantIgnore;
|
||||||
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
|
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
||||||
|
import org.dromara.generator.constant.GenConstants;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,8 +25,10 @@ import java.util.List;
|
|||||||
@Data
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@TableName("gen_table")
|
@TableName("gen_table")
|
||||||
|
@TenantIgnore
|
||||||
public class GenTable extends BaseEntity {
|
public class GenTable extends BaseEntity {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 编号
|
* 编号
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -4,13 +4,13 @@ import com.baomidou.mybatisplus.annotation.FieldStrategy;
|
|||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
import org.apache.ibatis.type.JdbcType;
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.dromara.common.core.annotation.TenantIgnore;
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
|
import org.dromara.common.mybatis.core.domain.BaseEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代码生成业务字段表 gen_table_column
|
* 代码生成业务字段表 gen_table_column
|
||||||
@ -20,6 +20,7 @@ import jakarta.validation.constraints.NotBlank;
|
|||||||
@Data
|
@Data
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@TableName("gen_table_column")
|
@TableName("gen_table_column")
|
||||||
|
@TenantIgnore
|
||||||
public class GenTableColumn extends BaseEntity {
|
public class GenTableColumn extends BaseEntity {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user