程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Spring與Mybatis相聯合完成多半據源切換功效

Spring與Mybatis相聯合完成多半據源切換功效

編輯:關於JAVA

Spring與Mybatis相聯合完成多半據源切換功效。本站提示廣大學習愛好者:(Spring與Mybatis相聯合完成多半據源切換功效)文章只能為提供參考,不一定能成為您想要的結果。以下是Spring與Mybatis相聯合完成多半據源切換功效正文


空話不多說,症結代碼以下所示:

1. 代碼: DbContextHolder

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();
}
} 

2. 代碼 : DynamicDataSource

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
public Object determineCurrentLookupKey() {
return DbContextHolder.getDbType();
}
} 

3.代碼: spring.xml

<!-- 數據源屬性設置裝備擺設文件 -->
<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> 

4. 代碼:main辦法

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();
}
}
}

以上所述是小編給年夜家引見的Spring與Mybatis相聯合完成多半據源切換功效,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved