SSH配置動態資料源

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   java   ar   資料   

用到一個項目,需要整合2個不同的資料庫!

現將代碼貼下,以備後用:

1、建立靜態映射類,該類映射動態資料源
public class DataSourceMap {       public static final String Analyse="Analyse";           public static final String DLmarket= "DLmarket";    } 
2、建立資料庫連接配置容器類
public class DataSourceContextHolder {      private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();            public static void setCustomerType(String customerType){          contextHolder.set(customerType);      }            public static String getCustomerType() {          return contextHolder.get();      }        public static void clearCustomerType() {          contextHolder.remove();      }    }  
3、建立動態資料源切換類
public class DynamicDataSource extends AbstractRoutingDataSource{        @Override      protected Object determineCurrentLookupKey() {          // TODO Auto-generated method stub          String customerType="";          if(DataSourceContextHolder.getCustomerType()!=null){              customerType = DataSourceContextHolder.getCustomerType().toString();          }          return customerType;      }        }  

該類繼承AbstractRoutingDataSource,並重寫determineCurrentLookupKey方法

4、在spring中配置多資料來源
<bean id="analyseDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  <property name="url" value="${jdbc_url}" />  <property name="username" value="${jdbc_username}" />  <property name="password" value="${jdbc_password}" />  xxx...  </bean>    <bean id="dlmarketDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  <property name="url" value="${jdbc_url_dlmarket}" />  <property name="username" value="${jdbc_username_dlmarket}" />  <property name="password" value="${jdbc_password_dlmarket}" />  xxx...  <bean>    <!-- 多資料來源的映射關係 -->  <bean id="dataSource" class="com.current.util.DynamicDataSource">      <property name="targetDataSources">      <map key-type="java.lang.String">      <!-- key的值必須要和靜態索引值對照類中的值相同&nbsp; -->          <entry value-ref="analyseDataSource" key="Analyse"></entry>          <entry value-ref="dlmarketDataSource" key="DLmarket"></entry>      </map>      </property>      <property name="defaultTargetDataSource" ref="analyseDataSource"></property>  </bean>  

其他的SessionFactory   交易管理員配置都不需要修改。

5、在Action中切換資料來源
public void getList(){          DataSourceContextHolder.setCustomerType(DataSourceMap.DLmarket);          int id=1;          List<Chaining> chainList = chainService.getList(id);          System.out.println(chainList.get(0).getChaining());       } 

SSH配置動態資料源

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.