Spring Boot JPA + H2 example: Build a CRUD Rest APIs

Day 3

好不容易找到一個做得出來的教學⋯⋯

而且這個教學是幫你整套寫好 你只要連資料庫就可以動了! 沒想到光是連資料庫就查了兩個小時==

本來想用mysql 結果一直設定不好

後來是用H2資料庫(可以上網下載 mac 直接用homebrew)

影片裡筆記的錯誤只要讓h2連線的url跟application.properties的一致就可以解決

其他重點就是在我影片中用postman試完request都沒問題後才回頭去讀網站的說明

最後依照我的理解 統整出要建CRUD的流程大致是:

  1. 建立 Entity: 建Model去定義你要拿資料的表格(例如資料表的欄位、主鍵、資料型態⋯⋯等等)
  2. 實作 Repository: 主要定義用JPA從資料庫拿資料後運作crud的功能
  3. 服務層的業務邏輯: 用來封裝crud運作方式,通常會透過實作一個服務來呼叫對應的repository方法(這個教學裡沒有 是用controller來呼叫)
  4. 建立 Controller: 把url跟http請求mapping在一起

After searching several tutorial, I find this one which the author config all the project well. The only thing we have to do is connect to the database for fetching data.

However as a newbie, it costs me almost 2 hours to complete the step.

At the end of the day, i use H2 database (you can download online or just from homebrew if you are a mac user) instead of MySQL.

The Exception part of my note showed in the video, is that the JDBC URL should be the same as the url set in the application.properties.

I test all type of the request in the tutorial using Postman. After that, I go through the whole tutorial again and take my note.

The process of build a CRUD operation for my aspect as follows: