码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis 连接多个库

时间:2020-09-17 17:27:26      阅读:27      评论:0      收藏:0      [点我收藏+]

标签:ref   mysql   targe   sql   key   comm   art   unicode   ted   

1.修改数据库配置database.properties文件

jdbc.url=jdbc:mysql://localhost:3306/hippo?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver


a.jdbc.url=jdbc:mysql://localhost:3306/hippo_auth?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
a.jdbc.username=root
a.jdbc.password=root
a.jdbc.driver=com.mysql.jdbc.Driver

2. 修改mybatis配置 applicationContext.xml文件

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${a.jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>

  <bean id="aDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10"/> <property name="maxIdle" value="5"/> </bean> <!-- 动态数据库,**.**.**.DatabaseContextHolder 屏蔽了包名,为具体选择数据库的类 --> <bean id="dataSource" class="**.**.**.DatabaseContextHolder"> <property name="defaultTargetDataSource" ref="sourceDataSource"/> <property name="targetDataSources"> <map> <entry key="sourceDataSource" value-ref="sourceDataSource"/> <entry key="antennaparaDataSource" value-ref="antennaparaDataSource"/> <entry key="tsysDataSource" value-ref="tsysDataSource"/> </map> </property> </bean>

3.创建类DbcontextHolder

public class DbcontextHolder extends AbstractRoutingDataSource {

    public static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

    /**
     * 设置当前数据源
     * @param dbType
     */
    public static void setDbType(String dbType){
        contextHolder.set(dbType);
    }
    /**
     * 获得当前数据源
     * @return
     */
    public static String getDbType(){
        String dbType = (String)contextHolder.get();
        return dbType;
    }
    /**
     *清除上下文
     *
     */
    public void clearContext(){
        contextHolder.remove();
    }
    @Override
    protected Object determineCurrentLookupKey() {
        return DbcontextHolder.getDbType();
    }
}

4.在方法中连接不同数据库

//连接dataSourceOld进行查询
DbcontextHolder.setDbType("dataSource");
Template template = templateMapperExtend.selectByPrimaryKey(templateId);
//连接dataSourceNew进行插入
DbcontextHolder.setDbType("aDataSource");
siteAsyncMapper.insertSelective(siteAsync);

参考

https://blog.csdn.net/u010716879/article/details/78459529

mybatis 连接多个库

标签:ref   mysql   targe   sql   key   comm   art   unicode   ted   

原文地址:https://www.cnblogs.com/lulalaklkl/p/13627471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!