編集機能を作成のちテストすると、選んだデータ以外も全部変わってしまうのを直したいです。
データを全て変えるのではなく、ボタン押下時に指定したidに含まれるカラムのデータのみを変更できるようにです。
controller package com.example.demo.controller; import com.example.demo.service.BookService; import java.util.List; import java.util.Optional; import com.example.demo.domain.Book; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/books") public class BookController { private final BookService bookService; @Autowired public BookController(BookService bookService) { this.bookService = bookService; } @GetMapping public String index(Model model) { //Taskのリストを取得する List<Book> book = bookService.findAll(); model.addAttribute("book", book); return "books/index"; } @GetMapping("new") public String newBook(Model model) { return "books/new"; } @PostMapping public String create(@ModelAttribute("book") @Validated Book book, BindingResult result, Model model) { if (result.hasErrors()) { return "books/new"; } else { bookService.save(book); return "redirect:/books"; } } @GetMapping("{id}") public String show(@PathVariable int id, Model model) { //Bookを取得(findOneはOptionalで) Optional<Book> book = bookService.findOne(id); model.addAttribute("book", book); return "books/show"; } @GetMapping("{id}/edit") public String edit(@PathVariable("id") int id, @ModelAttribute("book") Book book, Model model) { bookService.findOne(id).ifPresent(o -> model.addAttribute("book", o)); return "books/edit"; } @PostMapping("{id}") public String update(@PathVariable int id, @ModelAttribute("book") @Validated Book book, BindingResult result, Model model){ if (result.hasErrors()) { model.addAttribute("book", book); return "edit"; } else { book.setId(id); bookService.update(book); return "redirect:/books"; } } }
domain package com.example.demo.domain; import java.sql.Date; public class Book { private int id; private String title; private String author; private String publisher; private Date buyDate; private Date releaseDate; private String overView; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public Date getBuyDate() { return buyDate; } public void setBuyDate(Date buyDate) { this.buyDate = buyDate; } public Date getReleaseDate() { return releaseDate; } public void setReleaseDate(Date releaseDate) { this.releaseDate = releaseDate; } public String getOverView() { return overView; } public void setOverView(String overView) { this.overView = overView; } }
repository package com.example.demo.repository; import java.sql.Date; import java.util.List; import java.util.Map; import java.util.Optional; import com.example.demo.domain.Book; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class BookRepositoryImpl implements BookRepository{ @Autowired private final JdbcTemplate jdbcTemplate; @Autowired public BookRepositoryImpl(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public List<Book> findAll() { String sql = "SELECT book.id, title, author, publisher, buy_date, release_date, over_view FROM Book"; return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class)); } @Override public Optional<Book> findOne(int id) { String sql = "SELECT book.id, title, author, publisher, buy_date, release_date, over_view FROM book " + "WHERE id = ?"; Map<String, Object> result = jdbcTemplate.queryForMap(sql, id); Book book = new Book(); book.setId((int)result.get("id")); book.setTitle((String)result.get("title")); book.setAuthor((String)result.get("author")); book.setPublisher((String)result.get("publisher")); book.setBuyDate((Date)result.get("buy_date")); book.setReleaseDate((Date)result.get("release_date")); book.setOverView((String)result.get("over_view")); System.out.println(result); //bookをOptionalでラップする Optional<Book> bookOpt = Optional.ofNullable(book); return bookOpt; } @Override public void save(Book book) { jdbcTemplate.update("INSERT INTO book(title, author, publisher, buy_date, release_date, over_view) VALUES(?, ?, ?, ?, ?, ?)", book.getTitle(), book.getAuthor(), book.getPublisher(), book.getBuyDate(), book.getReleaseDate(), book.getOverView() ); } @Override public int update(Book book) { return jdbcTemplate.update("UPDATE book SET title = ?, author = ?, publisher = ?, buy_date = ?, release_date = ?, over_view = ? WHERE id = id", book.getTitle(), book.getAuthor(), book.getPublisher(), book.getBuyDate(), book.getReleaseDate(), book.getOverView() ); } @Override public int delete(int id) { return jdbcTemplate.update("DELETE FROM book WHERE id = ?", id); } }
service package com.example.demo.service; import java.util.List; import java.util.Optional; import com.example.demo.repository.BookRepository; import com.example.demo.domain.Book; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class BookServiceImpl implements BookService{ private final BookRepository repo; @Autowired public BookServiceImpl(BookRepository repo) { this.repo = repo; } @Override public List<Book> findAll() { return repo.findAll(); } @Override public Optional<Book> findOne(int id) { return repo.findOne(id); } @Override public void save(Book book) { repo.save(book); } @Override public void update(Book book) { //Taskを更新 idが無ければ例外発生 if (repo.update(book) == 0) { throw new BookNotFoundException("error"); } } @Override public void delete(int id) { //Taskを更新 idがなければ例外発生 if (repo.delete(id) == 0) { throw new BookNotFoundException("error"); } } }
<html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>Editing Player - baseball</title> <link rel="stylesheet" href="/css/bootstrap.css" /> <script src="/js/jquery.js"></script> <script src="/js/bootstrap.js"></script> </head> <body> <div class="container"> <h1>Editing</h1> <form th:action="@{/books/{id}(id=*{id})}" th:method="put" th:object="${book}"> <div class="form-group"> <label class="control-label">名前</label> <input class="form-control" type="text" th:field="*{title}" /> </div> <div class="form-group"> <label class="control-label">著者</label> <input class="form-control" type="text" th:field="*{author}" /> </div> <div class="form-group"> <label class="control-label">出版社</label> <input class="form-control" type="text" th:field="*{publisher}" /> </div> <div class="form-group"> <label class="control-label">購入日</label> <input class="form-control" type="date" th:field="*{buyDate}" /> </div> <div class="form-group"> <label class="control-label">販売日</label> <input class="form-control" type="date" th:field="*{releaseDate}" /> </div> <div class="form-group"> <label class="control-label">概要</label> <input class="form-control" type="text" th:field="*{overView}" /> </div> <button class="btn btn-default" type="submit">更新</button> </form> <div class="pull-right"> <a class="btn btn-link" href="/books">一覧画面へ</a> </div> </div> </body> </html>
controllerのupdate内でidを限定した記述をするべきだと考え調べているのですがこれ以上進めず,
申し訳ないのですが質問させていただきます。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/25 06:41