不多说,配置如下:
1:引入关键jar包:
mybatis-spring 1.2.0,mybatis 3.2.0+、mysql-connector-java 5.1.27
2:配置jdbc.properties文件,配置如下:
3:Dao层目录结构如下:
4:mybatis下相关配置:
5:关键配置spring-database.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:conf/jobjdbc.properties"/>
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.db1.driverClassName}" />
<property name="url" value="${jdbc.db1.url}" />
<property name="username" value="${jdbc.db1.username}" />
<property name="password" value="${jdbc.db1.password}" />
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="100"></property>
<property name="maxWait" value="1000"></property>
<property name="timeBetweenEvictionRunsMillis" value="5000"></property>
<property name="testWhileIdle" value="true"></property>
<property name="testOnBorrow" value="true"></property>
<property name="testOnReturn" value="true"></property>
<property name="validationQuery">
<value>select 1</value>
</property>
</bean>
<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype">
<property name="dataSource" ref="dataSource1" />
<property name="mapperLocations" value="classpath:mybatis/student.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.myspring.dao.datasource1.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/>
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.db2.driverClassName}" />
<property name="url" value="${jdbc.db2.url}" />
<property name="username" value="${jdbc.db2.username}" />
<property name="password" value="${jdbc.db2.password}" />
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="100"></property>
<property name="maxWait" value="100"></property>
<property name="timeBetweenEvictionRunsMillis" value="5000"></property>
<property name="testWhileIdle" value="true"></property>
<property name="testOnBorrow" value="true"></property>
<property name="testOnReturn" value="true"></property>
<property name="validationQuery">
<value>select 1</value>
</property>
</bean>
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype">
<property name="dataSource" ref="dataSource2" />
<property name="mapperLocations" value="classpath:mybatis/user.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.myspring.dao.datasource2.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
</bean>
</beans>
说明:针对不同数据源,采用不同的SqlSessionFaction进行处理,2套配置分开。
6:单元测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-database.xml")
public class TestDataSource {
@Resource
private StudentDao studentDao;
@Resource
private UserDao userDao;
@Test
public void test0(){
System.out.println("Student size = "+studentDao.getStudentCount());
System.out.println("User size = "+userDao.getUserCount());
}
}
7:单元测试结果:
更多深入讲解参考:http://www.mybatis.org/spring/zh/factorybean.html