spring+mybatis多数据源配置及动态切换

1、配置

1.1 jdbc.properties
#table [hero_pos]
jdbc1.driverClassName=com.mysql.jdbc.Driver
jdbc1.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
jdbc1.username=root
jdbc1.password=123456

#table [hero_menu]
jdbc2.driverClassName=com.mysql.jdbc.Driver
jdbc2.url=jdbc:mysql://localhost:3306/hero_menu?characterEncoding=UTF-8
jdbc2.username=root
jdbc2.password=123456

#table [hero_outlet]
jdbc3.driverClassName=com.mysql.jdbc.Driver
jdbc3.url=jdbc:mysql://localhost:3306/hero_outlet?characterEncoding=UTF-8
jdbc3.username=root
jdbc3.password=123456
1.2 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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	default-lazy-init="true"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<context:component-scan base-package="com.infrasys.dao" />
	<context:annotation-config />
	<context:property-placeholder location="jdbc.properties" />
	
	<!--多个数据源配置-->
	<bean id="parentDataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	</bean>
	<bean id="pos" parent="parentDataSource">
		<property name="driverClassName" value="${jdbc1.driverClassName}"></property>
		<property name="url" value="${jdbc1.url}"></property>
		<property name="username" value="${jdbc1.username}"></property>
		<property name="password" value="${jdbc1.password}"></property>
	</bean>
	<bean id="menu" parent="parentDataSource">
		<property name="driverClassName" value="${jdbc2.driverClassName}"></property>
		<property name="url" value="${jdbc2.url}"></property>
		<property name="username" value="${jdbc2.username}"></property>
		<property name="password" value="${jdbc2.password}"></property>
	</bean>
	<bean id="outlet" parent="parentDataSource">
		<property name="driverClassName" value="${jdbc3.driverClassName}"></property>
		<property name="url" value="${jdbc3.url}"></property>
		<property name="username" value="${jdbc3.username}"></property>
		<property name="password" value="${jdbc3.password}"></property>
	</bean>
	<!--动态数据源配置-->
	<bean id="dataSource" class="com.infrasys.dao.datasource.DynamicDataSource">
		<!--通过key匹配数据源-->
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<entry key="pos" value-ref="pos" />
				<entry key="menu" value-ref="menu" />
				<entry key="outlet" value-ref="outlet" />
			</map>
		</property>
		<!--默认数据源-->
		<property name="defaultTargetDataSource" ref="menu" />
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath:mybatis/*.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.infrasys.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	
	<bean id="posService" class="com.infrasys.service.PosService"></bean>
	<!--aop注解驱动-->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

2、相关类

2.1 数据源动态切换类
package com.infrasys.dao.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

	@Override
	protected Object determineCurrentLookupKey() {
		 return DatasourceHold.getDataSource();
	}

}
2.2 数据源key管理
package com.infrasys.dao.datasource;

import org.apache.commons.lang3.StringUtils;

/**
 * 用于保存数据源key
 * @author lr
 *
 */
public class DatasourceHold {
	
	 /**  注意:数据源标识保存在线程变量中,避免多线程操作数据源时互相干扰  */
     private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal<String>();
     
     // 返回ThreadLocal中数据源标识(key)
     public static String getDataSource() {
         String dataSource = THREAD_DATA_SOURCE.get();
         if(StringUtils.isEmpty(dataSource)){
             return DataSourceKey.DATA_SOURCE;
         }else{
             return dataSource;
         }
     }
     
     public static void setDataSource(String dataSource) {
         THREAD_DATA_SOURCE.set(dataSource);
     }
     
     // 清除key
     public static void clearDataSource() {
         THREAD_DATA_SOURCE.remove();
     }

}

2.3 自定义注解

创建一个注解,用于后期在目标方法上进行标识所需要的切换的数据源

package com.infrasys.dao.datasource;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSourceAnno {
	// 自定义的变量保存数据源的可以(要与配置文件中一致),也可以后面使用注解时直接写对应的key
	String HERO_POS = "pos";
	String HERO_MENU = "menu";
	String HERO_OUTLET = "outlet";

	String name();
}

3 切面类

这里通过AOP监控方法调用,进行数据源动态切换

package com.infrasys.dao.datasource;


import java.lang.reflect.Method;

import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DynamicDataSourceAspect1 {

	@Before("execution(public * com.infrasys.service.PosService.*(..))")
	public void before(JoinPoint point) {
		MethodSignature methodSignature = (MethodSignature) point.getSignature();
		// 获取目标方法对象
		Method method = methodSignature.getMethod();
		// 获取方法的DataSourceAnno注解对象
		DataSourceAnno annotation = method.getAnnotation(DataSourceAnno.class);
		if(annotation != null) {
			// 获取注解中的值并设置
			DatasourceHold.setDataSource(annotation.name());
		}else {
			DatasourceHold.clearDataSource();
		}
	}
	
	@Before("execution(public * com.infrasys.service.PosService.*(..))")
	public void after(JoinPoint point) {
		DatasourceHold.clearDataSource();
	}
}

4 测试

dao接口:

package com.infrasys.dao;

import java.util.Map;

import org.apache.ibatis.annotations.Param;

public interface PosDao {

	public Map<String,Object> getMenuItemDept(@Param("itemID") Integer itemID);
	
	public Map<String,Object> getShop(@Param("shopID") Integer shopID);
}

service:

package com.infrasys.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.infrasys.dao.PosDao;
import com.infrasys.dao.datasource.DataSourceAnno;

public class PosService {

	@Autowired
	private PosDao posDao;
	// 通过注解切换数据源
	@DataSourceAnno(name=DataSourceAnno.HERO_MENU)
	public void test2() {
		Map<String, Object> menuItemDept = posDao.getMenuItemDept(102);
		System.out.println(menuItemDept);
	}
	// 通过注解切换数据源
	@DataSourceAnno(name=DataSourceAnno.HERO_OUTLET)
	public void test1() {
		Map<String, Object> shop = posDao.getShop(1);
		System.out.println(shop);
	}
}

package com.infrasys.dao.datasource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.infrasys.service.PosService;

public class Main {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-bean.xml");
		PosService posService = context.getBean(PosService.class);
		
		posService.test1();
		posService.test2();
	}

}

输出:

{shop_id=1, shop_code=0001}
{item_code=90000203, item_id=102}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值