1. 程式人生 > >spring+ibatis 雙資料來源配置

spring+ibatis 雙資料來源配置

背景:
鑑於兩個資料來源彼此獨立,沒有動態切換庫的現象,所以一般不存在跨資料來源的事務問題,此處只是簡單的複製一份新的資料來源配置,dao層應用新的SqlMapClientTemplate,為了結構清晰,兩個資料來源的SqlMap配置和dao配置獨立開來,具體程式碼如下:

applicationContext.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"> </property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="initialSize" value="${pool.initialSize}"></property> <property name="maxActive" value="${pool.maxActive}"></property> <property name="maxIdle" value="${pool.maxIdle}"></property> <property name="maxWait" value="${pool.maxWait}"></property> </bean> <!-- 配置ibatis --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="configLocation"> <value>classpath:SqlMapConfig.xml</value> </property> </bean> <!-- 事務配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" rollback-for="com.maifanfan.common.ServiceException" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="com.maifanfan.common.ServiceException" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="com.maifanfan.common.ServiceException" /> <!-- <tx:method name="*" read-only="true" /> --> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.tom.hss.service.impl.*.*(..))"></aop:pointcut> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"></aop:advisor> </aop:config> <!-- <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* com.service.*.*(..))" advice-ref="txAdvice" /> </aop:config> --> <!-- jdbc連線 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!-- ibatis連線 --> <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <!-- 第二個資料來源配置,此處複製一份上面的配置,id標識為2--> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@10.7.70.216:1521:abpdb"></property> <property name="username" value="abpkftest"></property> <property name="password" value="abpkftest"></property> <property name="initialSize" value="${pool.initialSize}"></property> <property name="maxActive" value="${pool.maxActive}"></property> <property name="maxIdle" value="${pool.maxIdle}"></property> <property name="maxWait" value="${pool.maxWait}"></property> </bean> <bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource"> <ref local="dataSource2" /> </property> <property name="configLocation"> <value>classpath:SqlMapConfig2.xml</value> </property> </bean> <!-- ibatis連線 --> <bean id="sqlMapClientTemplate2" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient"> <ref bean="sqlMapClient2" /> </property> </bean> <!--第二份事務配置--> <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource2" /> </bean> <tx:advice id="txAdvice2" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" rollback-for="com.maifanfan.common.ServiceException" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="com.maifanfan.common.ServiceException" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="com.maifanfan.common.ServiceException" /> <!-- <tx:method name="*" read-only="true" /> --> </tx:attributes> </tx:advice> <!--aop--> <aop:config> <aop:pointcut id="allManagerMethod2" expression="execution(* com.tom.hss.service.impl.*.*(..))"></aop:pointcut> <aop:advisor advice-ref="txAdvice2" pointcut-ref="allManagerMethod2"></aop:advisor> </aop:config> </beans>

sqlMapConfig2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
        PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
        "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
    <settings useStatementNamespaces="true" />
    <sqlMap resource="com/tom/bean/sqlmap/TB_CUST_SqlMap.xml" />
    <sqlMap resource="com/tom/bean/sqlmap/TB_TASK_SqlMap.xml" />
</sqlMapConfig>

applicationContext_dao2.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"
xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--新增需求 處理結果查詢dao-->
<bean id="bssCustDAO" class="com.tom.dao.impl.BssCustDAOImpl">
         <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate2">
         </property>
</bean>
<bean id="bssTaskDAO" class="com.tom.dao.impl.BssTaskDAOImpl">
         <property name="sqlMapClientTemplate" ref="sqlMapClientTemplate2">
         </property>
</bean>
</beans>

說明:
在dao層,daoImpl除了實現dao介面(ibatis要求必須要有介面和實現類),還繼承了SqlMapClientDaoSupport,通過spring將SqlMapClientTemplate注入到SqlMapClientDaoSupport中,以便在dao中可以getSqlMapClientTemplate,獲取到這個Template從而可以query和execute。
這是ibatis與spring結合的一種做法。

後面針對更復雜的多資料來源動態切換和分散式事務問題,還有進一步的討論。

thanks