首页 > php教程 > php手册 > 正文

Spring双数据库配置

php中文网
发布: 2016-06-13 11:15:14
原创
1201人浏览过

有时候我们可能在一个项目中使用两个数据库,为了实现使用两个或多个数据库的功能,我们需要在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层有时会出现这样的配置信息:

数据源可配置的记事日历插件
数据源可配置的记事日历插件

数据源可配置的记事日历插件

数据源可配置的记事日历插件 52
查看详情 数据源可配置的记事日历插件
<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>
登录后复制

这样就能实现双数据库了。。。。


最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号