目录
- 1.TableScopeASPectAjk
- 2.TableScopeAjk注解
- 3.数据库表配置
- 4.业务切入点
- 5.sql动态配置
- 总结
1.TableScopeAspectAjk
package com.ruoyi.framework.aspectj;
import com.ruoyi.common.annotation.BranchScopeWjlz;
import com.ruoyi.common.annotation.TableScopeAjk;
import com.ruoyi.common.config.UploadConfig;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.entity.BaseSJFZEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.ApphpplicationContextUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import org.aspectjandroid.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Po编程intcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import Java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 2025-07-25 15:22:00
* 动态切换数据表
*
* @author wangwei
*/
@Aspect
@Component
@Service
public class TableScopeAspectAjk {
public static final String DATA_SCOPE = "tableScopeAjk";
// 配置织入点-登录用户的所属组织
@Pointcut("@annotation(com.ruoyi.common.annotation.TableScopeAjk)")
public void branchScopePointCutandroid() {
}
//登录用户的所属组织
@Before("branchScopePointCut()")
public void doBefore(JoinPoint point) throws Throwable {
handleDataScope(point);
}
/***
* 当前登录组织
* @param joinPoint
*/
protected void handleDataScope(final JoinPoint joinPoint) {
// 获得注解
TableScopeAjk controllerBranchScope = getAnnotationLog(joinPoint);
if (controllerBranchScope == null) {
return;
}
branchScopeFilter(joinPoint);
}
/**
* 数据范围过滤-当前用户所属组织
*
* @param joinPoint 切点
*/
public static void branchScopeFilter(JoinPoint joinPoint) {
SysConfigMapper sysConfigMapper = ApplicationCont编程客栈extUtils.getBean(SysConfigMapper.class);
String tableName = "";
SysConfig sysConfig = new SysConfig();
sysConfig.setRemark("AJKTABLENAME");
List<SysConfig> sysConfigList = sysConfigMapper.selectConfigList(sysConfig);
if (sysConfigList.size() < 1) {
tableName = "默认配置表";
} else {
List<SysConfig> filteredList = sysConfigList.stream()
.filter(config -> "Y".equals(config.getConfigType()))
.collect(Collectors.toList());
if (filteredList.size() < 1) {
tableName = "默认配置表";
} else {
tableName = filteredList.get(0).getConfigType();
}
}
Object params = joinPoint.getArgs()[0];
if (StringUtils.isNotNull(params) && params instanceof BaseSJFZEntity) {
BaseSJFZEntity baseEntity = (BaseSJFZEntity) params;
baseEntity
.getParams()
.put(
DATA_SCOPE,
tableName
//TODO 动态获取 传参 baseEntity.getTABLENAME()
);
}
}
/**
* 是否存在注解,如果存在就获取
*/
private TableScopeAjk getAnnotationLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(TableScopeAjk.class);
}
return null;
}
}
2.TableScopeAjk注解
package com.ruoyi.common.annotation;
import java.lang.annotation.*;
/** 组织过滤注解 */
/***
*组织数据过滤
* 1.当前登录组织
* wangwei
* 2023-11-07 09:08:00
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TableScopeAjk {
/**
* 案件表 名称
*/
public String TABLENAME() default "";
}
3.数据库表配置

4.业务切入点
- Mapper.java
@TableScopeAjk public List<Tb....> selectTb....ByList(Tb.... tb....);
5.sql动态配置
${params.tableScopeAjk}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
加载中,请稍侯......
精彩评论