>  기사  >  php教程  >  Spring双数据库配置

Spring双数据库配置

WBOY
WBOY원래의
2016-06-13 11:15:141065검색

有时候我们可能在一个项目中使用两个数据库,为了实现使用两个或多个数据库的功能,我们需要在Spring中配置相关信息。

首先是添加配置文件conf.properties

<ol class="dp-c">
<li class="alt"><span><span><bean><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></bean></span></span></li>
<li><span>        <property><span class="string">"locations"</span><span>>  </span></property></span></li>
<li class="alt"><span>            <list>  </list></span></li>
<li><span>                <value>classpath:config.properties</value>  </span></li>
<li class="alt"><span>              </span></li>
<li><span>          </span></li>
<li class="alt"><span> </span></li>
</ol>

其次是添加数据源(${...}对应的是conf.properties中的配置信息)

<ol class="dp-c">
<li class="alt"><span><span><!--对应数据A的数据源-->  </span></span></li>
<li><span><bean><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></bean></span></li>
<li class="alt"><span>        <property><span class="string">"driverClassName"</span><span> value=</span><span class="string">"${A.driver_class}"</span><span> />  </span></property></span></li>
<li><span>        <property><span class="string">"url"</span><span> value=</span><span class="string">"${A.url}"</span><span> />  </span></property></span></li>
<li class="alt"><span>        <property><span class="string">"username"</span><span> value=</span><span class="string">"${A.username}"</span><span> />  </span></property></span></li>
<li><span>        <property><span class="string">"password"</span><span> value=</span><span class="string">"${A.password}"</span><span> />  </span></property></span></li>
<li class="alt"><span>  </span></li>
<li><span><!--对应数据库B的数据源-->  </span></li>
<li class="alt"><span><bean><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></bean></span></li>
<li><span>        <property><span class="string">"driverClassName"</span><span> value=</span><span class="string">"${B.driver_class}"</span><span> />  </span></property></span></li>
<li class="alt"><span>        <property><span class="string">"url"</span><span> value=</span><span class="string">"${B.url}"</span><span> />  </span></property></span></li>
<li><span>        <property><span class="string">"username"</span><span> value=</span><span class="string">"${B.username}"</span><span> />  </span></property></span></li>
<li class="alt"><span>        <property><span class="string">"password"</span><span> value=</span><span class="string">"${B.password}"</span><span> />  </span></property></span></li>
<li><span> </span></li>
</ol>

之后是添加对应的sessionFactory:

<ol class="dp-c">
<li class="alt"><span><span><!-- A的sessionFactory -->  </span></span></li>
<li><span>    <bean><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></bean></span></li>
<li class="alt"><span>        <property><span class="string">"dataSource"</span><span> ref=</span><span class="string">"dataSource_A"</span><span>/>  </span></property></span></li>
<li><span>      </span></li>
<li class="alt"><span><!-- B的sessionFactory -->  </span></li>
<li><span>    <bean><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></bean></span></li>
<li class="alt"><span>        <property><span class="string">"dataSource"</span><span> ref=</span><span class="string">"dataSource_B"</span><span>/>      </span></property></span></li>
<li><span>     </span></li>
</ol>

在项目中的dao层有时会出现这样的配置信息:

<ol class="dp-c">
<li class="alt"><span><span><bean><span class="string">"XDao"</span><span> </span><span class="keyword">class</span><span> = </span><span class="string">"xxx.xxx.xDaoImpl"</span><span>>  </span></bean></span></span></li>
<li><span><property><span class="string">"sessionFactory"</span><span> ref=</span><span class="string">"sessionFactory"</span><span>></span></property>  </span></li>
<li class="alt"><span> </span></li>
</ol>

为了实现使用两个不同的数据库,可以改成

<ol class="dp-c">
<li class="alt"><span><span><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><span class="string">"font-family:'sans serif', tahoma, verdana, helvetica;white-space:normal;background-color:#ffffff;"</span><span>><!--使用A数据库的DAO--></span> <bean><span class="string">"XDao"</span><span> </span><span class="keyword">class</span><span> = </span><span class="string">"xxx.xxx.xDaoImpl"</span><span>>  </span></bean></span></span></span></span></li>
<li><span><property><span class="string">"sessionFactory"</span><span> ref=</span><span class="string">"sessionFactory_A"</span><span>></span></property>  </span></li>
<li class="alt"><span>  </span></li>
<li><span><!--使用B数据库的DAO-->  </span></li>
<li class="alt"><span><bean><span class="string">"XDao"</span><span> </span><span class="keyword">class</span><span> = </span><span class="string">"xxx.xxx.xDaoImpl"</span><span>>  </span></bean></span></li>
<li><span><property><span class="string">"sessionFactory"</span><span> ref=</span><span class="string">"sessionFactory_B"</span><span>></span></property>  </span></li>
<li class="alt"><span> </span></li>
</ol>

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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.