Heim >Backend-Entwicklung >PHP-Tutorial >Spring双数据库配置_PHP教程

Spring双数据库配置_PHP教程

WBOY
WBOYOriginal
2016-07-20 10:58:52847Durchsuche

有时候我们可能在一个项目中使用两个数据库,为了实现使用两个或多个数据库的功能,我们需要在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>

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445650.htmlTechArticle有时候我们可能在一个项目中使用两个数据库,为了实现使用两个或多个数据库的功能,我们需要在Spring中配置相关信息。 首先是添加配置...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn