java 動態操作資料庫

來源:互聯網
上載者:User

標籤:步驟   hold   targe   margin   img   現在   int   設定檔   type   

問題描述:比如項目現在要使用在南京的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 動態操作資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.