美文网首页
5、mybatis动态切换数据源

5、mybatis动态切换数据源

作者: ltjxwxz | 来源:发表于2017-09-07 23:18 被阅读0次

需求:动态切换数据源
使用说明:
(1)重点是spring提供的AbstractRoutingDataSource,从它继承,重写方法determineCurrentLookupKey()
(2)向AbstractRoutingDataSource的子类属性targetDataSources注入自己的数据源,map注入。

applicationContext.xml 配置文件

    <bean id="dataSource" class="com.picc.autoTestingWeb.utils.dbutils.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry key="dataSourcePICCEBSS" value-ref="dataSourcePICCEBSS" />
                <entry key="dataSourcePICC"  value-ref="dataSourcePICC"  />
                <entry key="dataSourceAUTOTESTING"  value-ref="dataSourceAUTOTESTING"  />
                <entry key="dataSourceNEWELIFE"  value-ref="dataSourceNEWELIFE"  />
            </map>
        </property>
        <property name="defaultTargetDataSource" ref="dataSourceAUTOTESTING"/>
    </bean>

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocations">
            <list>
                <value>classpath:ibatis/SqlMapConfig.xml</value>
                <value>classpath:ibatis/EISqlMapConfig.xml</value>
            </list>
        </property>
    </bean>

    <bean id="baseDAOIbatis" class="com.picc.autoTestingWeb.utils.dbutils.BaseDAOIbatis">
        <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>

    <bean id="EIManagerSupport" class="com.picc.utils.dbUtils.ManagerSupport" abstract="true">
        <property name="baseDAOIbatis" ref="baseDAOIbatis" />
    </bean>
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DbContextHolder.getDbType();
    }
}
public class DbContextHolder {
    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();
    }
}
public class DataBaseType {
    // dataSourcePICCEBSS对应注入DynamicDataSource的map的key
    public static final String PICCEBSS = "dataSourcePICCEBSS";
    public static final String PICC = "dataSourcePICC";
    public static final String AUTOTESTING = "dataSourceAUTOTESTING";
    public static final String NEWELIFE = "dataSourceNEWELIFE";
}

使用:

DbContextHolder.setDbType(DataBaseType.AUTOTESTING);

好处
1、DataBaseType.PICCEBSS 等 对应注入DynamicDataSource的map的key,如果ThreadLocal是枚举类型,需要自己写代码注入
2、动态切换数据源避免创建大量的dao

public class ManagerSupport {
    @Resource(name="baseDAOIbatis")
    protected BaseDAO baseDAOIbatis;    
    
    public <T> T findCompare(Map<String, String> map) {
        return null;
    }
}
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;


public class BaseDAOIbatis extends SqlMapClientDaoSupport implements BaseDAO {
    
    public BaseDAOIbatis() {
    }

    @SuppressWarnings("unchecked")
    public List findObjectsByProperties(String statementId) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        return super.getSqlMapClientTemplate().queryForList(statementId);
    }

    @SuppressWarnings("unchecked")
    public List findObjectsByProperties(String statementId, Object object) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        return super.getSqlMapClientTemplate()
                .queryForList(statementId, object);
    }

    public Object findSingleObjectByProperties(String statementId) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        return super.getSqlMapClientTemplate().queryForObject(statementId);
    }

    public Object findSingleObjectByProperties(String statementId, Object object) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        return super.getSqlMapClientTemplate().queryForObject(statementId,
                object);
    }

    public Object findSingleObjectByProperties(String statementId,
            Object object, Object args) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        return super.getSqlMapClientTemplate().queryForObject(statementId,
                object, args);
    }

    @SuppressWarnings("unchecked")
    public List findObjectsByPage(String statementId, int pageNum, int pageSize) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        int startIndex = (pageNum - 1) * pageSize;
        return super.getSqlMapClientTemplate().queryForList(statementId,
                startIndex, pageSize);
    }

    @SuppressWarnings("unchecked")
    public List findObjectsByPage(String statementId, Object object,
            int pageNum, int pageSize) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        int startIndex = (pageNum - 1) * pageSize;
        return super.getSqlMapClientTemplate().queryForList(statementId,
                object, startIndex, pageSize);
    }

    public long getObjectTotal(String statementId) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        long total = 0;
        Object obj = super.getSqlMapClientTemplate()
                .queryForObject(statementId);
        if (obj != null)
            total = Long.valueOf(obj.toString());
        return total;
    }

    public long getObjectTotal(String statementId, Object args) {
        if (StringUtils.isBlank(statementId))
            throw new RuntimeException("StatementId is not allow null.");
        long total = 0;
        Object obj = super.getSqlMapClientTemplate().queryForObject(
                statementId, args);
        if (obj != null)
            total = Long.valueOf(obj.toString());
        return total;
    }

    public Long saveObject(Object object) {
        if (object == null)
            throw new RuntimeException("Object is not allow null.");
        String simpleName = object.getClass().getSimpleName();
        String statementId = "insert" + simpleName;
        return (Long) super.getSqlMapClientTemplate().insert(statementId,
                object);
    }

    public Long saveObject(String statementId, Object object) {
        if (object == null)
            throw new RuntimeException("Object is not allow null.");
        return (Long) super.getSqlMapClientTemplate().insert(statementId,
                object);
    }

    public int updateObject(Object object) {
        if (object == null)
            throw new RuntimeException("Object is not allow null.");
        String simpleName = object.getClass().getSimpleName();
        String statementId = "update" + simpleName;
        return super.getSqlMapClientTemplate().update(statementId, object);
    }

    public int updateObject(String statementId, Object object) {
        if (object == null)
            throw new RuntimeException("Object is not allow null.");
        return super.getSqlMapClientTemplate().update(statementId, object);
    }

    public int deleteObject(Object object) {
        if (object == null)
            throw new RuntimeException("Object is not allow null.");
        String simpleName = object.getClass().getSimpleName();
        String statementId = "delete" + simpleName;
        return super.getSqlMapClientTemplate().delete(statementId, object);
    }

    public int deleteObject(String statementId, Object object) {
        if (object == null)
            throw new RuntimeException("Object is not allow null.");
        return super.getSqlMapClientTemplate().delete(statementId, object);
    }

    @SuppressWarnings("unchecked")
    public Map execProcedure(String statementId, Object object,
            String keyProperty) {
        return super.getSqlMapClientTemplate().queryForMap(statementId, object,
                keyProperty);
    }

    public Object findObjectsByProperties(String statementId, Object object,
            Object args) {
        // TODO Auto-generated method stub
        return null;
    }
}

相关文章

网友评论

      本文标题:5、mybatis动态切换数据源

      本文链接:https://www.haomeiwen.com/subject/jovwjxtx.html