Spring双数据库设置装备摆设
</bean>
有时辰我们可能在一个项目中使用两个数据库,为了实现使用两个或多个数据库的功能,我们需要在Spring中设置装备摆设相关信息。
首先是添加设置装备摆设文件conf.properties:
<bean id=“propertyConfigurer”class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<list>
<value>classpath:config.properties</value>
</list>
<property name=“driverClassName” value=http://www.zhuojie.net/?/web/2012/0925/“${A.driver_class}” />
<property name=“dataSource” ref=“dataSource_B”/>
</property>
</bean>
<property name=“url” value=http://www.zhuojie.net/?/web/2012/0925/“${B.url}” />
</bean>
</bean>
其次是添加数据源(${...}对应的是conf.properties中的设置装备摆设信息)
<!--对应数据A的数据源-->
<bean id = “XDao” class = “xxx.xxx.xDaoImpl”>
<property name=“url” value=http://www.zhuojie.net/?/web/2012/0925/“${A.url}” />
<property name=“username” value=http://www.zhuojie.net/?/web/2012/0925/“${A.username}” />
<property name=“locations”>
<property name=“password” value=http://www.zhuojie.net/?/web/2012/0925/“${A.password}” />
</bean>
<property name=“password” value=http://www.zhuojie.net/?/web/2012/0925/“${B.password}” />
<!-- A的sessionFactory -->
在项目中的dao层有时会呈现这样的设置装备摆设信息:
<property name=“dataSource” ref=“dataSource_A”/>
之后是添加对应的sessionFactory:
<property name=“username” value=http://www.zhuojie.net/?/web/2012/0925/“${B.username}” />
<bean id=“dataSource_B” class=“org.apache.commons.dbcp.BasicDataSource”>
<bean id=“sessionFactory_A”class=“moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean”>
<bean id=“dataSource_A” class=“org.apache.commons.dbcp.BasicDataSource”>
</bean>
<bean id=“sessionFactory_B”class=“moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean”>
</bean>
<!--对应数据库B的数据源-->
<!--使用B数据库的DAO-->
<bean id = “XDao” class = “xxx.xxx.xDaoImpl”>
<property name=“driverClassName” value=http://www.zhuojie.net/?/web/2012/0925/“${B.driver_class}” />
<property name=“sessionFactory” ref=“sessionFactory”></property>
<!-- B的sessionFactory -->
为了实现使用两个分歧的数据库,可以改成:
<span style=“font-family:‘sans serif’, tahoma, verdana, helvetica;font-size:13px;line-height:19px;white-space:normal;background-color:#ffffff;”> </span><span style=“font-family:‘sans serif’, tahoma, verdana, helvetica;white-space:normal;background-color:#ffffff;”><!--使用A数据库的DAO--></span> <bean id = “XDao” class = “xxx.xxx.xDaoImpl”>
<property name=“sessionFactory” ref=“sessionFactory_A”></property>
<property name=“sessionFactory” ref=“sessionFactory_B”></property>
</bean>
这样就能实现双数据库了。
本页关键词: