如何使用Spring解決ibatis多數(shù)據(jù)源的問題

本篇內(nèi)容介紹了“如何使用Spring解決ibatis多數(shù)據(jù)源的問題”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

為馬山等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及馬山網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、馬山網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

iBatis多數(shù)據(jù)源的苦惱

在僅使用ibatis時(shí),多數(shù)據(jù)源簡直就是夢魘,每多一個(gè)數(shù)據(jù)源就需要多一份sql-map-config配置文件。

采用spring的AbstractRoutingDataSource就可以簡單的解決這個(gè)問題。

AbstractRoutingDataSource實(shí)現(xiàn)了javax.sql.DataSource接口,因此可以理解為一個(gè)虛擬的動(dòng)態(tài)DataSource,在需要的時(shí)候根據(jù)上下文Context動(dòng)態(tài)決定使用哪個(gè)數(shù)據(jù)源。

Spring+iBatis多數(shù)據(jù)源的配置

下面是一個(gè)完整的配置:

< 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"   xmlns:jee="http://www.springframework.org/schema/jee"   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     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">    < !-- ========================= GENERAL DEFINITIONS ========================= -->  < jee:jndi-lookup id="ds0" jndi-name="jdbc/ds0"/>  < jee:jndi-lookup id="ds1" jndi-name="jdbc/ds1"/>  < jee:jndi-lookup id="ds2" jndi-name="jdbc/ds2"/>     < bean id="dataSource" class="com.xxx.xxx.util.DynamicDataSource">             < property name="targetDataSources">                < map key-type="java.lang.String">                   < entry key="0" value-ref="ds0"/>                < entry key="1" value-ref="ds1"/>                < entry key="2" value-ref="ds2"/>                < /map>             < /property>             < property name="defaultTargetDataSource" ref="1"/>     < /bean>         < !-- SqlMap setup for iBATIS Database Layer -->  < bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   < property name="dataSource" ref="dataSource"/>   < property name="configLocation" value="classpath:com/xxx/xxx/dao/sqlmap/sql-map-config.xml"/>  < /bean>         < bean id="testDAO" class="com.xxx.xxx.dao.impl.TestDAO">   < property name="sqlMapClient" ref="sqlMapClient"/>  < /bean>         < bean id="testService" class="com.xxx.xxx.service.impl.TestService">   < property name="testDAO" ref="testDAO"/>  < /bean> < /beans>

其核心是DynamicDataSource,代碼如下

package com.xxx.xxx.util;   import org.apache.log4j.Logger;  import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;   public class DynamicDataSource extends AbstractRoutingDataSource {    static Logger log = Logger.getLogger("DynamicDataSource");   @Override  protected Object determineCurrentLookupKey() {    // TODO Auto-generated method stub    return DbContextHolder.getDbType();   }   }

上下文DbContextHolder為一線程安全的ThreadLocal,如下

package com.xxx.xxx.util;   public class DbContextHolder {   private static final ThreadLocal contextHolder = new ThreadLocal();    public static void setDbType(String dbType) {    contextHolder.set(dbType);   }    public static String getDbType() {    return (String) contextHolder.get();   }    public static void clearDbType() {    contextHolder.remove();   }  }

sql-map-config.xml如下

< ?xml version="1.0" encoding="UTF-8" standalone="no"?> < !DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"          "http://www.ibatis.com/dtd/sql-map-config-2.dtd">  < sqlMapConfig>      < sqlMap resource="com/xxx/xxx/dao/sqlmap/Object.xml"/>  < /sqlMapConfig>

這樣在調(diào)用service之前只需要設(shè)置一下上下文即可調(diào)用相應(yīng)的數(shù)據(jù)源,如下:

DbContextHolder.setDbType("2");  //execute services  //.........

dao如下

package com.xxx.xxx.dao.impl;   import java.util.HashMap;  import java.util.List;  import java.util.Map;   import org.apache.log4j.Logger;  import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;   import com.xxx.xxx.vo.TestObj;   public class TestDAO extends SqlMapClientDaoSupport implements ITestDAO {       static Logger log = Logger.getLogger(TestDAO.class);       public TestObj getTestObj(String objID) throws Exception {          return (TestObj) getSqlMapClientTemplate().queryForObject("getTestObj", objID);      }  }

“如何使用Spring解決ibatis多數(shù)據(jù)源的問題”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

當(dāng)前名稱:如何使用Spring解決ibatis多數(shù)據(jù)源的問題
本文路徑:http://muchs.cn/article12/jcpedc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、做網(wǎng)站、全網(wǎng)營銷推廣、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司