Spring MVC CRUD with Example

Day 4

上篇有提到上個範例是沒有用到服務層的 所以這幾天(沒錯 我根本沒空每天發文)

找了另一個簡單的CRUD而且有實作服務的範例

把他寫在前個範例專案

本來又以為很簡單 結果開局就build失敗

因為這個範例使用Thymeleaf模板引擎和gradle去建置

而我用maven去建置這個project

所以要在我的pom.xml裡面加上Thymeleaf的dependency

另外一個問題是CRUD的Update一直無法成功 因為無論我怎麼改 只要在修改後的資料按下提交

永遠會跳錯誤 因為我是透過hidden id 去抓資料庫資料來更新

在輸入更新資料的頁面 所有資料包含id都是正確的值

但提交資料後只有 id 一直跳成0 其他資料欄位都沒問題== 問ChatGPT兩三個小時也鬼打牆

查一陣子StackOverflow才發現

是因為我在Model忘記寫id的 Setter

所以沒辦法設置id(?)

總之加上setter後就能正常運行了⋯⋯

I mentioned that the service layer was not used in the last example.

So I come with the other one I found with the service layer, implement the service, and integrate it into the last project.