在实际的公司项目中,很可能会遇到一个问题就是,一个java项目,但是项目中涉及两个数据库,这两个数据库还在不同ip的机子上。
遇到这种情况的时候,我们有两个选择
1、不走sPRing的aop方式,直接去多做两个dataSource
2、用spring进行管理,灵活地进行数据源切换
现在就来对第2种方式进行笔记:
spring.xml配置文件:
1<?xml version="1.0" encoding="UTF-8"?>2<beansxmlns="http://www.springframework.org/schema/beans"3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"4xmlns:task="http://www.springframework.org/schema/task"xmlns:tx="http://www.springframework.org/schema/tx"5xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"6xsi:schemaLocation="7http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd8http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd9http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd10http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd11http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd12http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">1314<!--配置MySQL数据库参数-->15<context:property-placeholderlocation="mysql.properties"/>1617<!--多个数据源配置-->18<beanid="db1"class="org.apache.commons.dbcp.BasicDataSource"19destroy-method="close">20<!--连接数据库参数-->21<propertyname="driverClassName"value="${db1.driver}"/>22<propertyname="url"value="${db1.url}"/>23<propertyname="username"value="${db1.username}"/>24<propertyname="passWord"value="${db1.password}"/>2526<!--连接池参数-->27<propertyname="initialSize"value="10"/>28<propertyname="maxActive"value="500"/>29<propertyname="maxIdle"value="40"/>30<propertyname="minIdle"value="10"/>31</bean>3233<beanid="db2"class="org.apache.commons.dbcp.BasicDataSource"34destroy-method="close">35<!--连接数据库参数-->36<propertyname="driverClassName"value="${db2.driver}"/>37<propertyname="url"value="${db2.url}"/>38<propertyname="username"value="${db2.username}"/>39<propertyname="password"value="${db2.password}"/>4041<!--连接池参数-->42<propertyname="initialSize"value="10"/>43<propertyname="maxActive"value="500"/>44<propertyname="maxIdle"value="40"/>45<propertyname="minIdle"value="10"/>46</bean>4748<beanid="dataSource"class="com.ckd.datasource.mybatis.DynamicDataSource">49<propertyname="targetDataSources">50<mapkey-type="java.lang.String">51<entrykey="db1"value-ref="db1"/>52<entrykey="db2"value-ref="db2"/>53</map>54</property>55<propertyname="defaultTargetDataSource"ref="db2"/>56</bean>5758<!--mybatis配置-->59<beanid="sqlsessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">60<propertyname="dataSource"ref="dataSource"/>61<propertyname="configLocation"value="classpath:mybatis/mybatis-config.xml"/>62<propertyname="mapperLocations"value="classpath:mybatis/mapper/*.xml"/>63</bean>64<!--获取sqlSession-->65<beanid="sqlSession"class="org.mybatis.spring.SqlSessionTemplate">66<constructor-argindex="0">67<refbean="sqlSessionFactory"/>68</constructor-arg>69</bean>70<!--事务管理器配置,单数据源事务-->71<beanid="transactionManager"72class="org.springframework.jdbc.datasource.DataSourceTransactionManager">73<propertyname="dataSource"ref="dataSource"/>74</bean>7576</beans>
具体实现切换的操作类:
1/**2* @fileName DynamicDataSource.java3*@authorchenkaideng4* @date 2015年8月27日5* @describe 动态获取数据源6*/7publicclassDynamicDataSourceextendsAbstractRoutingDataSource{8@Override9protectedObject determineCurrentLookupKey() {10//TODO Auto-generated method stub11returnDataSourceContextHolder.getDbType();12}13}
/*** @fileName DataSourceContextHolder.java
*@authorchenkaideng
* @date 2015年8月27日
* @describe 数据源设值Holder类*/publicclassDataSourceContextHolder {privatestaticfinalThreadLocal<String> contextHolder =newThreadLocal<String>();publicstaticvoidsetDbType(String dbType) {
contextHolder.set(dbType);
}publicstaticString getDbType() {return((String) contextHolder.get());
}publicstaticvoidclearDbType() {
contextHolder.remove();
}
}
弄完以上的事情,剩下的事情就简单了
-》先是加载spring.xml文件applicationContext = new ClassPathXmlApplicationContext("spring.xml");
-》然后设置数据源DataSourceContextHolder.setDbType("db1");
-》接着从applicationContext 中获取sqlSession = (SqlSession) applicationContext.getBean("sqlSession");
-》最后就可以拿这个sqlSession去做增删改查的操作
注意:不用对这个sqlSession做close和comit的操作,因为都已经由spring自己管理了,不用手动做这些操作。