在实际的公司项目中,很可能会遇到一个问题就是,一个java项目,但是项目中涉及两个数据库,这两个数据库还在不同ip的机子上。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd11 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd12 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 13 14 <!--配置MySQL数据库参数 -->15 <context:property-placeholder location="mysql.properties" />16 17 <!--多个数据源配置 -->18 <bean id="db1" class="org.apache.commons.dbcp.BasicDataSource"19 destroy-method="close">20 <!-- 连接数据库参数 -->21 <property name="driverClassName" value="${db1.driver}" />22 <property name="url" value="${db1.url}" />23 <property name="username" value="${db1.username}" />24 <property name="passWord" value="${db1.password}" />25 26 <!--连接池参数 -->27 <property name="initialSize" value="10" />28 <property name="maxActive" value="500" />29 <property name="maxIdle" value="40" />30 <property name="minIdle" value="10" />31 </bean>32 33 <bean id="db2" class="org.apache.commons.dbcp.BasicDataSource"34 destroy-method="close">35 <!-- 连接数据库参数 -->36 <property name="driverClassName" value="${db2.driver}" />37 <property name="url" value="${db2.url}" />38 <property name="username" value="${db2.username}" />39 <property name="password" value="${db2.password}" />40 41 <!--连接池参数 -->42 <property name="initialSize" value="10" />43 <property name="maxActive" value="500" />44 <property name="maxIdle" value="40" />45 <property name="minIdle" value="10" />46 </bean>47 48 <bean id="dataSource" class="com.ckd.datasource.mybatis.DynamicDataSource">49 <property name="targetDataSources">50 <map key-type="java.lang.String">51 <entry key="db1" value-ref="db1" />52 <entry key="db2" value-ref="db2" />53 </map>54 </property>55 <property name="defaultTargetDataSource" ref="db2" />56 </bean>57 58 <!-- mybatis配置 -->59 <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">60 <property name="dataSource" ref="dataSource" />61 <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />62 <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>63 </bean>64 <!--获取sqlSession-->65 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">66 <constructor-arg index="0">67 <ref bean="sqlSessionFactory"/>68 </constructor-arg>69 </bean> 70 <!-- 事务管理器配置,单数据源事务 -->71 <bean id="transactionManager"72 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">73 <property name="dataSource" ref="dataSource" />74 </bean>75 76 </beans>
具体实现切换的操作类:
1 /** 2 * @fileName DynamicDataSource.java 3 * @author chenkaideng 4 * @date 2015年8月27日 5 * @describe 动态获取数据源 6 */ 7 public class DynamicDataSource extends AbstractRoutingDataSource{ 8 @Override 9 protected Object determineCurrentLookupKey() {10 // TODO Auto-generated method stub11 return DataSourceContextHolder.getDbType();12 }13 }
/** * @fileName DataSourceContextHolder.java * @author chenkaideng * @date 2015年8月27日 * @describe 数据源设值Holder类 */public class DataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static void setDbType(String dbType) { contextHolder.set(dbType); } public static String getDbType() { return ((String) contextHolder.get()); } public static void clearDbType() { contextHolder.remove(); } }
新闻热点
疑难解答