Home >Backend Development >PHP Tutorial >Spring dual database configuration_PHP tutorial
Sometimes we may use two databases in a project. In order to implement the function of using two or more databases, we need to configure relevant information in Spring.
The first is to add the configuration file conf.properties
<ol class="dp-c"> <li class="alt"><span><span><bean id=</span><span class="string">"propertyConfigurer"</span><span> </span><span class="keyword">class</span><span>=</span><span class="string">"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"</span><span>> </span></span></li> <li><span> <property name=</span><span class="string">"locations"</span><span>> </span></li> <li class="alt"><span> <list> </span></li> <li><span> <value>classpath:config.properties</value> </span></li> <li class="alt"><span> </list> </span></li> <li><span> </property> </span></li> <li class="alt"><span></bean> </span></li> </ol>
The second is to add the data source (${...} corresponds to the configuration information in conf.properties)
<ol class="dp-c"> <li class="alt"><span><span><!--对应数据A的数据源--> </span></span></li> <li><span><bean id=</span><span class="string">"dataSource_A"</span><span> </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.commons.dbcp.BasicDataSource"</span><span>> </span></li> <li class="alt"><span> <property name=</span><span class="string">"driverClassName"</span><span> value=</span><span class="string">"${A.driver_class}"</span><span> /> </span></li> <li><span> <property name=</span><span class="string">"url"</span><span> value=</span><span class="string">"${A.url}"</span><span> /> </span></li> <li class="alt"><span> <property name=</span><span class="string">"username"</span><span> value=</span><span class="string">"${A.username}"</span><span> /> </span></li> <li><span> <property name=</span><span class="string">"password"</span><span> value=</span><span class="string">"${A.password}"</span><span> /> </span></li> <li class="alt"><span></bean> </span></li> <li><span><!--对应数据库B的数据源--> </span></li> <li class="alt"><span><bean id=</span><span class="string">"dataSource_B"</span><span> </span><span class="keyword">class</span><span>=</span><span class="string">"org.apache.commons.dbcp.BasicDataSource"</span><span>> </span></li> <li><span> <property name=</span><span class="string">"driverClassName"</span><span> value=</span><span class="string">"${B.driver_class}"</span><span> /> </span></li> <li class="alt"><span> <property name=</span><span class="string">"url"</span><span> value=</span><span class="string">"${B.url}"</span><span> /> </span></li> <li><span> <property name=</span><span class="string">"username"</span><span> value=</span><span class="string">"${B.username}"</span><span> /> </span></li> <li class="alt"><span> <property name=</span><span class="string">"password"</span><span> value=</span><span class="string">"${B.password}"</span><span> /> </span></li> <li><span></bean> </span></li> </ol>
is followed by adding the corresponding sessionFactory:
<ol class="dp-c"> <li class="alt"><span><span><!-- A的sessionFactory --> </span></span></li> <li><span> <bean id=</span><span class="string">"sessionFactory_A"</span><span> </span><span class="keyword">class</span><span>=</span><span class="string">"moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean"</span><span>> </span></li> <li class="alt"><span> <property name=</span><span class="string">"dataSource"</span><span> ref=</span><span class="string">"dataSource_A"</span><span>/> </span></li> <li><span> </bean> </span></li> <li class="alt"><span><!-- B的sessionFactory --> </span></li> <li><span> <bean id=</span><span class="string">"sessionFactory_B"</span><span> </span><span class="keyword">class</span><span>=</span><span class="string">"moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean"</span><span>> </span></li> <li class="alt"><span> <property name=</span><span class="string">"dataSource"</span><span> ref=</span><span class="string">"dataSource_B"</span><span>/> </span></li> <li><span> </bean> </span></li> </ol>
Sometimes such configuration information appears in the dao layer in the project:
<ol class="dp-c"> <li class="alt"><span><span><bean id = </span><span class="string">"XDao"</span><span> </span><span class="keyword">class</span><span> = </span><span class="string">"xxx.xxx.xDaoImpl"</span><span>> </span></span></li> <li><span><property name=</span><span class="string">"sessionFactory"</span><span> ref=</span><span class="string">"sessionFactory"</span><span>></property> </span></li> <li class="alt"><span></bean> </span></li> </ol>
In order to achieve this, two different The database can be changed to
<ol class="dp-c"> <li class="alt"><span><span><span style=</span><span class="string">"font-family:'sans serif', tahoma, verdana, helvetica;font-size:13px;line-height:19px;white-space:normal;background-color:#ffffff;"</span><span>> </span><span style=</span><span class="string">"font-family:'sans serif', tahoma, verdana, helvetica;white-space:normal;background-color:#ffffff;"</span><span>><!--使用A数据库的DAO--></span> <bean id = </span><span class="string">"XDao"</span><span> </span><span class="keyword">class</span><span> = </span><span class="string">"xxx.xxx.xDaoImpl"</span><span>> </span></span></li> <li><span><property name=</span><span class="string">"sessionFactory"</span><span> ref=</span><span class="string">"sessionFactory_A"</span><span>></property> </span></li> <li class="alt"><span></bean> </span></li> <li><span><!--使用B数据库的DAO--> </span></li> <li class="alt"><span><bean id = </span><span class="string">"XDao"</span><span> </span><span class="keyword">class</span><span> = </span><span class="string">"xxx.xxx.xDaoImpl"</span><span>> </span></li> <li><span><property name=</span><span class="string">"sessionFactory"</span><span> ref=</span><span class="string">"sessionFactory_B"</span><span>></property> </span></li> <li class="alt"><span></bean> </span></li> </ol>
so that dual databases can be realized. . . .