一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - spring基于通用Dao的多數(shù)據(jù)源配置詳解

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

2021-04-15 11:16Crazy光光 Java教程

這篇文章主要為大家詳細(xì)介紹了spring基于通用Dao的多數(shù)據(jù)源配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下解

有時(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或者其他名字,最終解決方法如下:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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配置文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<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è)試類如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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í)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/u012116457/article/details/50518430

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品久久久成人 | 免费一级特黄特色大片在线观看 | 日韩av.com| 国产伦精一区二区三区视频 | 热穴高校| 亚洲欧美日韩一区成人 | 亚洲欧美精品天堂久久综合一区 | 密臀tv| 欧美日韩亚洲另类人人澡 | 日韩欧美在线视频一区二区 | 国产精品青青青高清在线密亚 | 日本男男gaygays| 麻豆网站在线看 | 好大好硬好深好爽想要吃奶 | 久久亚洲国产成人影院 | 日本深夜视频 | ferr孕妇videos毛茸茸 | 美女伊人网 | 天天射天天舔 | 亚洲国产成人99精品激情在线 | 三体动漫在线观看免费完整版2022 | 四虎免费影院ww4164h | 成人精品一区久久久久 | 美女脱一光二净的视频 | 人配人种视频xxxx | 国产精品天天看天天爽 | 精品日韩欧美一区二区三区 | 男人肌肌捅女人肌肌 | 亚洲视频中文 | 小苹果日本在线观看 | 国产-第1页-草草影院 | 91麻豆精品 | 久久国产36精品色熟妇 | 婷婷色在线 | yy6080欧美三级理论 | 97网站| 亚洲夜色夜色综合网站 | 免费永久观看美女视频网站网址 | 亚洲成人影院在线观看 | 欧美一区a | 午夜理论片YY4399影院 |