spring基于通用Dao的多数据源配置详解

有时候在一个项目中会连接多个数据库,需要在spring中配置多个数据源,最近就遇到了这个问题,由于我的项目之前是基于通用Dao的,配置的时候问题不断,这种方式和资源文件冲突;扫描映射文件的话,SqlSessionFactory的bean名字必须是sqlSessionFactory 他读不到sqlSessioNFactory2或者其他名字,最终解决方法如下:

十载专注成都网站制作,企业网站设计,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于企业网站设计,高端网页制作,对水泥搅拌车等多个行业,拥有丰富设计经验。

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

测试类如下:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。

当前标题:spring基于通用Dao的多数据源配置详解
网页路径:https://www.cdcxhl.com/article36/gpjppg.html

成都网站建设公司_创新互联,为您提供网站维护品牌网站建设云服务器Google微信小程序App开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

手机网站建设