双数据源配置(一个项目中链接两个数据库)
生活随笔
收集整理的這篇文章主要介紹了
双数据源配置(一个项目中链接两个数据库)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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. 然后寫一個數(shù)據(jù)源持有類:
package org.thcic.ejw.components.datatable;public class DataSourceHolder {private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();/*** @Description: 設置數(shù)據(jù)源類型* @param dataSourceType 數(shù)據(jù)庫類型* @return void* @throws*/public static void setDataSourceType(String dataSourceType) {contextHolder.set(dataSourceType);}/*** @Description: 獲取數(shù)據(jù)源類型* @return String* @throws*/public static String getDataSourceType() {return contextHolder.get();}/*** @Description: 清除數(shù)據(jù)源類型* @return void* @throws*/public static void clearDataSourceType() {contextHolder.remove();}}2.?寫一個數(shù)據(jù)源路由類
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. 配置數(shù)據(jù)源路由
<!--配置數(shù)據(jù)源路由--><bean id="dataSource" class="org.thcic.ejw.components.datatable.RoutingDataSource"><!-- 為targetDataSources注入兩個數(shù)據(jù)源 --><property name="targetDataSources"><map key-type="java.lang.String"><entry key="orcleDb" value-ref="orclDataSource"/><entry key="mysqlDb" value-ref="mysqlDataSource"/></map></property><!-- 為指定數(shù)據(jù)源RoutingDataSource注入默認的數(shù)據(jù)源--><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.使用數(shù)據(jù)源
/*jdbcTemplate的使用*/ @Autowired@Qualifier("oracleJdbcTemplate")private JdbcTemplate dbTwojdbcTemplate;@Autowired@Qualifier("mysqlJdbcTemplate")private JdbcTemplate dbOneJdbcTemplate;/*數(shù)據(jù)源切換*/ //獲取數(shù)據(jù)源類型 DataSourceHolder.getDataSourceType(); //設置為 db1 的數(shù)據(jù)源 DataSourceHolder.setDataSourceType("db1"); //設置為 db2 的數(shù)據(jù)源 DataSourceHolder.setDataSourceType("db2");
?
轉(zhuǎn)載于:https://www.cnblogs.com/HHR-SUN/p/11187408.html
總結
以上是生活随笔為你收集整理的双数据源配置(一个项目中链接两个数据库)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Red and Black (找到一个标
- 下一篇: 【MM系列】SAP里批量设置采购信息记录