spring +mybatis 多数据源

先建两个util类

package util;

import org.springframework.stereotype.Component;

@Component
public class DataSourceContextHolder {  
     private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  
  
     public static void setDbType(String dbType) {  
            contextHolder.set(dbType);  
     }  
  
     public static String getDbType() {  
            return ((String) contextHolder.get());  
     }  
  
     public static void clearDbType() {  
            contextHolder.remove();  
     }  
}  

  

package util;

import java.sql.SQLFeatureNotSupportedException;  
import java.util.logging.Logger;  
  
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;  
@Component
public class DynamicDataSource extends AbstractRoutingDataSource {  
  
     @Override  
     public Logger getParentLogger() {  
            return null;  
     }  
  
     @Override  
     protected Object determineCurrentLookupKey() {  
            return DataSourceContextHolder. getDbType();  
     }  
  
}  

  在controller中直接

datasourcetwo  与datasourceone 是在properties中配的数据源

原文地址:https://www.cnblogs.com/gaofangquan/p/8892025.html