首先在spring配置proxool数据源这些不说, 我们先在src/main/resources/spring-config/下建立spring-mybatis.xml.把sqlSession放在spring容器里管理:
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
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-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:test/mybabitsmapping/*.xml" />
</bean>
<!-- 配置扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="test.ssm.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
</beans>
现在测试一下, 用mybatis生成工具生成如下文档:
开一个junit 测试,因为这个是脱离web环境的测试,所以之前需要手动加载proxool的连接池:
package test.testmybatis;
import java.io.File;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.ssm.mapper.TaUserMapper;
import test.ssm.model.TaUser;
public class Test1 {
ApplicationContext context ;
public void loadProxool() throws ProxoolException{
try {
// 获取配置文件句柄
String lvsPath=Class.class.getClass().getResource("/").getPath() ;
lvsPath=lvsPath+"../../src/main/webapp/WEB-INF/proxool.xml";
File file = new File(lvsPath);
//单元测试未启动web service,所以要自已加载proxool configurator
JAXPConfigurator.configure(lvsPath, false);
} catch (ProxoolException e) {
throw e;
}
}
@Before
public void setUp() throws Exception {
loadProxool();
context =new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@After
public void tearDown() throws Exception {
ProxoolFacade.shutdown(0);
}
@Test
public void test() {
org.mybatis.spring.SqlSessionTemplate lvSqlSession=(SqlSessionTemplate) context.getBean("sqlSession");
TaUserMapper lvUserMapper= lvSqlSession.getMapper(TaUserMapper.class);
TaUser lvUserItem=lvUserMapper.selectByPrimaryKey("admin");//update from desktop
Assert.assertNotNull(lvUserItem);
}
}