SSM高级整合-crud 之五 修改mapper文件和搭建Spring单元mapper测试

备注:本篇文章是链接

https://blog.csdn.net/zaq977684/article/details/121404127

SSM高级整合-crud 之五内容

一、自动生成的mapper文件并不能满足我们要求时,需修改mapper文件以达到我们想要的查询操作等

1.在自动生成的EmployeeMapper.xml文件下,查询员工同时带部门信息,添加的代码如下:

<resultMap id="BaseResultMapWithDept" type="com.atguigu.crud.bean.Employee">
    <id column="emp_id" jdbcType="INTEGER" property="empId" />
    <result column="emp_name" jdbcType="VARCHAR" property="empName" />
    <result column="gender" jdbcType="CHAR" property="gender" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="d_id" jdbcType="INTEGER" property="dId" />
    <association property="department" javaType="com.atguigu.crud.bean.Department">
      <id column="dept_id" jdbcType="INTEGER" property="deptId"/>
      <result column="dept_name" jdbcType="VARCHAR" property="deptName" />
    </association>
  </resultMap>

  <!--查询员工同时带部门信息-->
  <select id="selectAllWithDept" resultMap="BaseResultMapWithDept">
    select emp.emp_id, emp.emp_name, emp.gender, emp.email, emp.d_id,dept.dept_id,dept.dept_name
    from tbl_emp emp
    left join tnl_dept dept
    on emp.d_id=dept.dept_id
    order by emp_id
  </select>

二、Spring单元测试,测mapper是否可用

 推荐Spring的项目就可以使用Spring的单元测试,可以自动注入我们需要的组件

1.导入SpringTest模块
在pom.xml加入依赖包
        <!--Spring模块测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>compile</scope>
        </dependency>
2.@ContextConfiguration指定Spring配置文件的
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
3.直接autowired要使用的组件即可,如:
@Autowired
DepartmentMapper departmentMapper;
4.插入数据方法,执行单元测试成功后,可返回数据库查看可看到插入的数据,说明mapper测试成功,批量插入需要在Spring配置文件applicationContext.xml中配置一个可以执行批量的sqlSession
<!--==========配置一个可以执行批量的sqlSession==========-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
   <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
   <constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
package com.atzaq.crud.test;
import com.atzaq.crud.bean.Department;
import com.atzaq.crud.bean.Employee;
import com.atzaq.crud.dao.DepartmentMapper;
import com.atzaq.crud.dao.EmployeeMapper;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.UUID;

/*
 *Date:2021/11/22 9:55
 *Author:angle
 *Des:测试dao层的工作
 * 推荐Spring的项目就可以使用Spring的单元测试,可以自动注入我们需要的组件
 * 1.导入SpringTest模块
 * 2.@ContextConfiguration指定Spring配置文件的位置
 * 3.直接autowired要使用的组件即可
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MapperTest {

    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    SqlSession sqlSession;

    /**
     * 测试DepartmentMapper
     */
    @Test
    public void testCRUD(){
        //2.插入几个部门
        departmentMapper.insert(new Department(null,"开发部"));
        departmentMapper.insert(new Department(null,"测试部"));
        departmentMapper.insert(new Department(null,"产品部"));
        departmentMapper.insert(new Department(null,"行政部"));
        //插入员工
        employeeMapper.insert(new Employee(null,"张三","M","zhangsan@zaq.com",1));
        //3.批量插入多个员工
        EmployeeMapper mapper=sqlSession.getMapper(EmployeeMapper.class);
        for (int i = 0; i <1000 ; i++) {
            String uid= UUID.randomUUID().toString().substring(0,5)+i;
            mapper.insert(new Employee(null,uid,"M",uid+"@zaq.com",1));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值