Spring雙資料庫配置_PHP教程

來源:互聯網
上載者:User
有時候我們可能在一個項目中使用兩個資料庫,為了實現使用兩個或多個資料庫的功能,我們需要在Spring中配置相關資訊。

首先是添加設定檔conf.properties

 
  1. "propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2. "locations">
  3. classpath:config.properties

其次是添加資料來源(${...}對應的是conf.properties中的配置資訊)

 
  1. "dataSource_A" class="org.apache.commons.dbcp.BasicDataSource">
  2. "driverClassName" value="${A.driver_class}" />
  3. "url" value="${A.url}" />
  4. "username" value="${A.username}" />
  5. "password" value="${A.password}" />
  6. "dataSource_B" class="org.apache.commons.dbcp.BasicDataSource">
  7. "driverClassName" value="${B.driver_class}" />
  8. "url" value="${B.url}" />
  9. "username" value="${B.username}" />
  10. "password" value="${B.password}" />

之後是添加對應的sessionFactory:

 
  1. "sessionFactory_A" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">
  2. "dataSource" ref="dataSource_A"/>
  3. "sessionFactory_B" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">
  4. "dataSource" ref="dataSource_B"/>

在項目中的dao層有時會出現這樣的配置資訊:

 
  1. "XDao" class = "xxx.xxx.xDaoImpl">
  2. "sessionFactory" ref="sessionFactory">

為了實現使用兩個不同的資料庫,可以改成

 
  1. "font-family:'sans serif', tahoma, verdana, helvetica;font-size:13px;line-height:19px;white-space:normal;background-color:#ffffff;"> "font-family:'sans serif', tahoma, verdana, helvetica;white-space:normal;background-color:#ffffff;"> "XDao" class = "xxx.xxx.xDaoImpl">
  2. "sessionFactory" ref="sessionFactory_A">
  3. "XDao" class = "xxx.xxx.xDaoImpl">
  4. "sessionFactory" ref="sessionFactory_B">

這樣就能實現雙資料庫了。。。。


http://www.bkjia.com/PHPjc/445650.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445650.htmlTechArticle有時候我們可能在一個項目中使用兩個資料庫,為了實現使用兩個或多個資料庫的功能,我們需要在Spring中配置相關資訊。 首先是添加配置...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.