Spring dual-Database Configuration

Source: Internet
Author: User

Sometimes we may use two databases in a project. To implement the functions of two or more databases, we need to configure relevant information in Spring.

First, add the configuration file 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> 

The second is to add the data source ($ {...} corresponds to the configuration information in conf. properties)

 
 
  1. <! -- Data source corresponding to data A -->
  2. <Bean id = "cece_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. <! -- Data source corresponding to database B -->
  9. <Bean id = "cece_ 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>

Then add the corresponding sessionFactory:

 
 
  1. <! -- SessionFactory of A -->
  2. <Bean id = "sessionFactory_A" class = "moretv. commons. spring. hibernate3.AnnotationSessionFactoryBean">
  3. <Property name = "dataSource" ref = "performance_a"/>
  4. </Bean>
  5. <! -- SessionFactory of B -->
  6. <Bean id = "sessionFactory_ B" class = "moretv. commons. spring. hibernate3.AnnotationSessionFactoryBean">
  7. <Property name = "dataSource" ref = "performance_ B"/>
  8. </Bean>

In the dao layer of a project, the following configuration information is displayed:

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

To use two different databases, you can change

 
 
  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; "> <! -- Use DAO of database A --> </span> <bean id = "XDao" class = "xxx. xxx. xDaoImpl">
  2. <Property name = "sessionFactory" ref = "sessionFactory_A"> </property>
  3. </Bean>
  4. <! -- Use DAO of database B -->
  5. <Bean id = "XDao" class = "xxx. xxx. xDaoImpl">
  6. <Property name = "sessionFactory" ref = "sessionFactory_ B"> </property>
  7. </Bean>

In this way, dual databases can be implemented ....


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.