码迷,mamicode.com
首页 > 编程语言 > 详细

spring+mybatis 多数据源切换

时间:2014-05-26 22:22:46      阅读:400      评论:0      收藏:0      [点我收藏+]

标签:des   style   c   class   blog   code   

摘自: http://www.oschina.net/code/snippet_347813_12525

 

 

1. 代码: DbContextHolder

bubuko.com,布布扣
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();
    }

}
bubuko.com,布布扣

2. 代码 : DynamicDataSource

bubuko.com,布布扣
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

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

}
bubuko.com,布布扣

 

3.代码: spring.xml  

bubuko.com,布布扣
<!-- 数据源属性配置文件 -->
    <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>
bubuko.com,布布扣

 

4. 代码:main方法 

bubuko.com,布布扣
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();
        }




    }

}
bubuko.com,布布扣

 

 

spring+mybatis 多数据源切换,布布扣,bubuko.com

spring+mybatis 多数据源切换

标签:des   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/wuyifu/p/3745234.html

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