日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

双数据源配置(一个项目中链接两个数据库)

發布時間:2025/3/15 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 双数据源配置(一个项目中链接两个数据库) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
resin文件配置(我用的resin,其他的可自行配置) <database><jndi-name>jdbc/OracleDB</jndi-name><driver type="oracle.jdbc.driver.OracleDriver"><!--<url>jdbc:oracle:thin:@oa.paipai580.com:1521:orcl</url><user>cggl</user><password>cggl345#$%</password>--><url>jdbc:oracle:thin:@192.168.1.9:1521:orcl</url><user>ycg</user><password>ycg</password></driver><prepared-statement-cache-size>8</prepared-statement-cache-size><max-connections>20</max-connections><max-idle-time>30s</max-idle-time></database><database><jndi-name>jdbc/mysqlDB</jndi-name><driver type="com.mysql.jdbc.Driver"><url>jdbc:mysql://39.98.47.52:3306/dev-idr</url><user>admin</user><password>mySteel2019,!@#</password></driver><prepared-statement-cache-size>8</prepared-statement-cache-size><max-connections>20</max-connections><max-idle-time>30s</max-idle-time></database>

1.  然后寫一個數據源持有類:

package org.thcic.ejw.components.datatable;public class DataSourceHolder {private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();/*** @Description: 設置數據源類型* @param dataSourceType 數據庫類型* @return void* @throws*/public static void setDataSourceType(String dataSourceType) {contextHolder.set(dataSourceType);}/*** @Description: 獲取數據源類型* @return String* @throws*/public static String getDataSourceType() {return contextHolder.get();}/*** @Description: 清除數據源類型* @return void* @throws*/public static void clearDataSourceType() {contextHolder.remove();}}

  2.?寫一個數據源路由類

package org.thcic.ejw.components.datatable;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class RoutingDataSource extends AbstractRoutingDataSource {@Overrideprotected Object determineCurrentLookupKey() {return DataSourceHolder.getDataSourceType();}}

  

3. 配置數據源路由

<!--配置數據源路由--><bean id="dataSource" class="org.thcic.ejw.components.datatable.RoutingDataSource"><!-- 為targetDataSources注入兩個數據源 --><property name="targetDataSources"><map key-type="java.lang.String"><entry key="orcleDb" value-ref="orclDataSource"/><entry key="mysqlDb" value-ref="mysqlDataSource"/></map></property><!-- 為指定數據源RoutingDataSource注入默認的數據源--><property name="defaultTargetDataSource" ref="orclDataSource"/></bean>

?

4. spring整合

<!--hibernate start--><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">${dataSource.hibernate.dialect}</prop><prop key="hibernate.connection.useUnicode">true</prop><prop key="hibernate.connection.characterEncoding">utf-8</prop><prop key="hibernate.show_sql">${dataSource.hibernate.show_sql}</prop><prop key="hibernate.jdbc.fetch_size">50</prop></props></property><property name="packagesToScan"> <list> <value>org.thcic.ejw.sys.vo</value> <value>org.thcic.ejw.purchase.setting.vo</value> <value>org.thcic.ejw.purchase.inquiry.vo</value><value>org.thcic.ejw.purchase.notice.vo</value> <value>org.thcic.ejw.purchase.bid.vo</value><value>org.thcic.ejw.purchase.report.vo</value> </list> </property></bean>

  

5.雙jdbcTemplate配置

<!--jdbcTemplate start--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"lazy-init="false" autowire="default" ><property name="dataSource" ref="dataSource"></property></bean><bean id="mysqlJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"lazy-init="false" autowire="default" ><property name="dataSource" ref="mysqlDataSource"></property></bean>

  

6.使用數據源

/*jdbcTemplate的使用*/ @Autowired@Qualifier("oracleJdbcTemplate")private JdbcTemplate dbTwojdbcTemplate;@Autowired@Qualifier("mysqlJdbcTemplate")private JdbcTemplate dbOneJdbcTemplate;/*數據源切換*/ //獲取數據源類型 DataSourceHolder.getDataSourceType(); //設置為 db1 的數據源 DataSourceHolder.setDataSourceType("db1"); //設置為 db2 的數據源 DataSourceHolder.setDataSourceType("db2");

  

?

轉載于:https://www.cnblogs.com/HHR-SUN/p/11187408.html

總結

以上是生活随笔為你收集整理的双数据源配置(一个项目中链接两个数据库)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。