第六节:读取数据 -- read

在上节中,我们给出了参考的官方文档,并且新建了接口。本节中,仍然使用上节中给出参考文档中提及到的方法,使用TeacherRepository来实现数据的读取。


[说明] 由于在上节中,新建TeacherRepository时,已经进行过测试操作,本节再次使用该接口时,则可以直接跳过单元测试的内容。在实际的开发中也是这样的,如果该类是由你创建的,你需要对自己创建的类负责,必须进行相关的测试操作也保证类的可靠性。而如果类不是我们自己创建的,则假设该类经过了单元测试,并且可以直接被我们调用。


C层对接

在进行正式的对接前,我们首先要制定相关的API规范,即接收的数据都有什么,又期待返回什么样的数据。

  • 接收教师ID
  • 返回该ID对应的教师实体

下面,我们按上述的说明,对TeacherController类进行如下补充:


新建方法

    public Teacher getTeacherById() {
        
    }

添加路由

    // @GetMapping 表明该方法只接收 get 请求.
    // {id}即为url中传入教师关键字
    @GetMapping("/teacher/{id}")
    public Teacher getTeacherById() {

    }

添加接收参数

    // @GetMapping 表明该方法只接收 get 请求.
    // {id}即为url中传入教师关键字
    @GetMapping("/teacher/{id}")
    // @PathVariable 获取路由中的id值
    public Teacher getTeacherById(@PathVariable Long id) {

    }

获取教师实体并返回

    // @GetMapping 表明该方法只接收 get 请求.
    // {id}即为url中传入教师关键字
    @GetMapping("/teacher/{id}")
    // @PathVariable 获取路由中的id值
    public Teacher getTeacherById(@PathVariable Long id) {  
        return teacherRepository.findOne(id);
    }

测试

启动项目及MySQL数据库服务,并启动postMan对上述方法进行测试.

添加一条数据

add data

查询刚刚添加的数据

find data

查询不存在的数据

find not exist data

完事代码如下:

package com.mengyunzhi.controller;

import com.mengyunzhi.repository.Teacher;
import com.mengyunzhi.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by panjie on 17/4/6.
 */
// 声明为Rest控制器(支持前后台分离)
@RestController
public class TeacherController {
    @Autowired
    private TeacherRepository teacherRepository;

    // 设置路由
    @RequestMapping("/teacher/save")
    // 使用@RequestBody注解,将请求的`json`数据,直接加载至teacher对象
    public Teacher saveTeacher(@Valid @RequestBody Teacher teacher) {
        // 打印加载的数据
        System.out.println(teacher);

        // 调用保存操作
        return teacherRepository.save(teacher);
    }

    // @GetMapping 表明该方法只接收 get 请求.
    // {id}即为url中传入教师关键字
    @GetMapping("/teacher/{id}")
    // @PathVariable 获取路由中的id值
    public Teacher getTeacherById(@PathVariable Long id) {
        return teacherRepository.findOne(id);
    }
}

重构

TeacherController中,我们定义了两处路由,都有/teacher字符串,下面,我们通过对类添加路由的方式来进行重构,以降低代码的冗余。同时,更加明确的指定请求方式。

...
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    ...
    @PostMapping("/save")
    ...
    @GetMapping("/{id}")
    ...

完整代码:

package com.mengyunzhi.controller;

import com.mengyunzhi.repository.Teacher;
import com.mengyunzhi.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by panjie on 17/4/6.
 */
// 声明为Rest控制器(支持前后台分离)
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherRepository teacherRepository;

    // 设置路由
    @PostMapping("/save")
    // 使用@RequestBody注解,将请求的`json`数据,直接加载至teacher对象
    public Teacher saveTeacher(@Valid @RequestBody Teacher teacher) {
        // 打印加载的数据
        System.out.println(teacher);

        // 调用保存操作
        return teacherRepository.save(teacher);
    }

    // @GetMapping 表明该方法只接收 get 请求.
    // {id}即为url中传入教师关键字
    @GetMapping("/{id}")
    // @PathVariable 获取路由中的id值
    public Teacher getTeacherById(@PathVariable Long id) {
        return teacherRepository.findOne(id);
    }
}

参考官方文档:http://docs.spring.io/spring-data/jpa/docs/1.11.1.RELEASE/reference/html/#repositories.core-concepts


如果你感觉很轻松的就学习完了本节内容,那么说明你的基础打的比较牢固,具有很好的学习精神,具备了一定的英文阅读能力。恭喜你,在计算机工程的道路上,你将越走越加平坦。