java dbtype_java 动态操作数据库
問題描述:比如項目現在要使用在南京的8的區,這時這8個區分別建了一個數據庫,但是只有一個項目,每個區的用戶都使用這個項目進行登錄
問題難點:如何驗證登錄人屬于哪個區,然后確認之后,如何進行數據庫的切換;
問題思路:除了8個數據庫之外,在建一個數據庫:數據庫中包含的幾張表:儲存登錄用戶的信息等,直接上圖理解
一、數據庫的建立
h_right :
h_role:
紅色表示登錄人? 所屬哪一區
h_role_right:
分配顯示的菜單
h_role_sysuser:
用于分配區編號
h_sysuser:
用戶信息
以上所說的就是獨立的一個數據庫,加上8張之后就一共9個數據庫了,至此數據庫問題就先這樣
二、java代碼如何實現
1、項目使用的是shrio 進行安全處理
2、數據庫的配置文件的編輯
兩個配置文件:第一個:專門用于所有數據庫的配置信息:
最重要的就是下面的數據庫Key值的配置
3、如何動態切換數據庫,JAVA中有提供這些接口的:
AbstractRoutingDataSource
這個時候要使用Spring的依賴注入和控制翻轉了
@Aspect
@Component
public class LogAop implements Ordered{
@Pointcut("execution(* com.tangbo..*(..))")
public void recordLog(){}
//@Pointcut("execution(* com.tangbo.esmsys..*.*(..))")
//定義在service包里的任意方法的執行:
@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("開始執行:"+className+"."+methodName+"()方法..."+"選擇的數據庫為:"+ 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+"()方法正常執行結束...");
}
@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+"()最終執行步驟(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("開始執行:"+className+"."+methodName+"()方法..."+"選擇的數據庫為:"+ db);
}
@Override
public int getOrder() {
// TODO Auto-generated method stub
return 1;
}
}
解釋:時候Spring的注解,實現在登錄的時候使用哪個數據庫,然后驗證登錄人屬于哪個區之后,開始在調用每個接口之前,設置好要使用的數據庫
總結
以上是生活随笔為你收集整理的java dbtype_java 动态操作数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 内部类 加载_java内部类及
- 下一篇: java wcf 未提供用户名_WCF的