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

在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> 

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2012-02/55524.htm

你可能感兴趣的:(Spring的多数据源配置(Spring+iBATIS + Oracle环境下))