<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                在編寫后臺時,我們仍然假設前臺尚未開發或前臺的開發尚未完成。那么此時接口文檔便是我們開發的唯一參考: ## 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 |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看