SprintMVC+mybatis 多資料來源
SprintMVC+mybatis 多資料來源配置
1. 網上看了不少文章,都有這方面相關的文章,寫這邊部落格,也主要是給自己留一份記錄
2. 今天這個方法雖然實現了多資料來源的切換,不過未採用分散式事務管理,導致單個業務內 多資料來源無法回滾 ,這個後面有時間再加上。(這個連結寫的方法應該能用 https://blog.csdn.net/qq_37061442/article/details/82350258)
一定要注意的地方: 在沒配分散式事務的情況下 呼叫多資料來源的方法記得要去掉事務, 不去掉事務 切換資料來源會失敗
(https://blog.csdn.net/wangjun5159/article/details/51980489)
下面程式碼奉上 :
- 首先是資料來源配置 spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:websocket="http://www.springframework.org/schema/websocket" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"> <context:annotation-config /> <!-- 資料轉換和資料格式化功能日期 --> <mvc:annotation-driven /> <context:component-scan base-package="com.cjkj" /> <!-- 資料來源配置 --> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/h5mEduDB"></property> </bean> <!-- 外網資料來源配置 --> <bean id="nwDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/wwH5mEdu"></property> </bean> <!--統一的dataSource--> <bean id="dynamicDataSource" class="com.cjkj.h5m.dataSource.DynamicDataSource" > <property name="targetDataSources"> <map key-type="java.lang.String"> <!--通過不同的key決定用哪個dataSource--> <entry value-ref="dataSource" key="dataSource"></entry> <entry value-ref="nwDataSource" key="nwDataSource"></entry> </map> </property> <!--設定預設的dataSource--> <property name="defaultTargetDataSource" ref="dataSource"> </property> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置對映檔案 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dynamicDataSource" /> <!-- 自動掃描mapping.xml檔案 --> <property name="mapperLocations" value="classpath:/mybatis/*Mapper.xml"></property> </bean> <!-- DAO介面所在包名,Spring會自動查詢其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cjkj.h5m.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dynamicDataSource" /> </bean> <!-- 使用annotation定義事務 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- 將常用的屬性值放入屬性檔案中 --> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:/conf/*.properties</value> </list> </property> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties" /> </bean> </beans>
- 第二 增加兩個檔案
CustomerContextHolder
public class CustomerContextHolder { public static final String DATA_SOURCE_DEV = "dataSource"; public static final String DATA_SOURCE_NW = "nwDataSource"; //用ThreadLocal來設定當前執行緒使用哪個dataSource private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static void setCustomerType(String customerType) { contextHolder.set(customerType); } public static String getCustomerType() { String dataSource = contextHolder.get(); if (StringUtils.isEmpty(dataSource)) { return DATA_SOURCE_DEV; }else { return dataSource; } } public static void clearCustomerType() { contextHolder.remove(); } }
DynamicDataSource (此處DynamicDataSource 和 spring-mybatis.xml 中 <bean id="dynamicDataSource" class="com.cjkj.h5m.dataSource.DynamicDataSource" > 路徑相對應)
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
public static void setDataSourceKey(String dataSource) {
dataSourceKey.set(dataSource);
}
@Override
protected Object determineCurrentLookupKey() {
return dataSourceKey.get();
}
}
- 第三 如何切換資料來源
在業務模組程式碼中 使用下面程式碼進行切換
DynamicDataSource.setDataSourceKey(CustomerContextHolder.DATA_SOURCE_DEV);//切換本地資料來源
user.save(); //訪問本地資料來源
DynamicDataSource.setDataSourceKey(CustomerContextHolder.DATA_SOURCE_NW); //切換NW資料來源
user.save();//訪問nw 資料來源