springboot動(dòng)態(tài)切換數(shù)據(jù)庫(kù)表方式
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
* 動(dòng)態(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ù)范圍過(guò)濾-當(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 動(dòng)態(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.*;
/** 組織過(guò)濾注解 */
/***
*組織數(shù)據(jù)過(guò)濾
* 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ù)庫(kù)表配置

4.業(yè)務(wù)切入點(diǎn)
- Mapper.java
@TableScopeAjk public List<Tb....> selectTb....ByList(Tb.... tb....);
5.sql動(dòng)態(tài)配置
${params.tableScopeAjk}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot集成screw實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成
- springboot整合ACTable生成數(shù)據(jù)庫(kù)表方式
- SpringBoot中集成screw(螺絲釘)實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)文檔生成方法
- SpringBoot結(jié)合Mybatis實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫(kù)表的方法
- Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫(kù)表
- springBoot下實(shí)現(xiàn)java自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)表
- springboot+mybatis通過(guò)實(shí)體類自動(dòng)生成數(shù)據(jù)庫(kù)表的方法
相關(guān)文章
java spring整合junit操作(有詳細(xì)的分析過(guò)程)
這篇文章主要介紹了java spring整合junit操作(有詳細(xì)的分析過(guò)程),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
MyEclipse開發(fā)一個(gè)webservice接口
這篇文章主要為大家詳細(xì)介紹了MyEclipse開發(fā)一個(gè)webservice接口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Java微服務(wù)Filter過(guò)濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過(guò)程詳解
這篇文章主要介紹了Java微服務(wù)Filter過(guò)濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過(guò)程,首先Sentinel規(guī)則的存儲(chǔ)默認(rèn)是存儲(chǔ)在內(nèi)存的,應(yīng)用重啟之后規(guī)則會(huì)丟失。因此我們通過(guò)配置中心Nacos保存規(guī)則,然后通過(guò)定時(shí)拉取Nacos數(shù)據(jù)來(lái)獲取規(guī)則配置,可以做到動(dòng)態(tài)實(shí)時(shí)的刷新規(guī)則2023-02-02
mybatis多對(duì)多查詢的實(shí)現(xiàn)(xml方式和注解方式)
本文主要介紹了mybatis多對(duì)多查詢的實(shí)現(xiàn),有xml方式和注解方式兩種,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
Java中@DateTimeFormat @JsonFormat失效原因及測(cè)試填坑
本文主要介紹了Java中@DateTimeFormat @JsonFormat失效原因及測(cè)試填坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
部署springboot項(xiàng)目到云服務(wù)器的兩種方式(jar+war)
本文主要介紹了部署springboot項(xiàng)目到云服務(wù)器的兩種方式,主要介紹了jar和war兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
實(shí)戰(zhàn)SpringBoot集成JWT實(shí)現(xiàn)token驗(yàn)證
本文詳細(xì)講解了SpringBoot集成JWT實(shí)現(xiàn)token驗(yàn)證,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12

