加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

spring+mybatis 多数据源切换

发布时间:2020-12-15 00:29:14 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class DbContextHolder { //线程安全的ThreadLocal private static final ThreadLocalString contextHolder = new ThreadLocalString(); publi

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

public class DbContextHolder {
    //线程安全的ThreadLocal
    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();
    }

}

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    public Object determineCurrentLookupKey() {
        return  DbContextHolder.getDbType();
    }

}

<!-- 数据源属性配置文件 -->
    <context:property-placeholder location="classpath:ibatis.properties" />

    <bean id="jksh" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="${driver.jksh}" />
        <property name="url" value="${url.jksh}" />
        <property name="username" value="${username.jksh}" />
        <property name="password" value="${password.jksh}" />

        <!-- Connection Pooling Info -->
        <property name="maxIdle" value="${maxIdle.jksh}" />
        <property name="maxActive" value="${maxActive.jksh}" />
        <property name="defaultAutoCommit" value="false" />
        <property name="timeBetweenEvictionRunsMillis"
            value="${timeBetweenEvictionRunsMillis.jksh}" />
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis.jksh}" />
    </bean>

    <bean id="jclt" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="${driver.jclt}" />
        <property name="url" value="${url.jclt}" />
        <property name="username" value="${username.jclt}" />
        <property name="password" value="${password.jclt}" />

        <!-- Connection Pooling Info -->
        <property name="maxIdle" value="${maxIdle.jclt}" />
        <property name="maxActive" value="${maxActive.jclt}" />
        <property name="defaultAutoCommit" value="false" />
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis.jclt}" />
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis.jclt}" />
    </bean>

    <bean id="dataSource" class="com.jclt.service.commons.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry key="jksh" value-ref="jksh" />
                <entry key="jclt" value-ref="jclt" />
            </map>
        </property>
        <property name="defaultTargetDataSource" ref="jksh" />
    </bean>

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.jclt.service.commons.DbContextHolder;
import com.jclt.service.model.User;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;

public class Text {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("client-beans.xml");

        DbContextHolder.setDbType("jclt");
        String res="src/main/resources/ibatis-config.xml";
        DataSource datasource=(DataSource) appContext.getBean("dataSource");

        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        Resource resource=new FileSystemResource(res);
        bean.setConfigLocation(resource);
        try {
            SqlSessionFactory sessionfactory = bean.getObject();
            SqlSession session=sessionfactory.openSession();
            User user=session.selectOne("com.jclt.service.Dao.readJKSH.findOne");
            System.out.println(user.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DbContextHolder.setDbType("jksh");
        String res1="src/main/resources/ibatis-config.xml";
        DataSource datasource1=(DataSource) appContext.getBean("dataSource");

        SqlSessionFactoryBean bean1=new SqlSessionFactoryBean();
        bean1.setDataSource(datasource1);
        Resource resource1=new FileSystemResource(res1);
        bean1.setConfigLocation(resource1);

        try {
            SqlSessionFactory sessionfactory = bean.getObject();
            SqlSession session=sessionfactory.openSession();
            User user=session.selectOne("com.jclt.service.Dao.readJKSH.findOne");
            System.out.println(user.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读