Spring+MyBatis多数据源配置实现

Spring多数据源的配置

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd	
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
		default-autowire="byName">

	<bean id="xingeimsDataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://172.16.4.39/xingeims?characterEncoding=utf8" />
		<property name="username" value="xinge" />
		<property name="password" value="xingeims" />
	</bean>

	<bean id="creditDataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/credit?characterEncoding=utf8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>


	<bean id="multipleDataSource" class="com.xinge.ims.common.MultipleDataSource">
		<property name="defaultTargetDataSource" ref="xingeimsDataSource" />
		<property name="targetDataSources">
			<map>
				<entry key="xingeimsDataSource" value-ref="xingeimsDataSource" />
				<entry key="creditDataSource" value-ref="creditDataSource" />
			</map>
		</property>
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="multipleDataSource" />
		<property name="typeAliasesPackage" value="com.xinge.ims.dal.domain" />
	</bean>

	<!-- scan for mappers and let them be autowired -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xinge.ims.dal.persistence" />
	</bean>

	<!-- transaction manager, use JtaTransactionManager for global tx -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="multipleDataSource" />
	</bean>

	<!-- enable transaction demarcation with annotations -->
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
public class MultipleDataSource 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();
	}

	
}
@Component
@Aspect
public class MultipleDataSourceAspectAdvice {

	/**
	 * 设置数据源,拦截dao层
	 * @param jp
	 * @return
	 * @throws Throwable
	 */
	@Around("execution(* com.xinge.ims.dal.persistence.*.*(..))")
	public Object doAround(ProceedingJoinPoint jp) throws Throwable {
		if (jp.getTarget() instanceof CreditDAO) {
			MultipleDataSource.setDataSourceKey("creditDataSource");
		} else {
			MultipleDataSource.setDataSourceKey("xingeimsDataSource");
		}
		return jp.proceed();
	}
}


你可能感兴趣的:(Spring+MyBatis多数据源配置实现)