Springboot implemented using a simple restful crud - 02, dao layer unit test, take the test data from the database

Then on one, the one we created the project, created entity classes, and created a database data. This one will write about Dao layer, and the layer of Dao unit testing, look at the success of the operation database data.

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>

We focus look getEmpById () operation, which is obtained according to a staff ID . Because employees have a department which class property, citing department department class, that instance of the employee class will contain an instance of the class department. So this case is called "one to one" in Mybatis, one corresponding to a department employee.

In this case we need The resultMap , we define a resultMap, and specifies the type id: id = "DeptInEmpMap", type = "employee", we specify the type of the entity class employee, and will result in the class and a correspondence attribute field in a database table, property class attribute name, column is a field in a database table. id is the primary key of the table id. If the property names and field names exactly the same, it can be automatically mapped with the = automapping "to true" , do not write result.

We mapped in association included in the department of example employee. property = "department" is the attribute name, javaType = "department" is the class name. Inside the id and the result is also the same as above. It can also be used autoMapping = "true" to automatically map

The use in select the resultMap: resultMap = "DeptInEmpMap".


Speaking of the above there is resultMap of nested results using the method, resultMap there is a nested query usage patterns. The following look at ways:

Nested query is done step by step:

1, first check employee information by employee id

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

2, based on the ID value employees instance instance department to department information inquiry

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

ps: getDeptById in DeptDao.xml in

3, the information provided to the department staff

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

In this way, association set to three values: property = "department" column = "department" select = ""

select: show the result of the current property is to call the select method specified ISOLATED

column: values ​​that specify which column is passed to this method as a parameter

property: property name

Similarly, modify getAllEmp query can also be achieved by way getAllEmp nested queries.

Dao layer unit test console output sql

Unit testing can help us find mistakes early in the coding of correction as soon as possible. If the post-coding the entire system to be tested again when complete, the entire system needs to go through the process, consuming time and energy resources. So springboot how to perform basic unit testing it?

rely

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

Test category

This is a good idea that we automatically create a test class, and then add in there own way to test it.

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

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


Sql console output

To better understand the operation of the database, you can also join in the project log output, the output of sql statement in the console. Join configuring logging in the configuration file.

com.jotal.springboot08restfulcrud.dao is the package name

debug log level is

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


The following two tests were carried out in the right-click Debug method method name on it at the code:

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++;
    }
}

Based resultMap of nested queries, sql statement is executed separately


getEmpByIdTest

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

yoyo, successfully got the data from the database. Our project progress made "significant" progress. Other methods of unit testing, too, is not in eleven out here.


This will stop here, were today Dao layer write code section, as well as the Dao layer code unit testing, successfully got the data from the database. Then it will start with a functional, integrated front and rear ends to complete.

Guess you like

Origin www.cnblogs.com/Jotal/p/11344111.html