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

Dao

EmpDao

  1. package com.jotal.springboot08restfulcrud.dao;
  2. //将类扫描进spring ioc容器中
  3. @Mapper
  4. public interface EmpDao {
  5. // 得到所有员工
  6. List<Employee> getAllEmp();
  7. // 根据id得到员工
  8. Employee getEmpById(Integer id);
  9. // 保存员工
  10. void saveEmp(Employee employee);
  11. //添加员工
  12. void addEmp(Employee employee);
  13. // 删除员工
  14. void delEmp(Integer emp_id);
  15. }

EmpMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.jotal.springboot08restfulcrud.dao.EmpDao">
  6. <select id="getAllEmp" resultMap="DeptInEmpMap">
  7. select * from employee,department
  8. where employee.department = department.departmentId
  9. </select>
  10. <select id="getEmpById" resultMap="DeptInEmpMap" parameterType="Integer">
  11. select * from employee,department
  12. where employee.emp_id=#{id} and department.departmentId=employee.department
  13. </select>
  14. <resultMap id="DeptInEmpMap" type="employee" autoMapping="true">
  15. <id property="emp_id" column="emp_id"/>
  16. <result property="lastName" column="lastName"/>
  17. <result property="email" column="email"/>
  18. <result property="gender" column="gender"/>
  19. <result property="birth" column="birth"/>
  20. <association property="department" javaType="department" autoMapping="true">
  21. <id column="departmentId" property="departmentId"/>
  22. <result column="departmentName" property="departmentName"/>
  23. </association>
  24. </resultMap>
  25. <delete id="delEmp" parameterType="Integer">
  26. delete from employee
  27. where emp_id=#{id}
  28. </delete>
  29. <insert id="addEmp" parameterType="employee">
  30. insert into employee(lastName,email,gender,birth,department)
  31. values (#{lastName},#{email},#{gender},#{birth},#{department.departmentId})
  32. </insert>
  33. <update id="saveEmp" parameterType="employee">
  34. update employee set
  35. lastName=#{lastName},email=#{email},gender=#{gender},department=#{department.departmentId},birth=#{birth}
  36. where emp_id=#{emp_id}
  37. </update>
  38. </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查询员工信息

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

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

  1. <select id="getDeptById" resultType="department" parameterType="Integer">
  2. select * from department
  3. where departmentId=#{id}
  4. </select>

ps: getDeptById在DeptDao.xml里

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

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

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

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

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

property:属性名

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

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

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

依赖

  1. <!--测试-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-test</artifactId>
  5. <scope>test</scope>
  6. </dependency>

测试类

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

  1. @RunWith(SpringRunner.class)
  2. //主启动类
  3. @SpringBootTest(classes = Springboot08RestfulcrudApplication.class)
  4. public class Springboot08RestfulcrudApplicationTests {
  5. //自动装配
  6. @Autowired
  7. EmpDao empDao;
  8. @Test
  9. public void contextLoads() {
  10. }
  11. }

控制台输出sql

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

com.jotal.springboot08restfulcrud.dao是包名

debug是日志的等级

  1. #日志
  2. logging:
  3. level:
  4. com.jotal.springboot08restfulcrud.dao: debug

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

getAllEmpTest

  1. @Test
  2. public void getAllEmpTest() {
  3. List<Employee> employeeList = empDao.getAllEmp();
  4. System.out.println(empDao.getAllEmp());
  5. Iterator iterator = employeeList.iterator();
  6. int i=0;
  7. while (iterator.hasNext()) {
  8. Employee employee = (Employee) iterator.next();
  9. System.out.println(i+":"+employee);
  10. i++;
  11. }
  12. }

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

getEmpByIdTest

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

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

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

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

  1. 使用springboot实现一个简单的restful crud——01、项目简介以及创建项目

    前言 之前一段时间学习了一些springboot的一些基础使用方法和敲了一些例子,是时候写一个简单的crud来将之前学的东西做一个整合了 -- 一个员工列表的增删改查. 使用 restful api ...

  2. 使用springboot实现一个简单的restful crud——03、前端页面、管理员登陆(注销)功能

    前言 这一篇我们就先引入前端页面和相关的静态资源,再做一下管理员的登陆和注销的功能,为后续在页面上操作数据做一个基础. 前端页面 前端的页面是我从网上找的一个基于Bootstrap 的dashboar ...

  3. 使用webpy创建一个简单的restful风格的webservice应用

    下载:wget http://webpy.org/static/web.py-0.38.tar.gz解压并进入web.py-0.38文件夹安装:easy_install web.py 这是一个如何使用 ...

  4. 使用springboot写一个简单的测试用例

    使用springboot写一个简单的测试用例 目录结构 pom <?xml version="1.0" encoding="UTF-8"?> < ...

  5. springboot搭建一个简单的websocket的实时推送应用

    说一下实用springboot搭建一个简单的websocket 的实时推送应用 websocket是什么 WebSocket是一种在单个TCP连接上进行全双工通信的协议 我们以前用的http协议只能单 ...

  6. python 多进程——使用进程池,多进程消费的数据)是一个队列的时候,他会自动去队列里依次取数据

    我的mac 4核,因此每次执行的时候同时开启4个线程处理: # coding: utf-8 import time from multiprocessing import Pool def long_ ...

  7. 【SpingBoot】 测试如何使用SpringBoot搭建一个简单后台1

    很久没写博客了,最近接到一个组内的测试开发任务是做一个使用SpringBoot 开发一个后台程序(还未完成),特写感想记录一下 1. 为什么选择SpringBoot ? 首先是目前很多公司的后台还是J ...

  8. 基础项目构建,引入web模块,完成一个简单的RESTful API 转载来自翟永超

    简介 在您第一次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用Spring框架的时候,是否觉得一堆反复粘贴的配置有一些厌烦?那么您就不妨来试试使用Spring Boot ...

  9. laravel 实现一个简单的 RESTful API

    创建一个 Article 资源 php artisan make:resource Article 你可以在 app/Http/Resources 目录下看到你刚刚生成的 Article 资源 当然我 ...

随机推荐

  1. Linux下的nexus数据迁移

    刚到公司没多久,目前公司有两个项目公用一个nexus的maven私服,现在想把两个私服的jar包拆分开: 我在原私服的nexus服务器中, 1.备份原nexus使用命令 完成tar包的压缩 打包完毕后 ...

  2. 剑指offer:整数中1出现的次数

    题目描述: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了 ...

  3. SQLServer charindex函数, 查 某个字符 或 某个字符串 在 另一个字符串中的位置

    一:charindex()语法 CHARINDEX ( expression1 , expression2 [ , start_location ] ) 解析: expression1 必需 ---要 ...

  4. word 转 pdf,c#代码

    通过使用 C# 控制 office 软件 com 组件转 pdf 1 word 转 pdf 方案二:可以使用 netoffice 进行转换 参考文档:https://netoffice.io/docu ...

  5. SpringCloud基础知识

    什么是SpringCloud Spring Cloud是一系列框架的有序集合. 为什么用SpringCloud Spring Cloud涵盖面广,能够与Spring Framework.Spring ...

  6. Shenzhen Wanze Technology Co., Ltd.技术支持

    本网页为Shenzhen Wanze Technology Co., Ltd.团队的技术支持网址,如果在我们开发的游戏中遇到任何问题,欢迎联系我们! QQ:2535510006 邮箱:25355100 ...

  7. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  8. python:日期计算

    python语言中的datetime模块可以利用其中的方法获取不同的日期,比如获取当前日期.明天.昨天.上个月.下个月和明年.下面利用几个实例说明这些日期的获取方法,操作如下: 第一步,利用datet ...

  9. MySQL之表约束

    MySQL表约束 约束是一种限制,它通过对表的行或者列的数据做出限制,来确保表数据的完整性和唯一性. 在mysql当中一般有一下这几种约束: 非空约束. 唯一约束. 主键约束. 自增长. 默认约束. ...

  10. 【VS开发】#pragma pack(push,1)与#pragma pack(1)的区别

    这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n)             作用:C编译器将按照n个字节对 ...