本文共 1071 字,大约阅读时间需要 3 分钟。
1.在spring配置文件中配置
2.继承AbstractRoutingDataSource
public class DBContextHolder { private static final ThreadLocalcontextHolder = new ThreadLocal (); public static void setDbType(String dbType) { contextHolder.set(dbType); } public static String getDbType() { return ((String) contextHolder.get()); } public static void clearDbType() { contextHolder.remove(); }}
public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DBContextHolder.getDbType(); }}
3.切换数据库
//指定数据库DBContextHolder.setDbType("dataSource1");
转载地址:http://mclyz.baihongyu.com/