Home > Java > Java Tutorial > body text

Spring and Mybatis are combined to realize multi-data source switching function

高洛峰
Release: 2017-01-24 10:17:27
Original
1119 people have browsed it

Without further ado, the key code is as follows:

1. Code: 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();
}
}
Copy after login

2. Code: DynamicDataSource

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

3. Code: spring.xml





































Copy after login

4. Code: main method

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

The above is the combination of Spring and Mybatis introduced by the editor to implement the multi-data source switching function. I hope it will be helpful to everyone. If you have any questions, please let me know. Leave a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more related articles on the combination of Spring and Mybatis to achieve multi-data source switching function, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!