Spring中的多数据源和@Transactional事务配置

基础Dao/Mapper定义
public interface BaseMapper {
	// read
	public Entity get(Integer id);
	
}

public interface JdbuyBaseMapper extends BaseMapper{

}

public interface LifecycleBaseMapper{

}

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

public class MultiDataSource extends AbstractRoutingDataSource {

    public static void setDataSourceKey(String dataSource) {
    	MultiDataSourceHolder.setDataSource(dataSource);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return MultiDataSourceHolder.getDataSource();
    }
}
多数据源AOP,动态切换数据源

/**
 * 多数据源AOP,动态切换数据源
 * 
 *
 */
@Aspect
public class MultiDataSourceAspectAdvice {

	private static final String JDBUY_DATA_SOURCE = "jdbuyDataSource";
	private static final String LIFECYCLE_DATA_SOURCE = "lifecycleDataSource";

	// @Pointcut("execution(* lifecycle.dao.*.*(..))")
	// 切入点路径配置很关键,dao包下的所有子包、所有类、所有方法,".."所有参数
	@Pointcut("execution(* lifecycle.dao.*.*.*(..))")
	protected void pointcut() {
	}

	// @Around("execution(* lifecycle.*.*(..))")
	@Around(value = "pointcut()")
	public Object doAround(ProceedingJoinPoint jp) throws Throwable {
		Object target = jp.getTarget();
		if (target instanceof LifecycleBaseMapper) {
			MultiDataSource.setDataSourceKey(LIFECYCLE_DATA_SOURCE);
		} else if (target instanceof JdbuyBaseMapper) {
			MultiDataSource.setDataSourceKey(JDBUY_DATA_SOURCE);
		}
		return jp.proceed();
	}
}

public class MultiDataSourceHolder {
	/**
	 * 注意:数据源标识保存在线程变量中,避免多线程操作数据源时互相干扰
	 */
	private static final ThreadLocal dataSourceHolder = new ThreadLocal();

	public static String getDataSource() {
		return dataSourceHolder.get();
	}

	public static void setDataSource(String dataSource) {
		dataSourceHolder.set(dataSource);
	}

	public static void clearDataSource() {
		dataSourceHolder.remove();
	}

}

普通的Mapper,不同的数据源继承不同的Mapper

@Repository
public interface BidProjectMapper extends JdbuyBaseMapper {

//---------------------read-------------------------
List listByProjectCodeList(@Param("projectCodeList")List projectCodeList);


//---------------------write-------------------------


}

@Repository
public interface DataDictionaryMapper extends LifecycleBaseMapper {


// ---------------------read-------------------------


DataDictionary getBySourceId(@Param("sourceId") Integer sourceId, @Param("type") Integer type);
// ---------------------write-------------------------


}

Spring多数据源和事务配置




	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	
	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	
	
	
        
        
            
                
                
            
        
    
    
    
	
		
		  
	

	

	
		
		  
	

	
	
		
	
	
		
		
		
		
			classpath:mybatis/mapper/**/*Mapper.xml
		
	

	
	
		
		
	

	
		
	
	

选择使用某个事务

public class TaskCheckService {
	
	@Resource
	private TaskConfigMapper configMapper;
	
	//需要指定使用的事务标记,2个数据源
	@Transactional("lifecycle")
	public boolean startTask(String key,Long timeout) {
	
	}
}

你可能感兴趣的:(工作问题)