有時候我們可能在一個項目中使用兩個資料庫,為了實現使用兩個或多個資料庫的功能,我們需要在Spring中配置相關資訊。
首先是添加設定檔conf.properties
- "propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- "locations">
-
- classpath:config.properties
-
-
-
其次是添加資料來源(${...}對應的是conf.properties中的配置資訊)
-
- "dataSource_A" class="org.apache.commons.dbcp.BasicDataSource">
- "driverClassName" value="${A.driver_class}" />
- "url" value="${A.url}" />
- "username" value="${A.username}" />
- "password" value="${A.password}" />
-
-
- "dataSource_B" class="org.apache.commons.dbcp.BasicDataSource">
- "driverClassName" value="${B.driver_class}" />
- "url" value="${B.url}" />
- "username" value="${B.username}" />
- "password" value="${B.password}" />
-
之後是添加對應的sessionFactory:
-
- "sessionFactory_A" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">
- "dataSource" ref="dataSource_A"/>
-
-
- "sessionFactory_B" class="moretv.commons.spring.hibernate3.AnnotationSessionFactoryBean">
- "dataSource" ref="dataSource_B"/>
-
在項目中的dao層有時會出現這樣的配置資訊:
- "XDao" class = "xxx.xxx.xDaoImpl">
- "sessionFactory" ref="sessionFactory">
-
為了實現使用兩個不同的資料庫,可以改成
- "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">
- "sessionFactory" ref="sessionFactory_A">
-
-
- "XDao" class = "xxx.xxx.xDaoImpl">
- "sessionFactory" ref="sessionFactory_B">
-
這樣就能實現雙資料庫了。。。。
http://www.bkjia.com/PHPjc/445650.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445650.htmlTechArticle有時候我們可能在一個項目中使用兩個資料庫,為了實現使用兩個或多個資料庫的功能,我們需要在Spring中配置相關資訊。 首先是添加配置...