国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring基于通用Dao的多數據源配置詳解

spring基于通用Dao的多數據源配置詳解

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

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

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

1.在項目中加入如下類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>

測試類如下:

?
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);
  }
 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品一区二区精品 | 婷婷久久五月天 | 欧美高清一区 | av免费在线观看网站 | 一区二区三区在线播放 | 亚洲久久久久久 | 国产成人黄色 | 亚洲综合二区 | 天堂免费在线 | 久久av一区二区 | 国产日韩欧美 | 国产福利视频在线观看 | 成人国产在线视频 | 亚洲精品久久久久久国产精华液 | 成人在线一级片 | 成人片免费看 | 亚洲伦理电影 | 中文字幕一区二区三区在线视频 | 日韩欧美在线不卡 | 国产精品一区二区久久 | www伊人| 久久三区 | 超色视频在线观看 | 精品在线一区二区 | 色久综合 | 久久大香 | 久操资源 | 亚洲视频天堂 | 夜夜摸夜夜操 | caoporn免费 | 爱色av网 | 国偷自产一区二区免费视频 | 玖玖综合网 | 国产精品18久久久 | 日韩精品一区二区三区中文在线 | 99久久久国产精品 | www.夜夜爽| 亚洲在线 | 亚洲视频在线观看免费 | 久久久久久久久久影院 | 夜夜摸夜夜操 |