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中配的数据源

 

posted on 2018-04-20 16:51  superficial。  阅读(181)  评论(0编辑  收藏  举报

导航