來源:dewffgqd 發(fā)布時(shí)間:2018-11-14 11:21:12 閱讀量:1000
項(xiàng)目結(jié)構(gòu):
配置文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">
<!-- 數(shù)據(jù)庫配置文件位置 -->
<context:property-placeholder location="classpath:/jdbc.properties" />
<context:component-scan base-package="com.cn" />
<!-- 配置c3p0數(shù)據(jù)源 -->
<bean id="parentDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxStatements" value="${c3p0.maxStatements}" />
<property name="testConnectionOnCheckin" value="false" />
<property name="acquireRetryAttempts" value="5" />
<property name="acquireRetryDelay" value="2000" />
<property name="breakAfterAcquireFailure" value="false" />
</bean>
<bean id="localDataSource" parent="parentDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
</bean>
<bean id="cnccDataSource" parent="parentDataSource">
<property name="jdbcUrl" value="${jdbc.url1}"></property>
</bean>
<bean id="dataSource" class="com.cn.dynamicDS.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="localDataSource" key="local"></entry>
<entry value-ref="cnccDataSource" key="cncc"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="cnccDataSource"></property>
</bean>
<!-- 使用JDBC事物 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用annotation注解方式配置事務(wù) -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
</beans>
jdbc.properties:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:實(shí)例名
jdbc.username=用戶名
jdbc.password=密碼
jdbc.url1=jdbc:oracle:thin:@192.168.42.147:1521:第二個(gè)數(shù)據(jù)庫實(shí)例名
#也可配置其他數(shù)據(jù)庫
c3p0.initialPoolSize=10
c3p0.maxPoolSize=10
c3p0.minPoolSize=10
c3p0.maxStatements=10
常量類:
package com.cn.cncc.dynamicDS;
/**
* 常量類
* @author hjr
*
*/
public class DataSourceConst {
/*
* 操作本地?cái)?shù)據(jù)庫
*/
public static final String LOCAL = "local";
/*
* 操作BMC遠(yuǎn)端數(shù)據(jù)庫
*/
public static final String CNCC = "cncc";
}
動態(tài)數(shù)據(jù)源:
package com.cn.cncc.dynamicDS;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 動態(tài)數(shù)據(jù)源
* @author hjr
*
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return DataSourceContextHolder.getDataSourceType();
}
}
數(shù)據(jù)源動態(tài)切換方法類:
package com.cn.cncc.dynamicDS;
/**
* 數(shù)據(jù)源切換方法類
* @author hjr
*
*/
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();//線程本地環(huán)境
//設(shè)置數(shù)據(jù)源類型
public static void setDataSourceType(String dataSourceType)
{
contextHolder.set(dataSourceType);
}
//獲取數(shù)據(jù)源類型
public static String getDataSourceType()
{
return contextHolder.get();
}
//清除數(shù)據(jù)源類型
public static void clearDataSourceType()
{
contextHolder.remove();
}
}
測試類:
package test;
import java.io.BufferedReader;
import java.math.BigDecimal;
import java.sql.Clob;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cn.cncc.dynamicDS.DataSourceConst;
import com.cn.cncc.dynamicDS.DataSourceContextHolder;
import com.cn.cncc.service.CpuService;
public class dyanmicDataSourceTest {
Logger log = Logger.getLogger(dyanmicDataSourceTest.class);
private CpuService cpuServiceImpl;
@Before
public void before(){
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-servlet.xml","classpath:applicationContext.xml"});
cpuServiceImpl = (CpuService)context.getBean("cpuServiceImpl");
}
@Test
public void cpuTest(){
String dataSourceType = DataSourceContextHolder.getDataSourceType();//當(dāng)前數(shù)據(jù)庫
System.out.println(dataSourceType);
List<Object> findcpu = cpuServiceImpl.findCpuTop5();
if(null != findcpu && findcpu.size()>0){
try {
DataSourceContextHolder.setDataSourceType(DataSourceConst.LOCAL);//切換數(shù)據(jù)庫
int insertcpu = cpuServiceImpl.insertCpuTop5(findcpu);//在另一個(gè)數(shù)據(jù)庫查詢
String dataSourceType2 = DataSourceContextHolder.getDataSourceType();
System.out.println(dataSourceType2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
---------------------
作者:dewffgqd
來源:CSDN
原文:https://blog.csdn.net/dewffgqd/article/details/68060978
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!