spring的多数据源配置(spring+ibatis + oracle环境下)

电信的业务逻辑是复杂的,数据库的相互调用是不可避免。同一个应用项目中,可以调用DBLink来调用多个数据库,但一般只是配了一个数据源。

我的业务逻辑是这样的,有两个数据库,服务器端提供这两个数据库的webservice接口,前提是只做在一个java project。

OK! 那就是配置多个数据源了。搜索了一下,发现spring可以支持多个数据源。

有好几种方法,结合到我只需配置两个数据源,我选择了我认为最简单、最容易实现的方式。

下面就简单的介绍一下,我的那种方法。

即在spring的配置文件上,配置两个数据源、两个事务、两个事务拦截、两个ibatis的工厂数据源配置、两个ibatis的抽象Dao。代码如下:

<?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"
	xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<!-- 数据源1 -->
     <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@192.168.1.2:1522:test01</value>
		</property>
		<property name="username">
			<value>user01</value>
		</property> 
		<property name="password">
			<value>psw01</value>
		</property>
		<property name="maxActive">
			<value>100</value>
		</property>
		<property name="maxIdle">
			<value>8</value>
		</property>
		<property name="minIdle">
			<value>1</value>
		</property>
	</bean>

	<!-- 数据源2 -->
	<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@192.169.1.3:1552:test02</value>
		</property>
		<property name="username">
			<value>user02</value>
		</property> 
		<property name="password">
			<value>psw02</value>
		</property>
		<property name="maxActive">
			<value>100</value>
		</property>
		<property name="maxIdle">
			<value>8</value>
		</property>
		<property name="minIdle">
			<value>1</value>
		</property>
	</bean>
	
	<!-- 事务1 -->
	<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource2" />
	</bean>
	<!-- 事务2 -->
	<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource1" />
	</bean>
	
	<!-- 事务拦截1 -->
	<bean id="transactionInterceptor1" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager1" />
		<property name="transactionAttributes">
		<props>
			<prop key="insert*">PROPAGATION_REQUIRED</prop>
			<prop key="delete*">PROPAGATION_REQUIRED</prop>
			<prop key="update*">PROPAGATION_REQUIRED</prop>
			<prop key="do*">PROPAGATION_REQUIRED</prop>
		</props>
		</property>
	</bean>
	<!-- 事务拦截2 -->
	<bean id="transactionInterceptor2" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager2" />
		<property name="transactionAttributes">
		<props>
			<prop key="insert*">PROPAGATION_REQUIRED</prop>
			<prop key="delete*">PROPAGATION_REQUIRED</prop>
			<prop key="update*">PROPAGATION_REQUIRED</prop>
			<prop key="do*">PROPAGATION_REQUIRED</prop>
		</props>
		</property>
	</bean>
	
	<!--  管理你连接的地方-->
	<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
		<value>*Service</value>
		</property>
		<property name="interceptorNames">
			<list>
			<value>transactionInterceptor1</value>
			<value>transactionInterceptor2</value>
			</list>
		</property>
	</bean>
	
	<!-- ibatis的工厂数据源配置1 -->
	<bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="classpath:config/sql-map-config.xml" /><!--这里是ibatis的sqlMap文件集合 -->
		<property name="dataSource" ref="dataSource1" />
	</bean>
	
	<!-- ibatis的工厂数据源配置2 -->
	<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="classpath:config/sql-map-config.xml" /><!--这里是ibatis的sqlMap文件集合 -->
		<property name="dataSource" ref="dataSource2" />
	</bean>
	
	

	
	<!-- ibatis抽象的Dao1 -->
	<bean id="baseIbatisDAO1" abstract="true">
		<property name="sqlMapClient">
			<ref local="sqlMapClient1" />
		</property>
	</bean>
	
	<!-- ibatis抽象的Dao2 -->
	<bean id="baseIbatisDAO2" abstract="true">
		<property name="sqlMapClient">
			<ref local="sqlMapClient2" />
		</property>
	</bean>
</beans>

 

剩下的就是spring的DAO和 service层的调用和配置了。举个例子吧:

DAO层:    

	<bean id="userDAO"
		class="org.xxx.dao.impl.UserDAOImpl"
		parent="baseIbatisDAO1">   
	</bean>
	
	<bean id="deptDAO"
		class="org.xxx.dao.impl.DeptDAOImpl"
		parent="baseIbatisDAO2">
	</bean>
 

service层(业务层): 

	<bean id="userService"
		class="org.xxx.service.impl.UserServiceImpl"
		parent="userDAO">   
	</bean>
	
	<bean id="deptService"
		class="org.xxx.service.impl.DeptServiceImpl"
		parent="deptDAO">
	</bean>
 
呵呵。。。简单吧!愿能帮助到遇到类似问题的你。。。



  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值