Maven下SSM项目整合笔记04:使用测试类进行数据库增删改查的测试

1.搭建Spring测试环境
- 在spring配置里面加入mapper的自动扫描,能从Spring中拿到mapper:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <!-- 扫描所有的dao接口的实现,加入到Ioc容器中 -->
    <property name="basePackage" value="com.zr.crud.dao" />
</bean> 
  • Maven里面导入Spring单元测试模块:
<!-- 导入Spring的测试模块 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.7.RELEASE</version>
</dependency>
  • 测试类添加注解指定配置文件的位置,能够自动创建Ioc容器:
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
  • 使用Junit的RunWith注解,运行单元测试时指定测试模块:
@RunWith(SpringJUnit4ClassRunner.class)
  • 直接使用@Autowired要使用的组件即可。

2.使用sqlsession批量处理数据:
- 在配置文件中添加sqlsession,传入sqlSessionFactory与批量的处理类型:

<!-- 配置一个可以批量查询的sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
    <constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
  • 之后注入sqlsession:
    @Autowired
    SqlSession sqlSession;

测试类代码:

package com.zr.crud.test;

import java.util.List;
import java.util.Random;
import java.util.UUID;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.zr.crud.bean.Department;
import com.zr.crud.bean.DepartmentExample;
import com.zr.crud.bean.Employee;
import com.zr.crud.dao.DepartmentMapper;
import com.zr.crud.dao.EmployeeMapper;

/**
 * 测试dao层的工作 Spring的项目推荐使用Spring的单元测试,可以自动注入我们需要的组件 1.在pom.xml中导入SpringTest的依赖
 * 2.使用注解@ContextConfiguration指定Spring配置文件的位置 3.直接autowired要使用的组件即可
 * 
 * @author asus
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class MapperTest {

    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    // 用于进行批量处理
    @Autowired
    SqlSession sqlSession;

    @Test
    public void insertEmp() {
        EmployeeMapper e_mapper = sqlSession.getMapper(EmployeeMapper.class);

        for (int i = 0; i < 1000; i++) {
            String uid = UUID.randomUUID().toString().substring(0, 5) + i;
            e_mapper.insertSelective(new Employee(null, uid, "M", uid + "@qwe.com", 1));
        }

        System.out.println("批量处理完成。");
    }
}

这里写图片描述
成功插入完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值