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

网友投稿 203 2023-07-13


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

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

1. 代码: DbContextHolder

public class DbContextHolder {

//线程安全的ThreadLocal

private static final ThreadLocal contextHolder = new ThreadLocal();

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

destroy-method="close"&gWdchCaIJCBt;

value="${timeBetweenEvictionRunsMillis.jksh}" />

destroy-method="close"&gWdchCaIJCBt;

value="${timeBetweenEvictionRunsMillis.jksh}" />

value="${timeBetweenEvictionRunsMillis.jksh}" />

destroy-method="close">

destroy-method="close">

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相结合实现多数据源切换功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java程序开发中abstract 和 interface的区别详解
下一篇:用Java程序判断是否是闰年的简单实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~