Spring的多数据源配置(Spring+iBATIS + Oracle环境下)


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

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

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

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

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

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

  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"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="  
  7.      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  9.      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
  10.   
  11.     <!-- 数据源1 -->  
  12.      <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">  
  13.         <property name="driverClassName">  
  14.             <value>Oracle.jdbc.driver.OracleDriver</value>  
  15.         </property>  
  16.         <property name="url">  
  17.             <value>jdbc:oracle:thin:@192.168.1.2:1522:test01</value>  
  18.         </property>  
  19.         <property name="username">  
  20.             <value>user01</value>  
  21.         </property>   
  22.         <property name="password">  
  23.             <value>psw01</value>  
  24.         </property>  
  25.         <property name="maxActive">  
  26.             <value>100</value>  
  27.         </property>  
  28.         <property name="maxIdle">  
  29.             <value>8</value>  
  30.         </property>  
  31.         <property name="minIdle">  
  32.             <value>1</value>  
  33.         </property>  
  34.     </bean>  
  35.   
  36.     <!-- 数据源2 -->  
  37.     <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">  
  38.         <property name="driverClassName">  
  39.             <value>oracle.jdbc.driver.OracleDriver</value>  
  40.         </property>  
  41.         <property name="url">  
  42.             <value>jdbc:oracle:thin:@192.169.1.3:1552:test02</value>  
  43.         </property>  
  44.         <property name="username">  
  45.             <value>user02</value>  
  46.         </property>   
  47.         <property name="password">  
  48.             <value>psw02</value>  
  49.         </property>  
  50.         <property name="maxActive">  
  51.             <value>100</value>  
  52.         </property>  
  53.         <property name="maxIdle">  
  54.             <value>8</value>  
  55.         </property>  
  56.         <property name="minIdle">  
  57.             <value>1</value>  
  58.         </property>  
  59.     </bean>  
  60.       
  61.     <!-- 事务1 -->  
  62.     <bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  63.         <property name="dataSource" ref="dataSource2" />  
  64.     </bean>  
  65.     <!-- 事务2 -->  
  66.     <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  67.         <property name="dataSource" ref="dataSource1" />  
  68.     </bean>  
  69.       
  70.     <!-- 事务拦截1 -->  
  71.     <bean id="transactionInterceptor1" class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  72.         <property name="transactionManager" ref="transactionManager1" />  
  73.         <property name="transactionAttributes">  
  74.         <props>  
  75.             <prop key="insert*">PROPAGATION_REQUIRED</prop>  
  76.             <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  77.             <prop key="update*">PROPAGATION_REQUIRED</prop>  
  78.             <prop key="do*">PROPAGATION_REQUIRED</prop>  
  79.         </props>  
  80.         </property>  
  81.     </bean>  
  82.     <!-- 事务拦截2 -->  
  83.     <bean id="transactionInterceptor2" class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  84.         <property name="transactionManager" ref="transactionManager2" />  
  85.         <property name="transactionAttributes">  
  86.         <props>  
  87.             <prop key="insert*">PROPAGATION_REQUIRED</prop>  
  88.             <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  89.             <prop key="update*">PROPAGATION_REQUIRED</prop>  
  90.             <prop key="do*">PROPAGATION_REQUIRED</prop>  
  91.         </props>  
  92.         </property>  
  93.     </bean>  
  94.       
  95.     <!--  管理你连接的地方-->  
  96.     <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  97.         <property name="beanNames">  
  98.         <value>*Service</value>  
  99.         </property>  
  100.         <property name="interceptorNames">  
  101.             <list>  
  102.             <value>transactionInterceptor1</value>  
  103.             <value>transactionInterceptor2</value>  
  104.             </list>  
  105.         </property>  
  106.     </bean>  
  107.       
  108.     <!-- ibatis的工厂数据源配置1 -->  
  109.     <bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  110.         <property name="configLocation" value="classpath:config/sql-map-config.xml" /><!--这里是ibatis的sqlMap文件集合 -->  
  111.         <property name="dataSource" ref="dataSource1" />  
  112.     </bean>  
  113.       
  114.     <!-- ibatis的工厂数据源配置2 -->  
  115.     <bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">  
  116.         <property name="configLocation" value="classpath:config/sql-map-config.xml" /><!--这里是ibatis的sqlMap文件集合 -->  
  117.         <property name="dataSource" ref="dataSource2" />  
  118.     </bean>  
  119.       
  120.       
  121.   
  122.       
  123.     <!-- ibatis抽象的Dao1 -->  
  124.     <bean id="baseIbatisDAO1" abstract="true">  
  125.         <property name="sqlMapClient">  
  126.             <ref local="sqlMapClient1" />  
  127.         </property>  
  128.     </bean>  
  129.       
  130.     <!-- ibatis抽象的Dao2 -->  
  131.     <bean id="baseIbatisDAO2" abstract="true">  
  132.         <property name="sqlMapClient">  
  133.             <ref local="sqlMapClient2" />  
  134.         </property>  
  135.     </bean>  
  136. </beans>  

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

DAO层:    

  1. <pre class="html" name="code">    <bean id="userDAO"  
  2.         class="org.xxx.dao.impl.UserDAOImpl"  
  3.         parent="baseIbatisDAO1">     
  4.     </bean>  
  5.       
  6.     <bean id="deptDAO"  
  7.         class="org.xxx.dao.impl.DeptDAOImpl"  
  8.         parent="baseIbatisDAO2">  
  9.     </bean>  

service层(业务层): 

  1. <bean id="userService"  
  2.     class="org.xxx.service.impl.UserServiceImpl"  
  3.     parent="userDAO">     
  4. </bean>  
  5.   
  6. <bean id="deptService"  
  7.     class="org.xxx.service.impl.DeptServiceImpl"  
  8.     parent="deptDAO">  
  9. </bean>  

相关内容