Spring的多数据源配置(Spring+iBATIS + Oracle环境下)
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层:
- <pre class="html" name="code"> <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>
评论暂时关闭