日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

java dbtype_java 动态操作数据库

發(fā)布時(shí)間:2025/3/19 数据库 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java dbtype_java 动态操作数据库 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問(wèn)題描述:比如項(xiàng)目現(xiàn)在要使用在南京的8的區(qū),這時(shí)這8個(gè)區(qū)分別建了一個(gè)數(shù)據(jù)庫(kù),但是只有一個(gè)項(xiàng)目,每個(gè)區(qū)的用戶都使用這個(gè)項(xiàng)目進(jìn)行登錄

問(wèn)題難點(diǎn):如何驗(yàn)證登錄人屬于哪個(gè)區(qū),然后確認(rèn)之后,如何進(jìn)行數(shù)據(jù)庫(kù)的切換;

問(wèn)題思路:除了8個(gè)數(shù)據(jù)庫(kù)之外,在建一個(gè)數(shù)據(jù)庫(kù):數(shù)據(jù)庫(kù)中包含的幾張表:儲(chǔ)存登錄用戶的信息等,直接上圖理解

一、數(shù)據(jù)庫(kù)的建立

h_right :

h_role:

紅色表示登錄人? 所屬哪一區(qū)

h_role_right:

分配顯示的菜單

h_role_sysuser:

用于分配區(qū)編號(hào)

h_sysuser:

用戶信息

以上所說(shuō)的就是獨(dú)立的一個(gè)數(shù)據(jù)庫(kù),加上8張之后就一共9個(gè)數(shù)據(jù)庫(kù)了,至此數(shù)據(jù)庫(kù)問(wèn)題就先這樣

二、java代碼如何實(shí)現(xiàn)

1、項(xiàng)目使用的是shrio 進(jìn)行安全處理

2、數(shù)據(jù)庫(kù)的配置文件的編輯

兩個(gè)配置文件:第一個(gè):專門用于所有數(shù)據(jù)庫(kù)的配置信息:

最重要的就是下面的數(shù)據(jù)庫(kù)Key值的配置

3、如何動(dòng)態(tài)切換數(shù)據(jù)庫(kù),JAVA中有提供這些接口的:

AbstractRoutingDataSource

這個(gè)時(shí)候要使用Spring的依賴注入和控制翻轉(zhuǎn)了

@Aspect

@Component

public class LogAop implements Ordered{

@Pointcut("execution(* com.tangbo..*(..))")

public void recordLog(){}

//@Pointcut("execution(* com.tangbo.esmsys..*.*(..))")

//定義在service包里的任意方法的執(zhí)行:

@Pointcut("execution(* com.tangbo.esmsys..*.*(..)) || execution(* com.tangbo.oprm.context..*.*(..)) || execution(* com.tangbo.oprm.institution..*.*(..))")

public void recordLog1(){}

@Pointcut("execution(* com.tangbo.oprm.right..*.*(..)) || execution(* com.tangbo.oprm.role..*.*(..)) || execution(* com.tangbo.oprm.sysuser..*.*(..)) ")

public void recordLogBySysUser(){}

@Before("recordLog1()")

public void before(JoinPoint call){

String className = call.getTarget().getClass().getName();

String methodName = call.getSignature().getName();

String sessionId = (String) SecurityUtils.getSubject().getSession().getId();

String dataSource = (String) SecurityUtils.getSubject().getSession().getAttribute(sessionId+"dataSource");

if(dataSource == null){

dataSource = (String) SecurityUtils.getSubject().getSession().getAttribute("datas");

}

DataSourceContextHolder.setDBType(dataSource);

String db =DataSourceContextHolder.getDBType();

System.out.println("開(kāi)始執(zhí)行:"+className+"."+methodName+"()方法..."+"選擇的數(shù)據(jù)庫(kù)為:"+ db);

}

@AfterThrowing("recordLog()")

public void afterThrowing(JoinPoint call){

String className = call.getTarget().getClass().getName();

String methodName = call.getSignature().getName();

System.out.println(className+"."+methodName+"()方法拋出了異常...");

}

@AfterReturning("recordLog()")

public void afterReturn(JoinPoint call){

String className = call.getTarget().getClass().getName();

String methodName = call.getSignature().getName();

System.out.println(className+"."+methodName+"()方法正常執(zhí)行結(jié)束...");

}

@After("recordLog()")

public void after(JoinPoint call){

String className = call.getTarget().getClass().getName();

String methodName = call.getSignature().getName();

DataSourceContextHolder.clearDBType();

System.out.println(className+"."+methodName+"()最終執(zhí)行步驟(finally)...");

}

@Before("recordLogBySysUser()")

public void beforeBySystem(JoinPoint call){

String className = call.getTarget().getClass().getName();

String methodName = call.getSignature().getName();

DataSourceContextHolder.setDBType("huawenchuan1");

String db =DataSourceContextHolder.getDBType();

System.out.println("開(kāi)始執(zhí)行:"+className+"."+methodName+"()方法..."+"選擇的數(shù)據(jù)庫(kù)為:"+ db);

}

@Override

public int getOrder() {

// TODO Auto-generated method stub

return 1;

}

}

解釋:時(shí)候Spring的注解,實(shí)現(xiàn)在登錄的時(shí)候使用哪個(gè)數(shù)據(jù)庫(kù),然后驗(yàn)證登錄人屬于哪個(gè)區(qū)之后,開(kāi)始在調(diào)用每個(gè)接口之前,設(shè)置好要使用的數(shù)據(jù)庫(kù)

總結(jié)

以上是生活随笔為你收集整理的java dbtype_java 动态操作数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。