`

spring + mybatis 多数据源切换

阅读更多

[代码] DbContextHolder

01 public class DbContextHolder {
02     //线程安全的ThreadLocal
03     private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
04       
05     public static void setDbType(String dbType) {
06         contextHolder.set(dbType);
07     }
08   
09     public static String getDbType() {
10         return ((String)contextHolder.get());
11     }
12     public static void clearDbType() {
13         contextHolder.remove();
14     }
15   
16 }

[代码] DynamicDataSource

01 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
02   
03 public class DynamicDataSource extends AbstractRoutingDataSource {
04   
05     @Override
06     public Object determineCurrentLookupKey() {
07         return  DbContextHolder.getDbType();
08     }
09   
10 }

[代码] spring.xml

01 <!-- 数据源属性配置文件 -->
02     <context:property-placeholder location="classpath:ibatis.properties" />
03   
04   
05     <bean id="jksh" class="org.apache.commons.dbcp.BasicDataSource"
06         destroy-method="close">
07         <!-- Connection Info -->
08         <property name="driverClassName" value="${driver.jksh}" />
09         <property name="url" value="${url.jksh}" />
10         <property name="username" value="${username.jksh}" />
11         <property name="password" value="${password.jksh}" />
12   
13         <!-- Connection Pooling Info -->
14         <property name="maxIdle" value="${maxIdle.jksh}" />
15         <property name="maxActive" value="${maxActive.jksh}" />
16         <property name="defaultAutoCommit" value="false" />
17         <property name="timeBetweenEvictionRunsMillis"
18             value="${timeBetweenEvictionRunsMillis.jksh}" />
19         <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis.jksh}" />
20     </bean>
21   
22     <bean id="jclt" class="org.apache.commons.dbcp.BasicDataSource"
23         destroy-method="close">
24         <!-- Connection Info -->
25         <property name="driverClassName" value="${driver.jclt}" />
26         <property name="url" value="${url.jclt}" />
27         <property name="username" value="${username.jclt}" />
28         <property name="password" value="${password.jclt}" />
29   
30         <!-- Connection Pooling Info -->
31         <property name="maxIdle" value="${maxIdle.jclt}" />
32         <property name="maxActive" value="${maxActive.jclt}" />
33         <property name="defaultAutoCommit" value="false" />
34         <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis.jclt}" />
35         <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis.jclt}" />
36     </bean>
37   
38   
39     <bean id="dataSource" class="com.jclt.service.commons.DynamicDataSource">
40         <property name="targetDataSources">
41             <map key-type="java.lang.String">
42                 <entry key="jksh" value-ref="jksh" />
43                 <entry key="jclt" value-ref="jclt" />
44             </map>
45         </property>
46         <property name="defaultTargetDataSource" ref="jksh" />
47     </bean>

[代码] main方法

01 import javax.sql.DataSource;
02   
03 import org.springframework.context.ApplicationContext;
04 import org.springframework.context.support.ClassPathXmlApplicationContext; 
05 import org.springframework.core.io.FileSystemResource;
06 import org.springframework.core.io.Resource;
07   
08 import com.jclt.service.commons.DbContextHolder;
09 import com.jclt.service.model.User;
10   
11 import org.apache.ibatis.session.SqlSession;
12 import org.apache.ibatis.session.SqlSessionFactory;
13 import org.mybatis.spring.SqlSessionFactoryBean;
14   
15 public class Text {
16   
17     /**
18      * @param args
19      */
20     public static void main(String[] args) {
21         ApplicationContext appContext = new ClassPathXmlApplicationContext("client-beans.xml");
22           
23         DbContextHolder.setDbType("jclt");
24         String res="src/main/resources/ibatis-config.xml";
25         DataSource datasource=(DataSource) appContext.getBean("dataSource");
26               
27         SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
28         bean.setDataSource(datasource);
29         Resource resource=new FileSystemResource(res);
30         bean.setConfigLocation(resource);
31         try {
32             SqlSessionFactory sessionfactory = bean.getObject();
33             SqlSession session=sessionfactory.openSession();
34             User user=session.selectOne("com.jclt.service.Dao.readJKSH.findOne");
35             System.out.println(user.getName());
36         } catch (Exception e) {
37             e.printStackTrace();
38         }
39   
40         DbContextHolder.setDbType("jksh");
41         String res1="src/main/resources/ibatis-config.xml";
42         DataSource datasource1=(DataSource) appContext.getBean("dataSource");
43   
44         SqlSessionFactoryBean bean1=new SqlSessionFactoryBean();
45         bean1.setDataSource(datasource1);
46         Resource resource1=new FileSystemResource(res1);
47         bean1.setConfigLocation(resource1);
48   
49         try {
50             SqlSessionFactory sessionfactory = bean.getObject();
51             SqlSession session=sessionfactory.openSession();
52             User user=session.selectOne("com.jclt.service.Dao.readJKSH.findOne");
53             System.out.println(user.getName());
54         } catch (Exception e) {
55             e.printStackTrace();
56         }
57   
58   
59   
60   
61     }
62   
63 }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics