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 数据源