使用springboot实现一个简单的restful crud——02、dao层单元测试,测试从数据库取数据

接着上一篇,上一篇我们创建了项目、创建了实体类,以及创建了数据库数据。这一篇就写一下Dao层,以及对Dao层进行单元测试,看下能否成功操作数据库数据。

Dao

EmpDao

package com.jotal.springboot08restfulcrud.dao;

//将类扫描进spring ioc容器中
@Mapper
public interface EmpDao {
//    得到所有员工
    List<Employee> getAllEmp();
//    根据id得到员工
    Employee getEmpById(Integer id);
    //    保存员工
    void saveEmp(Employee employee);
    //添加员工
    void addEmp(Employee employee);
    //    删除员工
    void delEmp(Integer emp_id);
}

EmpMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.jotal.springboot08restfulcrud.dao.EmpDao">
    <select id="getAllEmp" resultMap="DeptInEmpMap">
        select * from employee,department
        where employee.department = department.departmentId
    </select>
    <select id="getEmpById" resultMap="DeptInEmpMap" parameterType="Integer">
        select * from employee,department
        where employee.emp_id=#{id} and department.departmentId=employee.department
    </select>

    <resultMap id="DeptInEmpMap" type="employee" autoMapping="true">
        <id property="emp_id" column="emp_id"/>
        <result property="lastName" column="lastName"/>
        <result property="email" column="email"/>
        <result property="gender" column="gender"/>
        <result property="birth" column="birth"/>
        <association property="department" javaType="department" autoMapping="true">
            <id column="departmentId" property="departmentId"/>
            <result column="departmentName" property="departmentName"/>
        </association>
    </resultMap>

    <delete id="delEmp" parameterType="Integer">
        delete from employee
        where emp_id=#{id}
    </delete>

    <insert id="addEmp" parameterType="employee">
        insert into employee(lastName,email,gender,birth,department)
         values (#{lastName},#{email},#{gender},#{birth},#{department.departmentId})
    </insert>

    <update id="saveEmp" parameterType="employee">
        update employee set
        lastName=#{lastName},email=#{email},gender=#{gender},department=#{department.departmentId},birth=#{birth}
        where emp_id=#{emp_id}
    </update>

</mapper>

我们重点看一下getEmpById( )的操作,也就是根据ID得到一个员工。因为员工类当中有一个department属性,department部门类的引用,也就是说employee类的实例中会包含着一个department类的实例。那么这种情况在Mybatis中称为"一对一",一个员工对应一个部门。

这种情况我们需要用resultMap,我们定义了一个resultMap,指定了id和类型: id="DeptInEmpMap",type="employee",这个类型我们指定了实体类中的employee,然后在result中将类的属性和数据库表中的字段一一对应,property是类中的属性名,column是数据库表的字段。id是表中的主键id。如果属性名和字段名完全一致,就可以用autoMapping="true"来自动映射,不用写result。

在association中映射我们包含在employee中的department实例。property="department"是属性名,javaType="department"是类名。里面的id和result也和上面一样。也可以用autoMapping="true"来自动映射

在select中使用该resultMap:resultMap="DeptInEmpMap"。


上面的有说到的是resultMap的嵌套结果的使用方式,resultMap还有一种嵌套查询的使用方式。下面看一下实现方式:

嵌套查询是分步完成的:

1、先按照员工id查询员工信息

<select id="getEmpById" resultMap="DeptInEmpMap" parameterType="Integer">
    select * from employee
    where employee.emp_id=#{id}
</select>

2、根据员工实例中的部门实例的ID值去查询部门信息

<select id="getDeptById" resultType="department" parameterType="Integer">
    select * from department
    where departmentId=#{id}
</select>

ps: getDeptById在DeptDao.xml里

3、将部门信息设置到员工中

<resultMap id="DeptInEmpMap" type="employee" autoMapping="true">
    <association property="department" column="department" select="com.jotal.springboot08restfulcrud.dao.DeptDao.getDeptById">
    </association>
</resultMap>

在这种方式中,association要设置三个值:property="department" column="department" select=""

select:表明当前属性是调用select指定的方法查出的结果

column:指定将哪一列的值作为参数传给这个方法

property:属性名

同样,修改一下getAllEmp的查询语句也可以用嵌套查询的方式实现getAllEmp。

Dao层单元测试、控制台输出sql

单元测试可以在编码的初期帮我们发现错误,尽快修正。如果编码后期整个系统完整的时候再进行测试,需要走完整个系统流程,耗费时间精力资源。那么springboot怎么进行基本的单元测试呢?

依赖

<!--测试-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

测试类

这是idea为我们自动创建好的测试类,然后自己在里面添加测试方法就可以了。

@RunWith(SpringRunner.class)
//主启动类
@SpringBootTest(classes = Springboot08RestfulcrudApplication.class)
public class Springboot08RestfulcrudApplicationTests {

    //自动装配
    @Autowired
    EmpDao empDao;
    
    @Test
    public void contextLoads() {
    }
}


控制台输出sql

为了更好地了解数据库操作情况,还可以在项目中加入日志输出,在控制台输出sql语句。在配置文件中加入日志的配置。

com.jotal.springboot08restfulcrud.dao是包名

debug是日志的等级

#日志
logging:
  level:
    com.jotal.springboot08restfulcrud.dao: debug


下面进行进行两个测试,在方法代码处右键点击Debug 方法名就可以了:

getAllEmpTest

@Test
public void getAllEmpTest() {
    List<Employee> employeeList = empDao.getAllEmp();
    System.out.println(empDao.getAllEmp());
    Iterator iterator = employeeList.iterator();
    int i=0;
    while (iterator.hasNext()) {
        Employee employee = (Employee) iterator.next();
        System.out.println(i+":"+employee);
        i++;
    }
}

基于resultMap的嵌套查询,sql语句是分开执行的


getEmpByIdTest

@Test
public void getEmpByIdTest() {
    System.out.println(empDao.getEmpById(1002));
}

yoyo,成功地从数据库拿到了数据。我们的项目进度取得了“重大”进展。其他方法的单元测试也是如此,就不在这里一一贴出来了。


这篇就讲到这里,今天进行部分Dao层代码的编写,以及进行了Dao层代码的单元测试,成功地从数据库中拿到了数据。接下来就会从一个个功能入手,完成前后端的整合。

猜你喜欢

转载自www.cnblogs.com/Jotal/p/11344111.html