<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>

                使用JPA插入數據的會簡單到我們懷疑人生,我們僅僅需要在原KlassController中加入以下代碼便能夠完成數據的保存功能。 controller/KlassController.java ```java @PostMapping public void save(@RequestBody Klass klass) { klassRepository.save(klass); ? } ``` * ? KlassRepository繼承了CrudRepository,而CrudRepository中為我們預置了save方法,在此直接調用即完成了數據保存的功能。 ### 測試 啟動數據庫、啟動后臺應用后進行測試: HTTP-Request ``` POST http://localhost:8080/Klass Content-Type: application/json { "name": "測試班級" } ``` 測試結果: ``` POST http://localhost:8080/Klass HTTP/1.1 200 ① Content-Length: 0 Date: Tue, 05 Nov 2019 06:22:09 GMT <Response body is empty> Response code: 200①; Time: 86ms; Content length: 0 bytes ``` * ① 返回的狀態碼為200。 ![](https://img.kancloud.cn/67/88/678823060faec00bce19fcc127ceca59_482x121.png) 接下來,我們在數據表中新增一個用于測試的教師: ![](https://img.kancloud.cn/ef/db/efdb234810f24d091a75bb34ba0fe6ee_678x123.png) 然后繼續進行數據插入測試: ``` POST http://localhost:8080/Klass Content-Type: application/json { "name": "測試班級1", "teacher": { "id": 1 } } ``` ![](https://img.kancloud.cn/db/16/db166b873969eab63140ffdd0fb115a8_455x133.png) **結論:** 使用JPA的確可以非常輕松的完成數據插入操作 。 > 在向數據表中添加數據時,我們有時候也會把該操作稱為`插入`數據或`創建`數據,這是由于在`sql`中新增數據為`insert`,而在講數據一般操作的`CRUD`中的`C`又對應`create`。 ## 自定義返回狀態碼 我們習慣性在數據成功新增后將返回的http狀態碼設置為201,在成功的查詢數據后將返回的狀態碼設置為200;同時我們還習慣性使用`get`方法進行數據的查詢操作,使用`post`方法來進行數據的新增操作。我們的這種`習慣`,是完全的建立在`REST`風格之上的。`REST`風格是一種軟的規范,遵守這個規范的好處是:開發的項目源碼對同樣遵循該規范的工程師具有高度的可讀性。 在Spring中使用`@ResponseStatus`注解來定義返回狀態碼: controller/KlassController.java ``` @PostMapping @ResponseStatus(HttpStatus.CREATED) ? public void save(@RequestBody Klass klass) { klassRepository.save(klass); } ``` * ? 該注解接收的參數類型為HttpStatus,HttpStatus是一個emum類型,而HttpStatus.CREATED是其中的一項,對應的值為:CREATED(201, "Created"),表示:狀態碼對應為201,對該狀態碼的說明為:Created(已創建)。 ### 測試 再次進行測試: ``` POST http://localhost:8080/Klass HTTP/1.1 201 ① Content-Length: 0 Date: Tue, 05 Nov 2019 06:46:44 GMT <Response body is empty> Response code: 201; Time: 259ms; Content length: 0 bytes ``` * ① 狀態碼成功變更為201. # 參考文檔 | 名稱 | 鏈接 | 預計學習時長(分) | | --- | --- | --- | | 源碼地址 | [https://github.com/mengyunzhi/spring-boot-and-angular-guild/releases/tag/step3.3.4](https://github.com/mengyunzhi/spring-boot-and-angular-guild/releases/tag/step3.3.4) | - | | Accessing Data with JPA | [https://spring.io/guides/gs/accessing-data-jpa/](https://spring.io/guides/gs/accessing-data-jpa/) | 15 | | HTTP 狀態碼 | [https://www.runoob.com/http/http-status-codes.html](https://www.runoob.com/http/http-status-codes.html) | 5 |
                  <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>

                              哎呀哎呀视频在线观看