springboot動態(tài)切換數(shù)據(jù)庫表方式
更新時間:2025年08月01日 09:11:02 作者:王大錘4391
本文分享了使用TableScopeAspectAjk切面和TableScopeAjk注解實(shí)現(xiàn)數(shù)據(jù)庫表配置、業(yè)務(wù)切入點(diǎn)管理及SQL動態(tài)配置的經(jīng)驗(yàn),旨在提升代碼可維護(hù)性與靈活性,適用于業(yè)務(wù)邏輯與數(shù)據(jù)層的分離開發(fā)
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.ApplicationContextUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
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
* 動態(tài)切換數(shù)據(jù)表
*
* @author wangwei
*/
@Aspect
@Component
@Service
public class TableScopeAspectAjk {
public static final String DATA_SCOPE = "tableScopeAjk";
// 配置織入點(diǎn)-登錄用戶的所屬組織
@Pointcut("@annotation(com.ruoyi.common.annotation.TableScopeAjk)")
public void branchScopePointCut() {
}
//登錄用戶的所屬組織
@Before("branchScopePointCut()")
public void doBefore(JoinPoint point) throws Throwable {
handleDataScope(point);
}
/***
* 當(dāng)前登錄組織
* @param joinPoint
*/
protected void handleDataScope(final JoinPoint joinPoint) {
// 獲得注解
TableScopeAjk controllerBranchScope = getAnnotationLog(joinPoint);
if (controllerBranchScope == null) {
return;
}
branchScopeFilter(joinPoint);
}
/**
* 數(shù)據(jù)范圍過濾-當(dāng)前用戶所屬組織
*
* @param joinPoint 切點(diǎn)
*/
public static void branchScopeFilter(JoinPoint joinPoint) {
SysConfigMapper sysConfigMapper = ApplicationContextUtils.getBean(SysConfigMapper.class);
String tableName = "";
SysConfig sysConfig = new SysConfig();
sysConfig.setRemark("AJKTABLENAME");
List<SysConfig> sysConfigList = sysConfigMapper.selectConfigList(sysConfig);
if (sysConfigList.size() < 1) {
tableName = "默認(rèn)配置表";
} else {
List<SysConfig> filteredList = sysConfigList.stream()
.filter(config -> "Y".equals(config.getConfigType()))
.collect(Collectors.toList());
if (filteredList.size() < 1) {
tableName = "默認(rèn)配置表";
} 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 動態(tài)獲取 傳參 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.*;
/** 組織過濾注解 */
/***
*組織數(shù)據(jù)過濾
* 1.當(dāng)前登錄組織
* wangwei
* 2023-11-07 09:08:00
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TableScopeAjk {
/**
* 案件表 名稱
*/
public String TABLENAME() default "";
}
3.數(shù)據(jù)庫表配置

4.業(yè)務(wù)切入點(diǎn)
- Mapper.java
@TableScopeAjk public List<Tb....> selectTb....ByList(Tb.... tb....);
5.sql動態(tài)配置
${params.tableScopeAjk}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot集成screw實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)文檔生成
- springboot整合ACTable生成數(shù)據(jù)庫表方式
- SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)文檔生成方法
- SpringBoot結(jié)合Mybatis實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫表的方法
- Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表
- springBoot下實(shí)現(xiàn)java自動創(chuàng)建數(shù)據(jù)庫表
- springboot+mybatis通過實(shí)體類自動生成數(shù)據(jù)庫表的方法
相關(guān)文章
完美解決gson將Integer默認(rèn)轉(zhuǎn)換成Double的問題
下面小編就為大家?guī)硪黄昝澜鉀Qgson將Integer默認(rèn)轉(zhuǎn)換成Double的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
SpringBatch從入門到精通之StepScope作用域和用法詳解
這篇文章主要介紹了SpringBatch從入門到精通之StepScope作用域和用法詳解,主要包括IOC容器中幾種bean的作用范圍以及可能遇到的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
Java中SimpleDateFormat 格式化日期的使用
本文主要介紹了Java中SimpleDateFormat 格式化日期的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
SpringBoot之返回json數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot之返回json數(shù)據(jù)的實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
用StopWatch優(yōu)雅替代currentTimeMillis計算程序執(zhí)行耗時
別再用System.currentTimeMillis()計算程序執(zhí)行耗時了,擁抱StopWatch優(yōu)雅來優(yōu)雅的計算,代碼更簡潔效率更高,本文帶你了解StopWatch的使用2021-09-09
使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決
這篇文章主要介紹了使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Springboot mybais配置多數(shù)據(jù)源過程解析
這篇文章主要介紹了Springboot+mybais配置多數(shù)據(jù)源過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03

