参考《spring 集成mybatis》,并进行修改。
一、新建一个数据库kad,然后新建用户表s_user
CREATE TABLE s_user(
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(30),
user_birthday DATE,
user_salary DOUBLE
);
INSERT INTO `s_user` (`user_id`, `user_name`, `user_birthday`, `user_salary`) VALUES('2','marry','2015-04-18','300');
项目目录如下:
三、动态选择数据源
public class DataSourceContextHolder {
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();
}
}
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDbType();
}
}
四、beans.xml
这里用DynamicDataSource管理两个数据源ds1和ds2,后面我们可以在程序中动态选用其中一个数据源。
五、测试类
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.domain.User;
import com.mapper.UserMapper;
import com.utils.DataSourceContextHolder;
@RunWith(SpringJUnit4ClassRunner.class)
// 使用Springtest框架
@ContextConfiguration("/beans.xml")
// 加载配置
public class MybatisSpringTest {
@Autowired
// 注入
private UserMapper userMapper;
@Test
public void findByIdDs1() {
DataSourceContextHolder.setDbType("ds1"); //注意这里在调用userMapper前切换到ds1的数据源
User user = userMapper.findById(2);
System.out.println(user);
assertEquals("marry", user.getName());
}
@Test
public void findByIdDs2() {
DataSourceContextHolder.setDbType("ds2"); //注意这里在调用userMapper前切换到ds2的数据源
User user = userMapper.findById(1);
System.out.println(user);
assertEquals("marry", user.getName());
}
}
2015-05-09 18:34:42,859 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2015-05-09 18:34:42,875 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1e87512] was not registered for synchronization because synchronization is not active
2015-05-09 18:34:42,953 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2015-05-09 18:34:42,953 [main] DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/kad]
2015-05-09 18:34:42,968 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mysql.jdbc.JDBC4Connection@1dd40f3] will not be managed by Spring
2015-05-09 18:34:42,984 [main] DEBUG [com.mapper.UserMapper.findById] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@1dd40f3]
2015-05-09 18:34:42,984 [main] DEBUG [com.mapper.UserMapper.findById] - ==> Preparing: select * from s_user where user_id = ?
2015-05-09 18:34:43,109 [main] DEBUG [com.mapper.UserMapper.findById] - ==> Parameters: 2(Integer)
2015-05-09 18:34:43,156 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1e87512]
2015-05-09 18:34:43,156 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
User [id=2, name=marry, birthday=Sat Apr 18 00:00:00 CST 2015, salary=300.0]
2015-05-09 18:34:43,171 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
2015-05-09 18:34:43,171 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1245e75] was not registered for synchronization because synchronization is not active
2015-05-09 18:34:43,171 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource
2015-05-09 18:34:43,171 [main] DEBUG [org.springframework.jdbc.datasource.DriverManagerDataSource] - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/mybatis]
2015-05-09 18:34:43,187 [main] DEBUG [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mysql.jdbc.JDBC4Connection@13c2ac3] will not be managed by Spring
2015-05-09 18:34:43,187 [main] DEBUG [com.mapper.UserMapper.findById] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@13c2ac3]
2015-05-09 18:34:43,187 [main] DEBUG [com.mapper.UserMapper.findById] - ==> Preparing: select * from s_user where user_id = ?
2015-05-09 18:34:43,187 [main] DEBUG [com.mapper.UserMapper.findById] - ==> Parameters: 1(Integer)
2015-05-09 18:34:43,187 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1245e75]
2015-05-09 18:34:43,187 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
User [id=1, name=marry, birthday=Sat Apr 18 00:00:00 CST 2015, salary=2000.0]
这样,我们可以看到在程序里可以切换不同的数据源。