Spring雙數(shù)據(jù)庫是怎樣配置的

這篇文章將為大家詳細講解有關(guān)Spring雙數(shù)據(jù)庫是怎樣配置的,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)公司長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為玉田企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè),玉田網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

有時候我們可能在一個項目中使用兩個數(shù)據(jù)庫,為了實現(xiàn)使用兩個或多個數(shù)據(jù)庫的功能,我們需要在Spring中配置相關(guān)信息。

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

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="locations">              <list>                  <value>classpath:config.properties</value>              </list>          </property>  </bean>

其次是添加數(shù)據(jù)源(${...}對應(yīng)的是conf.properties中的配置信息)

<!--對應(yīng)數(shù)據(jù)A的數(shù)據(jù)源-->  <bean id="dataSource_A" class="org.apache.commons.dbcp.BasicDataSource">          <property name="driverClassName" value="${A.driver_class}" />          <property name="url" value="${A.url}" />          <property name="username" value="${A.username}" />          <property name="password" value="${A.password}" />  </bean>  <!--對應(yīng)數(shù)據(jù)庫B的數(shù)據(jù)源-->  <bean id="dataSource_B" class="org.apache.commons.dbcp.BasicDataSource">          <property name="driverClassName" value="${B.driver_class}" />          <property name="url" value="${B.url}" />          <property name="username" value="${B.username}" />          <property name="password" value="${B.password}" />  </bean>

之后是添加對應(yīng)的sessionFactory:

<!-- A的sessionFactory -->      <bean id="sessionFactory_A" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">          <property name="dataSource" ref="dataSource_A"/>      </bean>  <!-- B的sessionFactory -->      <bean id="sessionFactory_B" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">          <property name="dataSource" ref="dataSource_B"/>          </bean>

在項目中的dao層有時會出現(xiàn)這樣的配置信息:

<bean id = "XDao" class = "xxx.xxx.xDaoImpl">  <property name="sessionFactory" ref="sessionFactory"></property>  </bean>

為了實現(xiàn)使用兩個不同的數(shù)據(jù)庫,可以改成

<span style="font-family:'sans serif', tahoma, verdana, helvetica;font-size:13px;line-height:19px;white-space:normal;background-color:#ffffff;">&nbsp;</span><span style="font-family:'sans serif', tahoma, verdana, helvetica;white-space:normal;background-color:#ffffff;"><!--使用A數(shù)據(jù)庫的DAO--></span> <bean id = "XDao" class = "xxx.xxx.xDaoImpl">  <property name="sessionFactory" ref="sessionFactory_A"></property>  </bean>  <!--使用B數(shù)據(jù)庫的DAO-->  <bean id = "XDao" class = "xxx.xxx.xDaoImpl">  <property name="sessionFactory" ref="sessionFactory_B"></property>  </bean>

這樣就能實現(xiàn)雙數(shù)據(jù)庫了。。。。

關(guān)于Spring雙數(shù)據(jù)庫是怎樣配置的就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享名稱:Spring雙數(shù)據(jù)庫是怎樣配置的
路徑分享:http://muchs.cn/article22/gdiojc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、網(wǎng)站營銷、網(wǎng)站策劃、虛擬主機網(wǎng)站設(shè)計公司、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管