Commit 36ac9a6b by naan1993

引入动态数据源

parent 155f263c
package com.stylefeng.guns.common.annotion;
import java.lang.annotation.*;
/**
*
* 多数据源标识
*
* @author fengshuonan
* @date 2017年3月5日 上午9:44:24
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface DataSource {
String name() default "";
}
package com.stylefeng.guns.common.constant;
/**
*
* 多数据源的枚举
*
* @author fengshuonan
* @date 2017年3月5日 上午10:15:02
*/
public interface DSEnum {
String DATA_SOURCE_GUNS = "dataSourceGuns"; //guns数据源
String DATA_SOURCE_BIZ = "dataSourceBiz"; //其他业务的数据源
}
...@@ -8,6 +8,7 @@ import org.mybatis.spring.annotation.MapperScan; ...@@ -8,6 +8,7 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/** /**
* MybatisPlus配置 * MybatisPlus配置
...@@ -16,6 +17,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -16,6 +17,7 @@ import org.springframework.context.annotation.Configuration;
* @Date 2017/5/20 21:58 * @Date 2017/5/20 21:58
*/ */
@Configuration @Configuration
@EnableTransactionManagement(order=2)//由于引入多数据源,所以让spring事务的aop要在多数据源切换aop的后面
@MapperScan(basePackages = {"com.stylefeng.guns.modular.*.dao", "com.stylefeng.guns.common.persistence.dao"}) @MapperScan(basePackages = {"com.stylefeng.guns.modular.*.dao", "com.stylefeng.guns.common.persistence.dao"})
public class MybatisPlusConfig { public class MybatisPlusConfig {
......
package com.stylefeng.guns.core.aop;
import com.stylefeng.guns.common.annotion.DataSource;
import com.stylefeng.guns.common.constant.DSEnum;
import com.stylefeng.guns.core.mutidatesource.DataSourceContextHolder;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
*
* 多数据源切换的aop
*
* @author fengshuonan
* @date 2017年3月5日 上午10:22:16
*/
@Aspect
@Component
public class MultiSourceExAop implements Ordered {
private Logger log = Logger.getLogger(this.getClass());
@Pointcut(value = "@annotation(com.stylefeng.guns.common.annotion.DataSource)")
private void cut() {
}
@Around("cut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
Signature signature = point.getSignature();
MethodSignature methodSignature = null;
if (!(signature instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
methodSignature = (MethodSignature) signature;
Object target = point.getTarget();
Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
DataSource datasource = currentMethod.getAnnotation(DataSource.class);
if(datasource != null){
DataSourceContextHolder.setDataSourceType(datasource.name());
log.debug("设置数据源为:" + datasource.name());
}else{
DataSourceContextHolder.setDataSourceType(DSEnum.DATA_SOURCE_GUNS);
log.debug("设置数据源为:dataSourceCurrent");
}
try {
return point.proceed();
} finally {
log.debug("清空数据源信息!");
DataSourceContextHolder.clearDataSourceType();
}
}
/**
* aop的顺序要早于spring的事务
*/
@Override
public int getOrder() {
return 1;
}
}
package com.stylefeng.guns.core.mutidatesource;
/**
*
* datasource的上下文
*
* @author fengshuonan
* @date 2017年3月5日 上午9:10:58
*/
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
/**
* @Description: 设置数据源类型
* @param dataSourceType 数据库类型
*/
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
/**
* @Description: 获取数据源类型
*/
public static String getDataSourceType() {
return contextHolder.get();
}
/**
* @Description: 清除数据源类型
*/
public static void clearDataSourceType() {
contextHolder.remove();
}
}
package com.stylefeng.guns.core.mutidatesource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
*
* 动态数据源
*
* @author fengshuonan
* @date 2017年3月5日 上午9:11:49
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSourceType();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment