欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring与Mybatis相结合实现多数据源切换功能

程序员文章站 2024-03-12 16:55:20
废话不多说,关键代码如下所示: 1. 代码: dbcontextholder public class dbcontextholder { //线程安全的...

废话不多说,关键代码如下所示:

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相结合实现多数据源切换功能,希望对大家有所帮助