spring基于通用Dao的多數(shù)據(jù)源配置詳解

有時(shí)候在一個(gè)項(xiàng)目中會(huì)連接多個(gè)數(shù)據(jù)庫(kù),需要在spring中配置多個(gè)數(shù)據(jù)源,最近就遇到了這個(gè)問(wèn)題,由于我的項(xiàng)目之前是基于通用Dao的,配置的時(shí)候問(wèn)題不斷,這種方式和資源文件沖突;掃描映射文件的話,SqlSessionFactory的bean名字必須是sqlSessionFactory 他讀不到sqlSessioNFactory2或者其他名字,最終解決方法如下:

十載專注成都網(wǎng)站制作,企業(yè)網(wǎng)站設(shè)計(jì),個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站設(shè)計(jì),高端網(wǎng)頁(yè)制作,對(duì)水泥攪拌車等多個(gè)行業(yè),擁有豐富設(shè)計(jì)經(jīng)驗(yàn)。

1.在項(xiàng)目中加入如下類MultipleDataSource.java

package com.etoak.util; 
 
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 
 
public class MultipleDataSource extends AbstractRoutingDataSource { 
   
  private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();  
  public static void setDataSourceKey(String dataSource) { 
    dataSourceKey.set(dataSource); 
  } 
  @Override 
  protected Object determineCurrentLookupKey() { 
    // TODO Auto-generated method stub 
    return dataSourceKey.get(); 
  } 
 
} 

spring配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:p="http://www.springframework.org/schema/p" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
 
  <context:component-scan base-package="com"/> 
   
  <mvc:annotation-driven/> 
   
 
  <context:property-placeholder location="classpath:db.properties"/> 
  <bean id="ds1" class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="${MySQL.driver}" 
    p:url="${mysql.url}" 
    p:username="${mysql.username}" 
    p:password="${mysql.password}"/> 
  <bean id="ds2" class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="${mysql2.driver}" 
    p:url="${mysql2.url}" 
    p:username="${mysql2.username}" 
    p:password="${mysql2.password}"/> 
   
   
  <bean id="multipleDataSource" class="com.etoak.util.MultipleDataSource"> 
    <property name="defaultTargetDataSource" ref="ds1"/> 
    <property name="targetDataSources"> 
      <map> 
        <entry key="ds1" value-ref="ds1"/> 
        <entry key="ds2" value-ref="ds2"/> 
      </map> 
    </property> 
  </bean> 
   
  <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean" 
    p:dataSource-ref="multipleDataSource" 
    p:mapperLocations="classpath:com/etoak/dao/*-mapper.xml"/> 
   
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    <property name="basePackage" value="com.etoak.dao"/> 
        <property name="markerInterface" value="com.etoak.dao.BaseDao" /> 
  </bean>   
   
</beans> 

測(cè)試類如下:

package com.etoak.test; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.FileSystemXmlApplicationContext; 
 
import com.etoak.dao.ProductDaoIf; 
import com.etoak.util.MultipleDataSource; 
 
public class Test { 
 
  public static void main(String[] args) { 
    ApplicationContext ac = new  
      FileSystemXmlApplicationContext("WebContent/WEB-INF/etoak-servlet.xml"); 
     
    ProductDaoIf proDao = (ProductDaoIf)ac.getBean(ProductDaoIf.class); 
     
    MultipleDataSource.setDataSourceKey("ds1"); 
    int count1 = proDao.selectProductCount(); 
    MultipleDataSource.setDataSourceKey("ds2"); 
    int count2 = proDao.selectProductCount(); 
    System.out.println(count1); 
    System.out.println(count2); 
  } 
 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

當(dāng)前標(biāo)題:spring基于通用Dao的多數(shù)據(jù)源配置詳解
網(wǎng)頁(yè)路徑:http://muchs.cn/article36/gpjppg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、品牌網(wǎng)站建設(shè)、云服務(wù)器Google、微信小程序、App開(kāi)發(fā)

廣告

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

手機(jī)網(wǎng)站建設(shè)