java dbtype_java 动态操作数据库
問(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)題。
- 上一篇: java 内部类 加载_java内部类及
- 下一篇: linux cmake编译源码,linu