SSM + Druid 동적 다 중 데이터 원본 전환 (실 행 됨)

8322 단어 SSM
장면
두 개의 데이터 베 이 스 는 추가 삭제 와 검 사 를 마 칠 때 두 개의 데이터 베 이 스 를 동시에 조작 한다.
이루어지다
1. 웹. xml 에 spring 설정 파일 설정

 
  contextConfigLocation
  classpath:spring.xml
 

2. spring. xml 에 속성 파일 도입

 
 

3. config. properties 열기
다른 정상 적 인 설정 을 제외 하고 데이터 베 이 스 를 연결 하 는 url 두 개 를 추가 합 니 다.
다음 과 같이 추가:
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1

sys_url=jdbc:mysql://   1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
sys_username=
sys_password=

demo_url=jdbc:mysql://   2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
demo_username=
demo_password=

4. databaseType. properties 열기
#sysDs
1 = sysDs
#demoDS
2 = demoDs

5. 다시 spring. xml 로 돌아 가기
기타 설정 은 정상 입 니 다. 여 기 는 생략 합 니 다.
다른 spring 프로필 가 져 오기

 

6. spring - database. xml 열기
동적 전환 데이터 원본 설정

  
        
            
                
                
            
        
        
        
    

이어서 두 개의 데이터 원본 을 설정 합 니 다.
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    
  
  
  
  
  
  
  
  
  
  
  
  
 
 
 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    
  
  
  
  
  
  
  
  
  
  
  
  
 

그리고 트 랜 잭 션 관리 설정
   
  
     
     
     
         
     

7. 새 DataSource Holder 클래스
새 datasource 패키지, 새 DataSourceHolder. java 패키지
package com.badao.datasource;

public class DataSourceHolder {
 //      
    private static final ThreadLocal dataSources = new ThreadLocal();
    //     
    public static void setDataSource(String customerType) {
        dataSources.set(customerType);
    }
    //     
    public static String getDataSource() {
        return (String) dataSources.get();
    }
    //     
    public static void clearDataSource() {
        dataSources.remove();
    }
}

8. 업무 실현
import com.badao.datasource.DataSourceHolder;


@Description("    ")
 @ResponseBody
 @RequestMapping(value = "/doSave")
 public Map doSave(TbMessage entity, String op, String base64Str, String[] itemIdStr) {
  Map jsonResult = null;
  String msg = "";
  try {
   if (base64Str.length()!= 0) {
    String result = ImageUtil.getInstance().baseUploadImg(
      Constants.UPLOAD_IMG_MESSAGE, base64Str);
    entity.setThemeImageUrl((String) result);
   }
   String tabid = tabid(ModelAndViewConstants.MESSAGE_SYS_ID);
   Date now = new Date();
   ShiroUser currentUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
   //   
   if (null == op || ModelAndViewConstants.OPERATION_VALUE_ADD.equals(op)) {
    entity.setStatus("0");
    entity.setRecordTime(now);
    entity.setUserId(currentUser.getUserId());
    this.service.insert(entity);
    DataSourceHolder.setDataSource("demoDs");//       
    this.service.insert(entity);
    DataSourceHolder.setDataSource("sysDs");//       
   } else {//   
    entity.setModifyDate(now);
    this.service.updateByPrimaryKey(entity);
    DataSourceHolder.setDataSource("demoDs");//       
    this.service.updateByPrimaryKey(entity);
    DataSourceHolder.setDataSource("sysDs");//       
   }
   Integer statusCode = 200;
   String Msg = "        ";
   jsonResult = JsonResult.jsonReturn(statusCode, Msg, tabid);
   return jsonResult;

  } catch (Exception e) {
   msg = "      ";
   jsonResult = JsonResult.jsonWsReturn(1, msg, e.getMessage());
   LogService.getInstance(this).error("        " + e.getMessage());
   String mg = "        :" + e.getMessage();
   jsonResult = JsonResult.jsonReturnErr(mg);
   return jsonResult;
  }

 }

좋은 웹페이지 즐겨찾기