有时候我们可能在一个项目中使用两个数据库,为了实现使用两个或多个数据库的功能,我们需要在spring中配置相关信息。
首先是添加配置文件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>
其次是添加数据源(${...}对应的是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>之后是添加对应的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>
在项目中的dao层有时会出现这样的配置信息:
<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>
为了实现使用两个不同的数据库,可以改成
<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>
这样就能实现双数据库了。。。。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号