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;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
ssm에서 ansible 해 본 메모 (그냥 메모)SSM EC2에 SSM 에이전트를 넣고 EC2를 SSM의 관리하에 놓습니다. SSM 관리형 인스턴스에서 볼 수 있다면 OK 참고 링크 S3 버킷 생성 Ansible 템플릿을 zip으로 만들어 S3에 배치하기 위해. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.