多数据源之动态数据源

import java.util.HashMap;
import java.util.Map;

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

public class DynamicDataSource extends AbstractRoutingDataSource  {
    
    
	
	public static ThreadLocal<String> key = new ThreadLocal<String>();
	
	private Map<Object, Object> copyTargets = new HashMap<Object, Object>();
	
	@Override
	protected Object determineCurrentLookupKey() {
    
    
		return key.get();
	}
	
	
	public void setCopyTargets(Map<Object, Object> copyTargets) {
    
    
		this.copyTargets = copyTargets;
		setTargetDataSources(copyTargets);
		afterPropertiesSet();
	}
	
	
	public Map<Object, Object> getCopyTargets() {
    
    
		return copyTargets;
	}

	
	
}

猜你喜欢

转载自blog.csdn.net/dmw412724/article/details/120473812