最近项目中需要用到两个数据库,找了不少资料,都没有很好的解决方案,于是自己动手搭了个架子。相信今后还是用得到的,在此做个记号。
1、配置两个数据源,
a、dataSourceOne b、dataSourceTwo
spring-datasource.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byName">
<!-- 数据库1 -->
<bean id="dataSourceOne" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:orcl" />
<property name="username" value="admin1" />
<property name="password" value="111111" />
<property name="initialSize">
<value>3</value>
</property>
<property name="maxActive">
<value>30</value>
</property>
<property name="maxIdle">
<value>20</value>
</property>
<property name="maxWait">
<value>6000</value>
</property>
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<property name="logAbandoned">
<value>true</value>
</property>
</bean>
<!-- 数据库2 -->
<bean id="dataSourceTwo" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.1.20:1521:orcl" />
<property name="username" value="admin2" />
<property name="password" value="222222" />
<property name="initialSize">
<value>3</value>
</property>
<property name="maxActive">
<value>30</value>
</property>
<property name="maxIdle">
<value>20</value>
</property>
<property name="maxWait">
<value>6000</value>
</property>
<property name="removeAbandoned">
<value>true</value>
</property>
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<property name="logAbandoned">
<value>true</value>
</property>
</bean>
<!--动态选择数据源 编写spring 配置文件的配置多数源映射关系 -->
<bean class="com.sendinfo.xspring.util.DynamicDataSource" id="dataSource">
<property name="targetDataSources" >
<map key-type="java.lang.String">
<entry value-ref="dataSourceOne" key="1"></entry>
<entry value-ref="dataSourceTwo" key="2"></entry>
</map>
</property>
<!--默认连接的数据源-->
<property name="defaultTargetDataSource" ref="dataSourceOne"></property>
</bean>
</beans>
2、配置事物管理器
spring-manager.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd "
default-autowire="byName">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3、java的dao层访问技巧
/**
* 角色
* @author liang
*
*/
@Repository
public class RoleDao{
/**
* 查询角色列表
* @param queryMap
* @return
*/
public List<Role> getRoles(Map<String,Object> queryMap){
DbContextHolder.setDbType("2");//连接数据源databaseTwo
List<Role> roleList = getObjList(NAMESPACE_ROLE,queryMap,"ROLE");
DbContextHolder.clearDbtype();//清空本次连接数据源,还原为默认的databaseOne
return roleList;
}