上一节通过mybatis-generator自动生成了CategoryMapper接口,pojo等类,接下来我们写几个简单的测试来进行调用。
一、添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
二、测试SqlSessionFactroy
首先我们看下怎样实例化sqlSessionFactory,然后获取CategoryMapper。
1.新建文件resources/properties/config.property,用来保存数据库连接串和账号。
data_source_url=jdbc:mysql://localhost:3306/store
data_source_username=root
data_source_password=root
2.在resources/spring目录新建spring的配置文件applicationContext.xml
3.配置数据连接。 在applicationContext.xml中添加bean节点dataSource,详细配置参考:http://www.oschina.net/p/druid
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${data_source_url}"/>
<property name="username" value="${data_source_username}"/>
<property name="password" value="${data_source_password}"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="1"/>
<!-- 初始化连接池最大使用连接数量 -->
<property name="maxActive" value="20"/>
<!-- 初始化连接池最小空闲 -->
<property name="minIdle" value="1"/>
<!-- 获取连接最大等待时间,单位毫秒-->
<property name="maxWait" value="60000"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000"/>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<!--如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。-->
<property name="poolPreparedStatements" value="false"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true"/>
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800"/>
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true"/>
</bean>
4.然后配置bean sqlSessionFactory和sqlSession。
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.data.pojo"/>
<property name="mapperLocations" value="classpath*:mapper/CategoryMapper.xml"></property>
<property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
</bean>
4.测试
有了上面的配置,就可以直接以注解的方式实例化SqlSessionFactory.
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SqlSessionTests {
@Autowired
private SqlSession sqlSession; @Test
public void testSqlSession(){
CategoryMapper categoryMapper= sqlSession.getMapper(CategoryMapper.class);
Category category= categoryMapper.selectByPrimaryKey(1);
if(category==null){
System.out.println("Do not exist");
}
else{
System.out.println(category.getName());
}
}
}
三、测试直接实例化CategoryMapper
更加简洁的方式是直接通过注解实例化CategoryMapper
1.在applicationContext.xml新增bean节点
<!-- scan for mappers and let them be autowired -->
<mybatis:scan base-package="com.data.mapper"/>
<!-- mapper 接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.data.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
2.直接给CategoryMapper添加@Resource注解,来进行实例化。
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class CategoryDaoTests {
@Resource
CategoryMapper categoryMapper; @Test
public void test_selectById(){
Category category=categoryMapper.selectByPrimaryKey(1);
System.out.println(category.getName());//Fish
} @Test
public void test_count(){
CategoryExample example=new CategoryExample();
long result=categoryMapper.countByExample(example);
System.out.println(result);
} @Test
public void test_insert(){
System.out.println("before insert:count "+categoryMapper.countByExample(new CategoryExample())); Category category=new Category();
category.setName("Test");
categoryMapper.insert(category); System.out.println("after insert:count "+categoryMapper.countByExample(new CategoryExample())); } @Test
public void test_delete(){
System.out.println("before insert:count "+categoryMapper.countByExample(new CategoryExample())); CategoryExample example=new CategoryExample();
example.createCriteria().andNameEqualTo("Test");
int result=categoryMapper.deleteByExample(example);
System.out.println("删除条数:"+result); System.out.println("after insert:count "+categoryMapper.countByExample(new CategoryExample())); }
}