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.
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:config.properties</value>
- </list>
- </property>
- </bean>
The second is to add the data source ($ {...} corresponds to the configuration information in conf. properties)
- <! -- Data source corresponding to data A -->
- <Bean id = "cece_a" class = "org. apache. commons. dbcp. BasicDataSource">
- <Property name = "driverClassName" value = "$ {A. driver_class}"/>
- <Property name = "url" value = "$ {A. url}"/>
- <Property name = "username" value = "$ {A. username}"/>
- <Property name = "password" value = "$ {A. password}"/>
- </Bean>
- <! -- Data source corresponding to database B -->
- <Bean id = "cece_ B" class = "org. apache. commons. dbcp. BasicDataSource">
- <Property name = "driverClassName" value = "$ {B. driver_class}"/>
- <Property name = "url" value = "$ {B. url}"/>
- <Property name = "username" value = "$ {B. username}"/>
- <Property name = "password" value = "$ {B. password}"/>
- </Bean>
Then add the corresponding sessionFactory:
- <! -- SessionFactory of A -->
- <Bean id = "sessionFactory_A" class = "moretv. commons. spring. hibernate3.AnnotationSessionFactoryBean">
- <Property name = "dataSource" ref = "performance_a"/>
- </Bean>
- <! -- SessionFactory of B -->
- <Bean id = "sessionFactory_ B" class = "moretv. commons. spring. hibernate3.AnnotationSessionFactoryBean">
- <Property name = "dataSource" ref = "performance_ B"/>
- </Bean>
In the dao layer of a project, the following configuration information is displayed:
- <bean id = "XDao" class = "xxx.xxx.xDaoImpl">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
To use two different databases, you can change
- <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">
- <Property name = "sessionFactory" ref = "sessionFactory_A"> </property>
- </Bean>
- <! -- Use DAO of database B -->
- <Bean id = "XDao" class = "xxx. xxx. xDaoImpl">
- <Property name = "sessionFactory" ref = "sessionFactory_ B"> </property>
- </Bean>
In this way, dual databases can be implemented ....