Spring與Mybatis相結(jié)合實(shí)現(xiàn)多數(shù)據(jù)源切換功能
廢話不多說,關(guān)鍵代碼如下所示:
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
<!-- 數(shù)據(jù)源屬性配置文件 -->
<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相結(jié)合實(shí)現(xiàn)多數(shù)據(jù)源切換功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Spring+Mybatis動(dòng)態(tài)切換數(shù)據(jù)源的方法
- spring boot+mybatis 多數(shù)據(jù)源切換(實(shí)例講解)
- Spring + Mybatis 項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例詳解
- 關(guān)于Spring3 + Mybatis3整合時(shí)多數(shù)據(jù)源動(dòng)態(tài)切換的問題
- SpringMVC Mybatis配置多個(gè)數(shù)據(jù)源并切換代碼詳解
- spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例代碼
- SpringBoot Mybatis動(dòng)態(tài)數(shù)據(jù)源切換方案實(shí)現(xiàn)過程
- Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼
- Spring AOP如何實(shí)現(xiàn)注解式的Mybatis多數(shù)據(jù)源切換詳解
- mybatis多數(shù)據(jù)源動(dòng)態(tài)切換的完整步驟
相關(guān)文章
使用springboot單元測(cè)試對(duì)weblistener的加載測(cè)試
這篇文章主要介紹了使用springboot單元測(cè)試對(duì)weblistener的加載測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
基于@RestControllerAdvice與@ControllerAdvice的區(qū)別說明
這篇文章主要介紹了@RestControllerAdvice與@ControllerAdvice的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
詳解Spring Security的Web應(yīng)用和指紋登錄實(shí)踐
這篇文章主要介紹了詳解Spring Security的Web應(yīng)用和指紋登錄實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
JAVA中使用FTPClient實(shí)現(xiàn)文件上傳下載實(shí)例代碼
本文給大家介紹如何利用jakarta commons中的FTPClient(在commons-net包中)實(shí)現(xiàn)上傳下載文件。非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(27)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
Java泛型T,E,K,V,N,?與Object區(qū)別和含義
Java?泛型(generics)是?JDK?5?中引入的一個(gè)新特性,?泛型提供了編譯時(shí)類型安全檢測(cè)機(jī)制,該機(jī)制允許程序員在編譯時(shí)檢測(cè)到非法的類型。本文將詳細(xì)講講Java泛型T、E、K、V、N、?和Object區(qū)別和含義,需要發(fā)可以參考一下2022-03-03

