zoukankan      html  css  js  c++  java
  • spring双数据库配置

    有时候我们可能在一个项目中使用两个数据库,为了实现使用两个或多个数据库的功能,我们需要在Spring中配置相关信息。

    首先是添加配置文件conf.properties

    1. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    2.         <property name="locations">  
    3.             <list>  
    4.                 <value>classpath:config.properties</value>  
    5.             </list>  
    6.         </property>  
    7. </bean> 

    其次是添加数据源(${...}对应的是conf.properties中的配置信息)

    1. <!--对应数据A的数据源-->  
    2. <bean id="dataSource_A" class="org.apache.commons.dbcp.BasicDataSource">  
    3.         <property name="driverClassName" value="${A.driver_class}" />  
    4.         <property name="url" value="${A.url}" />  
    5.         <property name="username" value="${A.username}" />  
    6.         <property name="password" value="${A.password}" />  
    7. </bean>  
    8. <!--对应数据库B的数据源-->  
    9. <bean id="dataSource_B" class="org.apache.commons.dbcp.BasicDataSource">  
    10.         <property name="driverClassName" value="${B.driver_class}" />  
    11.         <property name="url" value="${B.url}" />  
    12.         <property name="username" value="${B.username}" />  
    13.         <property name="password" value="${B.password}" />  
    14. </bean> 

    之后是添加对应的sessionFactory:

    1. <!-- A的sessionFactory -->  
    2.     <bean id="sessionFactory_A" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">  
    3.         <property name="dataSource" ref="dataSource_A"/>  
    4.     </bean>  
    5. <!-- B的sessionFactory -->  
    6.     <bean id="sessionFactory_B" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">  
    7.         <property name="dataSource" ref="dataSource_B"/>      
    8.     </bean> 

    在项目中的dao层有时会出现这样的配置信息:

    1. <bean id = "XDao" class = "xxx.xxx.xDaoImpl">  
    2. <property name="sessionFactory" ref="sessionFactory"></property>  
    3. </bean> 

    为了实现使用两个不同的数据库,可以改成

    1. <span style="font-family:'sans serif', tahoma, verdana, helvetica;font-size:13px;line-height:19px;white-space:normal;background-color:#ffffff;">&nbsp;</span><span style="font-family:'sans serif', tahoma, verdana, helvetica;white-space:normal;background-color:#ffffff;">
    2. <!--使用A数据库的DAO--></span> 
    3. <bean id = "XDao" class = "xxx.xxx.xDaoImpl">  
    4. <property name="sessionFactory" ref="sessionFactory_A"></property>  
    5. </bean>  
    6. <!--使用B数据库的DAO-->  
    7. <bean id = "XDao" class = "xxx.xxx.xDaoImpl">  
    8. <property name="sessionFactory" ref="sessionFactory_B"></property>  
    9. </bean> 
  • 相关阅读:
    按照指定的字符串拆分字符串,split()方法。
    charAt()取出指定位置的字符 .length()得到一个字符串的长度 indexOf()查找一个指定的字符是否存在并返回其位置 trim()去掉字符串的左右空格 substring()字符串的截取 str.replace(" ", ""); 去掉所有空格,包括首尾、中间
    字符串与字符数组的多种转换方式。
    匿名对象。
    构造方法。
    递归的练习,1.统计文件夹大小 2.删除文件夹及文件夹下的文件
    jquery零散小饼干
    jQuery review
    git解决冲突
    url、href、src
  • 原文地址:https://www.cnblogs.com/123a/p/2706971.html
Copyright © 2011-2022 走看看