在编写后台时,我们仍然假设前台尚未开发或前台的开发尚未完成。那么此时接口文档便是我们开发的唯一参考: ## GET 获取某个班级 使用JPA根据关键字来获取数据特别简单,在此直接给代码: controller/KlassController.java ``` @GetMapping("{id}") @ResponseStatus(HttpStatus.OK) ➊ public Klass get(@PathVariable Long id) { return this.klassRepository.findById(id).get(); ➋ } ``` * ➊ OK(200, "OK") = 返回状态码200,对应的描述为"OK"。如果方法未使用@ResponseStatus进行注解,则表示默认返回HttpStatus.OK。所以此注解可省略。 * ➋ 使用`findById`方法来尝试获取数据表中的某个数据,如果该方法调用后成功获取到了数据,则可继续使用`get`方法来获取这个数据。 > 使用`findById`也是可能获取不到数据表中的值的,比如当ID为-1时则无法在数据表中找到id为-1的班级信息,此时继续调用`get`方法便会发生错误。 ## PUT 先Thinking再Coding,在`JPA`中我们在更新数据的一般思想如下: ![](https://img.kancloud.cn/76/e8/76e8a775d46aa507f1c54566d0c381e9_172x242.png) 具体在当前更新班级的操作为: controller/KlassController.java ``` /** * 更新班级 * 获取数据库中的老数据 * 使用传入的新数据对老数据的更新字段赋值 * 将更新后的老数据重新保存在数据表中 * @param id 要更新的班级ID * @param klass 新班级数据 */ @PutMapping("{id}") @ResponseStatus(HttpStatus.NO_CONTENT) public void update(@PathVariable Long id, @RequestBody Klass klass) { } ``` * ➊ NO_CONTENT(204, "No Content") The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. ## Coding controller/KlassController.java ``` @PutMapping("{id}") @ResponseStatus(HttpStatus.NO_CONTENT) public void update(@PathVariable Long id, @RequestBody Klass klass) { Klass oldKlass = klassRepository.findById(id).get(); ➊ oldKlass.setName(klass.getName()); ➋ oldKlass.setTeacher(klass.getTeacher()); ➌ klassRepository.save(oldKlass); ➍ } ``` * ➊ * ➋ 用传入的klass中的值来更新预更新字段。 * ➌ JPA会根据相关实体的关键字的值来设置数据表中的做为外键的值。比如Teacher的关键字为id,则JPA会根据klass中的teacher中的id来尝试设置`klass`数据表中的`teacher_id`的值。 * ➍ 更新操作与插入操作在JPA中均调用save方法,该方法会根据情景生成对应正确的sql语句来操作数据库。 ## 测试 我们启动数据库后启动项目,首先在数据表中添加两条教师测试信息、一条班级测试信息。 ![](https://img.kancloud.cn/fd/95/fd95d9e3d57d5ae8753f816ac7916bdf_573x140.png) ![](https://img.kancloud.cn/ad/ec/adeccbf89c619b29a469ff47638cfb91_459x121.png) 然后分别进行测试: ### GET 测试代码: ``` GET http://localhost:8080/Klass/1 ``` 测试结果: ``` GET http://localhost:8080/Klass/1 HTTP/1.1 200 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Thu, 07 Nov 2019 06:57:27 GMT { "id": 1, "teacher": { "id": 1, "name": "张喜硕", "sex": false, "username": "zhangxishuo", "email": "zhangxishuo@yunzhiclub.com", "createTime": null, "updateTime": null }, "name": "测试班级" } Response code: 200; Time: 282ms; Content length: 164 bytes ``` ### PUT 测试代码: ``` PUT http://localhost:8080/Klass/1 Content-Type: application/json;charset=UTF-8; { "name": "更新测试班级", "teacher": { "id": 2 } } ``` 测试结果: ``` PUT http://localhost:8080/Klass/1 HTTP/1.1 204 Date: Thu, 07 Nov 2019 06:58:48 GMT <Response body is empty> Response code: 204; Time: 79ms; Content length: 0 bytes ``` 验证: ![](https://img.kancloud.cn/98/2f/982fc7c81f52a0cf2c3469e6e8cc7c5b_419x126.png) # 参考文档 | 名称 | 链接 | 预计学习时长(分) | | --- | --- | --- | | 源码地址 | [https://github.com/mengyunzhi/spring-boot-and-angular-guild/releases/tag/step3.4.5](https://github.com/mengyunzhi/spring-boot-and-angular-guild/releases/tag/step3.4.5) | - | | saving-entites | [https://docs.spring.io/spring-data/jpa/docs/2.2.1.RELEASE/reference/html/#jpa.entity-persistence.saving-entites](https://docs.spring.io/spring-data/jpa/docs/2.2.1.RELEASE/reference/html/#jpa.entity-persistence.saving-entites) | 10 | | CrudRepository | [https://docs.spring.io/spring-data/jpa/docs/2.2.1.RELEASE/reference/html/#repositories.core-concepts](https://docs.spring.io/spring-data/jpa/docs/2.2.1.RELEASE/reference/html/#repositories.core-concepts) | 10 |