画面遷移としては、編集->編集確認->編集完了
edit_confirm.html
<form class="" method="get" action="/test1/editcomplete"> <input type="hidden" name="id" th:value="${id}"> <input type="hidden" name="name" th:value="${name}"> <input type="hidden" name="department" th:value="${department}"> <table class="table"> <tr><td>名前</td><td th:text="${name}"></td></tr> <tr><td>所属</td><td th:text="${department}"></td></tr> </table> <button type="button" class="btn btn-primary" onclick="location.href='/test1/index'">戻る</button> <button type="submit" class="btn btn-primary">編集完了</button> </form>
MainController.java
@GetMapping("editconfirm") public String editconfirm( @RequestParam(name = "id") Integer id, @RequestParam(name = "name") String name, @RequestParam(name = "department") String department, Model model) { model.addAttribute("id", id); model.addAttribute("name", name); model.addAttribute("department", department); return "test1/edit_confirm"; }
ここまでは何も考えずにいける
updateする為にnameとdepartment以外にidも加える
UsersRepository.java
L エンティティの値をupdate
public Users update(Users users) throws DataAccessException { // SQL文を作成 String sql = "" + "UPDATE users SET name = ?, department = ?" + " WHERE" + " id = ?"; jdbcTemplate.update(sql, users.getName(),users.getDepartment(),users.getId()); return users; }
MainController.java
@GetMapping("editcomplete") public String editcomplete( @RequestParam(name = "id") Integer id, @RequestParam(name = "name") String name, @RequestParam(name = "department") String department, Model model) { Users users = new Users(); users.setId(id); users.setName(name); users.setDepartment(department); usersRepository.update(users); model.addAttribute("name", name); model.addAttribute("department", department); return "test1/edit_complete"; }
updateされました。
よしゃああああああああああああああああああああああああ
SpringBootもCRUまできた。残りはDやな。