{"id":29,"date":"2022-11-24T13:59:22","date_gmt":"2022-11-24T04:59:22","guid":{"rendered":"https:\/\/kpc2022.satoshis.jp\/?p=29"},"modified":"2022-11-24T14:11:24","modified_gmt":"2022-11-24T05:11:24","slug":"%e6%9c%ac%e6%a3%9a%e3%82%92%e7%a7%bb%e5%8b%95%e3%81%97%e3%82%88%e3%81%86%e3%81%a8%e3%81%99%e3%82%8b%e3%81%a8%e3%82%a8%e3%83%a9%e3%83%bc%e3%81%ab%e3%81%aa%e3%82%8b%e5%95%8f%e9%a1%8c%e3%82%92%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/kpc2022.satoshis.jp\/?p=29","title":{"rendered":"\u672c\u68da\u3092\u79fb\u52d5\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\u554f\u984c\u3092\u89e3\u6d88\u3059\u308b"},"content":{"rendered":"<p>\u672c\u3092\u5225\u306e\u672c\u68da\u306b\u79fb\u52d5\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3002<\/p>\n<p>\u3053\u308c\u306f\u3001\u4eca\u306e\u72b6\u614b\u3067\u306f1\u518a\u306e\u672c\u3092\u8907\u6570\u306e\u672c\u68da\u306b\u5165\u308c\u3088\u3046\u3068\u3057\u3066\u3044\u308b\u3002<br \/>\n\u73fe\u5b9f\u7684\u306b\u4e0d\u53ef\u80fd\u306a\u3053\u3068\u3092\u3084\u308d\u3046\u3068\u3057\u3066\u3044\u308b\u306e\u3067\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002<\/p>\n<p>\u672c\u68da\u3092\u79fb\u52d5\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u5143\u306e\u672c\u68da\u304b\u3089\u672c\u3092\u53d6\u308a\u51fa\u3057\u305f\u5f8c\u3067\u3001\u5225\u306e\u672c\u68da\u306b\u79fb\u52d5\u3059\u308b\u3068\u3044\u3046\u64cd\u4f5c\u304c\u5fc5\u8981\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage jp.bookshelf;\r\n\r\nimport java.util.List;\r\nimport java.util.Optional;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.web.bind.annotation.ModelAttribute;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestMethod;\r\nimport org.springframework.web.bind.annotation.RequestParam;\r\nimport org.springframework.web.servlet.ModelAndView;\r\n\r\n@Controller\r\npublic class BookController {\r\n\t@Autowired\r\n\tprivate BookRepository repository;\r\n\t@Autowired\r\n\tprivate BookshelfRepository bookshelfRepository;\r\n\r\n\t@RequestMapping(value = &quot;\/book&quot;, method = RequestMethod.GET)\r\n\tpublic ModelAndView list(ModelAndView mav) {\r\n\t\tmav.addObject(&quot;book&quot;, new Book());\r\n\t\tmav.setViewName(&quot;books&quot;);\r\n\t\tList&lt;Book&gt; list = repository.findAll();\r\n\t\tList&lt;Bookshelf&gt; bookshelfs = bookshelfRepository.findAll();\r\n\t\tmav.addObject(&quot;list&quot;, list);\r\n\t\tmav.addObject(&quot;bookshelfs&quot;, bookshelfs);\r\n\t\treturn mav;\r\n\t}\r\n\r\n\t@RequestMapping(value = &quot;\/book&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView post(ModelAndView mav,\r\n\t\t\t@ModelAttribute(&quot;book&quot;) Book book) {\r\n\t\trepository.saveAndFlush(book);\r\n\t\treturn new ModelAndView(&quot;redirect:\/book&quot;);\r\n\t}\r\n\r\n\t@RequestMapping(value = &quot;\/edit\/{id}&quot;, method = RequestMethod.GET)\r\n\tpublic ModelAndView edit(ModelAndView mav,\r\n\t\t\t@PathVariable long id) {\r\n\t\tmav.setViewName(&quot;edit&quot;);\r\n\t\tOptional&lt;Book&gt; data = repository.findById(id);\r\n\t\tmav.addObject(&quot;book&quot;, data.get());\r\n\t\treturn mav;\r\n\t}\r\n\r\n\t@RequestMapping(value = &quot;\/edit&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView update(ModelAndView mav,\r\n\t\t\t@ModelAttribute(&quot;book&quot;) Book book) {\r\n\t\trepository.saveAndFlush(book);\r\n\t\treturn new ModelAndView(&quot;redirect:\/book&quot;);\r\n\t}\r\n\r\n\t@RequestMapping(value = &quot;\/push&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView push(ModelAndView mav,\r\n\t\t\t@RequestParam(&quot;id&quot;) long id,\r\n\t\t\t@RequestParam(&quot;bookshelf&quot;) long bookshelfId) {\r\n\t\tOptional&lt;Book&gt; data = repository.findById(id);\r\n\t\tBook book = data.get();\r\n\t\tBookshelf prev = book.getBookshelf();\r\n\t\tif (prev != null) {\r\n\t\t\tprev.removeBook(book);\r\n\t\t\tbookshelfRepository.saveAndFlush(prev);\r\n\t\t}\r\n\t\tif (bookshelfId != 0) {\r\n\t\t\tOptional&lt;Bookshelf&gt; bs = bookshelfRepository.findById(bookshelfId);\r\n\t\t\tBookshelf bookshelf = bs.get();\r\n\t\t\tbookshelf.addBook(book);\r\n\t\t\tbook.setBookshelf(bookshelf);\r\n\t\t\tbookshelfRepository.saveAndFlush(bookshelf);\r\n\t\t\trepository.saveAndFlush(book);\r\n\t\t} else {\r\n\t\t\tbook.setBookshelf(null);\r\n\t\t\trepository.saveAndFlush(book);\r\n\t\t\treturn new ModelAndView(&quot;redirect:\/&quot;);\r\n\t\t}\r\n\t\treturn new ModelAndView(&quot;redirect:\/bookshelf\/&quot; + bookshelfId);\r\n\t}\r\n}\r\n<\/pre>\n<p>\u672c\u3092\u672c\u68da\u306b\u5165\u308c\u305f\u5f8c\u3067\u3001\u672c\u68da\u304b\u3089\u53d6\u308a\u51fa\u3057\u3066\u3001\u3069\u306e\u672c\u68da\u306b\u3082\u5165\u308c\u306a\u3044\u3068\u3044\u3046\u64cd\u4f5c\u3082\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html xmlns:th=&quot;http:\/\/www.thymeleaf.org&quot;&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;UTF-8&quot;&gt;\r\n&lt;title&gt;\u672c\u306e\u30ea\u30b9\u30c8&lt;\/title&gt;\r\n&lt;style type=&quot;text\/css&quot;&gt;\r\nimg.book-image {\r\n\twidth: 100px;\r\n}\r\n&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;h2&gt;\u672c\u3092\u8ffd\u52a0&lt;\/h2&gt;\r\n&lt;form action=&quot;\/book&quot; method=&quot;post&quot; th:object=&quot;${book}&quot;&gt;\r\n\t\u30bf\u30a4\u30c8\u30eb: &lt;input type=&quot;text&quot; name=&quot;title&quot; th:value=&quot;*{title}&quot; \/&gt;\r\n\t&lt;br \/&gt;\r\n\t\u8457\u8005: &lt;input type=&quot;text&quot; name=&quot;author&quot; th:value=&quot;*{author}&quot; \/&gt;\r\n\t&lt;br \/&gt;\r\n\t\u753b\u50cfURL: &lt;input type=&quot;text&quot; name=&quot;image&quot; th:value=&quot;*{image}&quot; \/&gt;\r\n\t&lt;br \/&gt;\r\n\t&lt;input type=&quot;submit&quot; value=&quot;\u672c\u3092\u8ffd\u52a0&quot; \/&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;h1&gt;\u672c\u306e\u30ea\u30b9\u30c8&lt;\/h1&gt;\r\n&lt;table&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;th&gt;\u753b\u50cf&lt;\/th&gt;&lt;th&gt;\u30bf\u30a4\u30c8\u30eb&lt;\/th&gt;&lt;th&gt;\u8457\u8005\u540d&lt;\/th&gt;&lt;th&gt;\u64cd\u4f5c&lt;\/th&gt;&lt;th&gt;\u672c\u68da&lt;\/th&gt;\r\n\t&lt;\/tr&gt;\r\n\t&lt;tr th:each=&quot;book : ${list}&quot;&gt;\r\n\t\t&lt;td&gt;&lt;img class=&quot;book-image&quot; th:src=&quot;${book.image}&quot; \/&gt;&lt;\/td&gt;\r\n\t\t&lt;td th:text=&quot;${book.title}&quot;&gt;&lt;\/td&gt;\r\n\t\t&lt;td th:text=&quot;${book.author}&quot;&gt;&lt;\/td&gt;\r\n\t\t&lt;td&gt;\r\n\t\t\t&lt;a th:href=&quot;@{&#039;\/edit\/&#039; + ${book.id}}&quot;&gt;\u4fee\u6b63&lt;\/a&gt;\r\n\t\t\t&lt;a th:href=&quot;@{&#039;\/delete\/&#039; + ${book.id}}&quot;&gt;\u524a\u9664&lt;\/a&gt;\r\n\t\t&lt;\/td&gt;\r\n\t\t&lt;td&gt;\r\n\t\t\t\u4eca\u306e\u672c\u68da: &lt;span th:if=&quot;${book.bookshelf != null}&quot; th:text=&quot;${book.bookshelf.name}&quot;&gt;&lt;\/span&gt;\r\n\t\t\t&lt;form action=&quot;\/push&quot; method=&quot;post&quot;&gt;\r\n\t\t\t\t&lt;input type=&quot;hidden&quot; name=&quot;id&quot; th:value=&quot;${book.id}&quot; \/&gt;\r\n\t\t\t\t&lt;select name=&quot;bookshelf&quot;&gt;\r\n\t\t\t\t&lt;option value=&quot;0&quot;&gt;\u672c\u68da\u306b\u5165\u308c\u306a\u3044&lt;\/option&gt;\r\n\t\t\t\t&lt;th:block th:each=&quot;bookshelf : ${bookshelfs}&quot;&gt;\r\n\t\t\t\t&lt;option th:value=&quot;${bookshelf.id}&quot; th:text=&quot;${bookshelf.name}&quot;&gt;&lt;\/option&gt;\r\n\t\t\t\t&lt;\/th:block&gt;\r\n\t\t\t&lt;\/select&gt;\u306b&lt;button&gt;\u79fb\u52d5&lt;\/button&gt;\u3059\u308b\r\n\t\t\t&lt;p th:if=&quot;${book.bookshelf != null}&quot; th:text=&quot;${book.bookshelf.name}&quot;&gt;\r\n\t\t\t&lt;\/form&gt;\r\n\t\t&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n\r\n&lt;a href=&quot;\/&quot;&gt;\u623b\u308b&lt;\/a&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Bookshelf\u306bremoveBook()\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: java; first-line: 48; highlight: [52,53,54]; title: ; notranslate\" title=\"\">\r\n\tpublic void addBook(Book book) {\r\n\t\tbooks.add(book);\r\n\t}\r\n\r\n\tpublic void removeBook(Book book) {\r\n\t\tbooks.remove(book);\r\n\t}\r\n}\r\n<\/pre>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u672c\u3092\u5225\u306e\u672c\u68da\u306b\u79fb\u52d5\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u30a8\u30e9\u30fc\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3002 \u3053\u308c\u306f\u3001\u4eca\u306e\u72b6\u614b\u3067\u306f1\u518a\u306e\u672c\u3092\u8907\u6570\u306e\u672c\u68da\u306b\u5165\u308c\u3088\u3046\u3068\u3057\u3066\u3044\u308b\u3002 \u73fe\u5b9f\u7684\u306b\u4e0d\u53ef\u80fd\u306a\u3053\u3068\u3092\u3084\u308d\u3046\u3068\u3057\u3066\u3044\u308b\u306e\u3067\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002 \u672c\u68da\u3092\u79fb\u52d5\u3059\u308b\u305f\u3081\u306b\u306f &#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[3],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-springboot","tag-springboot"],"_links":{"self":[{"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":3,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":33,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/33"}],"wp:attachment":[{"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}