熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

使用Spring解決ibatis多數據源的苦惱

2013-11-23 20:28:00  來源: Java開源技術 

  iBatis多數據源的苦惱

  在僅使用ibatis時多數據源簡直就是夢魇每多一個數據源就需要多一份sqlmapconfig配置文件

  采用spring的AbstractRoutingDataSource就可以簡單的解決這個問題

  AbstractRoutingDataSource實現了javaxsqlDataSource接口因此可以理解為一個虛擬的動態DataSource在需要的時候根據上下文Context動態決定使用哪個數據源

  Spring+iBatis多數據源的配置

  下面是一個完整的配置

  

  < beans xmlns=
  xmlns:xsi=instance
  xmlns:aop=
  xmlns:tx=
  xmlns:jee=
  xsi:schemaLocation=
   beansxsd
   aopxsd
   txxsd
   jeexsd>

  < ! ========================= GENERAL DEFINITIONS ========================= >
 < jee:jndilookup id=ds jndiname=jdbc/ds/>
 < jee:jndilookup id=ds jndiname=jdbc/ds/>
 < jee:jndilookup id=ds jndiname=jdbc/ds/>

  < bean id=dataSource class=comxxxxxxutilDynamicDataSource>
         < property name=targetDataSources>
            < map keytype=javalangString>
               < entry key= valueref=ds/>
               < entry key= valueref=ds/>
               < entry key= valueref=ds/>
            < /map>
         < /property>
         < property name=defaultTargetDataSource ref=/>
 < /bean>
        < ! SqlMap setup for iBATIS Database Layer >
 < bean id=sqlMapClient class=orgspringframeworkormibatisSqlMapClientFactoryBean>
  < property name=dataSource ref=dataSource/>
  < property name=configLocation value=classpath:com/xxx/xxx/dao/sqlmap/sqlmapconfigxml/>
 < /bean>
        < bean id=testDAO class=comxxxxxxdaoimplTestDAO>
  < property name=sqlMapClient ref=sqlMapClient/>
 < /bean>
        < bean id=testService class=comxxxxxxserviceimplTestService>
  < property name=testDAO ref=testDAO/>
 < /bean>
< /beans>

  其核心是DynamicDataSource代碼如下

  

  package comxxxxxxutil;

  import orgapachelogjLogger;
import orgspringframeworkjdbcdatasourcelookupAbstractRoutingDataSource;

  public class DynamicDataSource extends AbstractRoutingDataSource {

  static Logger log = LoggergetLogger(DynamicDataSource);
 @Override
 protected Object determineCurrentLookupKey() {
  // TODO Autogenerated method stub
  return DbContextHoldergetDbType();
 }

  }

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

  

  package comxxxxxxutil;

  public class DbContextHolder {
 private static final ThreadLocal contextHolder = new ThreadLocal();

  public static void setDbType(String dbType) {
  contextHolderset(dbType);
 }

  public static String getDbType() {
  return (String) contextHolderget();
 }

  public static void clearDbType() {
  contextHolderremove();
 }
}

  sqlmapconfigxml如下

  

  < ?xml version= encoding=UTF standalone=no?>
< !DOCTYPE sqlMapConfig PUBLIC ////DTD SQL Map Config //EN
        mapconfigdtd>

  < sqlMapConfig>

  < sqlMap resource=com/xxx/xxx/dao/sqlmap/Objectxml/>

  < /sqlMapConfig>

  這樣在調用service之前只需要設置一下上下文即可調用相應的數據源如下

   DbContextHoldersetDbType();
//execute services
//

  dao如下

  

  package comxxxxxxdaoimpl;

  import javautilHashMap;
import javautilList;
import javautilMap;

  import orgapachelogjLogger;
import orgspringframeworkormibatissupportSqlMapClientDaoSupport;

  import comxxxxxxvoTestObj;

  public class TestDAO extends SqlMapClientDaoSupport implements ITestDAO {

  static Logger log = LoggergetLogger(TestDAOclass);

  public TestObj getTestObj(String objID) throws Exception {
        return (TestObj) getSqlMapClientTemplate()queryForObject(getTestObj objID);
    }
}

  以上就介紹了Spring+iBatis多數據源的配置方案本文來自eddysheng的博客《基於spring和ibatis的多數據源切換方案》


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28536.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.