{"id":118,"date":"2020-11-26T15:11:56","date_gmt":"2020-11-26T06:11:56","guid":{"rendered":"http:\/\/kpc2020.satoshis.jp\/?p=118"},"modified":"2020-11-26T16:01:46","modified_gmt":"2020-11-26T07:01:46","slug":"bookshelf%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b-update","status":"publish","type":"post","link":"https:\/\/kpc2020.satoshis.jp\/?p=118","title":{"rendered":"Bookshelf\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3059\u308b (Update)"},"content":{"rendered":"<p>CRUD\u306e\u3046\u3061\u3001Create\u3068Read\u306f\u52d5\u3044\u3066\u3044\u308b\u306e\u3067\u3001\u6b21\u306fUpdate\u3092\u4f5c\u3063\u3066\u307f\u308b\u3002<\/p>\n<p>BookContoller.java<\/p>\n<pre class=\"brush: java; first-line: 69; title: ; notranslate\" title=\"\">\r\n\t@RequestMapping(value = &quot;\/book\/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;editbook&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\tList&lt;Bookshelf&gt; list = bookshelfRepository.findAll();\r\n\t\tmav.addObject(&quot;bookshelfList&quot;, list);\r\n\t\treturn mav;\r\n\t}\r\n<\/pre>\n<p>editbook\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u4f5c\u6210\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;Book - \u672c\u306e\u7de8\u96c6&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;a href=&quot;\/&quot;&gt;\u30c8\u30c3\u30d7&lt;\/a&gt;\r\n\r\n&lt;h1&gt;\u672c\u306e\u7de8\u96c6&lt;\/h1&gt;\r\n\r\n&lt;form action=&quot;\/book\/edit&quot; method=&quot;post&quot;&gt;\r\n\t&lt;div&gt;\u30bf\u30a4\u30c8\u30eb: &lt;input type=&quot;text&quot; name=&quot;title&quot; th:value=&quot;${book.title}&quot; \/&gt;&lt;\/div&gt;\r\n\t&lt;div&gt;\u8457\u8005: &lt;input type=&quot;text&quot; name=&quot;author&quot; th:value=&quot;${book.author}&quot; \/&gt;&lt;\/div&gt;\r\n\t&lt;div&gt;&lt;input id=&quot;submit&quot; type=&quot;submit&quot; value=&quot;\u4fee\u6b63&quot; \/&gt;&lt;\/div&gt;\r\n&lt;\/form&gt;\r\n\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>\u30b5\u30fc\u30d0\u30fc\u3092\u518d\u8d77\u52d5\u3057\u3066 \/book\/edit\/{id} \u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001\u672c\u306e\u7de8\u96c6\u753b\u9762\u3092\u8868\u793a\u3059\u308b\u3002<\/p>\n<p>POST\u30e1\u30bd\u30c3\u30c9\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u53d7\u3051\u53d6\u308b\u3068\u3053\u308d\u306f\u4f5c\u3063\u3066\u306a\u3044\u306e\u3067\u300c\u4fee\u6b63\u300d\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002<\/p>\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067POST\u30e1\u30bd\u30c3\u30c9\u3092\u53d7\u3051\u53d6\u308c\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<pre class=\"brush: java; first-line: 80; title: ; notranslate\" title=\"\">\r\n\t@RequestMapping(value = &quot;\/book\/edit&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView edit(ModelAndView mav,\r\n\t\t\t@RequestParam(&quot;id&quot;) long id,\r\n\t\t\t@RequestParam(&quot;title&quot;) String title,\r\n\t\t\t@RequestParam(&quot;author&quot;) String author) {\r\n\t\tOptional&lt;Book&gt; data = repository.findById(id);\r\n\t\tBook book = data.get();\r\n\t\tbook.setTitle(title);\r\n\t\tbook.setAuthor(author);\r\n\t\trepository.saveAndFlush(book);\r\n\t\treturn new ModelAndView(&quot;redirect:\/book\/&quot; + id);\r\n\t}\r\n<\/pre>\n<p>\u518d\u8d77\u52d5\u3057\u3066\u300c\u4fee\u6b63\u300d\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u30a8\u30e9\u30fc\u8868\u793a\u306b\u306a\u308b\u3002<br \/>\n\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8aad\u3080\u3068\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u306bid\u304c\u5b58\u5728\u3057\u306a\u3044\u3068\u8a00\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u5165\u529b\u30d5\u30a9\u30fc\u30e0\u306b\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; highlight: [14]; 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;Book - \u672c\u306e\u7de8\u96c6&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;a href=&quot;\/&quot;&gt;\u30c8\u30c3\u30d7&lt;\/a&gt;\r\n\r\n&lt;h1&gt;\u672c\u306e\u7de8\u96c6&lt;\/h1&gt;\r\n\r\n&lt;form action=&quot;\/book\/edit&quot; method=&quot;post&quot;&gt;\r\n\t&lt;input type=&quot;hidden&quot; name=&quot;id&quot; th:value=&quot;${book.id}&quot; \/&gt;\r\n\t&lt;div&gt;\u30bf\u30a4\u30c8\u30eb: &lt;input type=&quot;text&quot; name=&quot;title&quot; th:value=&quot;${book.title}&quot; \/&gt;&lt;\/div&gt;\r\n\t&lt;div&gt;\u8457\u8005: &lt;input type=&quot;text&quot; name=&quot;author&quot; th:value=&quot;${book.author}&quot; \/&gt;&lt;\/div&gt;\r\n\t&lt;div&gt;&lt;input id=&quot;submit&quot; type=&quot;submit&quot; value=&quot;\u4fee\u6b63&quot; \/&gt;&lt;\/div&gt;\r\n&lt;\/form&gt;\r\n\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>book.html \u306b\u4fee\u6b63\u753b\u9762\u3078\u306e\u30ea\u30f3\u30af\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; highlight: [17]; 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;Book - \u672c\u3092\u5165\u308c\u308b\u672c\u68da\u3092\u6307\u5b9a\u3059\u308b&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;h1&gt;Book&lt;\/h1&gt;\r\n\r\n&lt;a href=&quot;\/&quot;&gt;\u30c8\u30c3\u30d7&lt;\/a&gt;\r\n\r\n&lt;table&gt;\r\n\t&lt;tr&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;&lt;a th:href=&quot;@{'\/book\/edit\/' + ${book.id}}&quot; th:text=&quot;\u4fee\u6b63&quot;&gt;&lt;\/a&gt;&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n:\r\n<\/pre>\n<p>\u672c\u306e\u753b\u50cf\u3092\u8868\u793a\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<pre class=\"brush: java; highlight: [27,28,65,66,67,68,69,70,71]; title: ; notranslate\" title=\"\">\r\npackage jp.kpc;\r\n\r\nimport javax.persistence.Column;\r\nimport javax.persistence.Entity;\r\nimport javax.persistence.GeneratedValue;\r\nimport javax.persistence.Id;\r\nimport javax.persistence.ManyToOne;\r\nimport javax.validation.constraints.NotEmpty;\r\nimport javax.validation.constraints.NotNull;\r\n\r\n@Entity\r\npublic class Book {\r\n\t@Id\r\n\t@GeneratedValue\r\n\t@Column\r\n\t@NotNull\r\n\tprivate long id;\r\n\r\n\t@Column\r\n\t@NotEmpty\r\n\tprivate String title;\r\n\r\n\t@Column\r\n\t@NotEmpty\r\n\tprivate String author;\r\n\r\n\t@Column\r\n\tprivate String image;\r\n\r\n\t@ManyToOne\r\n\tprivate Bookshelf bookshelf;\r\n\r\n\tpublic long getId() {\r\n\t\treturn id;\r\n\t}\r\n\r\n\tpublic void setId(long id) {\r\n\t\tthis.id = id;\r\n\t}\r\n\r\n\tpublic String getTitle() {\r\n\t\treturn title;\r\n\t}\r\n\r\n\tpublic void setTitle(String title) {\r\n\t\tthis.title = title;\r\n\t}\r\n\r\n\tpublic String getAuthor() {\r\n\t\treturn author;\r\n\t}\r\n\r\n\tpublic void setAuthor(String author) {\r\n\t\tthis.author = author;\r\n\t}\r\n\r\n\tpublic Bookshelf getBookshelf() {\r\n\t\treturn bookshelf;\r\n\t}\r\n\r\n\tpublic void setBookshelf(Bookshelf bookshelf) {\r\n\t\tthis.bookshelf = bookshelf;\r\n\t}\r\n\r\n\tpublic String getImage() {\r\n\t\treturn image;\r\n\t}\r\n\r\n\tpublic void setImage(String image) {\r\n\t\tthis.image = image;\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3067\u753b\u50cfURL\u3082\u4fdd\u5b58\u3059\u308b\u3088\u3046\u306b\u4fee\u6b63\u3059\u308b\u3002<\/p>\n<pre class=\"brush: java; first-line: 80; highlight: [85,90]; title: ; notranslate\" title=\"\">\r\n\t@RequestMapping(value = &quot;\/book\/edit&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView edit(ModelAndView mav,\r\n\t\t\t@RequestParam(&quot;id&quot;) long id,\r\n\t\t\t@RequestParam(&quot;title&quot;) String title,\r\n\t\t\t@RequestParam(&quot;author&quot;) String author,\r\n\t\t\t@RequestParam(&quot;image&quot;) String image) {\r\n\t\tOptional&lt;Book&gt; data = repository.findById(id);\r\n\t\tBook book = data.get();\r\n\t\tbook.setTitle(title);\r\n\t\tbook.setAuthor(author);\r\n\t\tbook.setImage(image);\r\n\t\trepository.saveAndFlush(book);\r\n\t\treturn new ModelAndView(&quot;redirect:\/book\/&quot; + id);\r\n\t}\r\n<\/pre>\n<p>editbook.html \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u5185\u3067\u753b\u50cf\u3092\u8868\u793a\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; highlight: [13]; 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;Book - \u672c\u306e\u7de8\u96c6&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;a href=&quot;\/&quot;&gt;\u30c8\u30c3\u30d7&lt;\/a&gt;\r\n\r\n&lt;h1&gt;\u672c\u306e\u7de8\u96c6&lt;\/h1&gt;\r\n\r\n&lt;img th:src=&quot;${book.image}&quot; width=&quot;200&quot; \/&gt;\r\n\r\n&lt;form action=&quot;\/book\/edit&quot; method=&quot;post&quot;&gt;\r\n\t&lt;input type=&quot;hidden&quot; name=&quot;id&quot; th:value=&quot;${book.id}&quot; \/&gt;\r\n\t&lt;div&gt;\u30bf\u30a4\u30c8\u30eb: &lt;input type=&quot;text&quot; name=&quot;title&quot; th:value=&quot;${book.title}&quot; \/&gt;&lt;\/div&gt;\r\n\t&lt;div&gt;\u8457\u8005: &lt;input type=&quot;text&quot; name=&quot;author&quot; th:value=&quot;${book.author}&quot; \/&gt;&lt;\/div&gt;\r\n\t&lt;div&gt;\u753b\u50cfURL: &lt;input type=&quot;text&quot; name=&quot;image&quot; th:value=&quot;${book.image}&quot; \/&gt;&lt;\/div&gt;\r\n\t&lt;div&gt;&lt;input id=&quot;submit&quot; type=&quot;submit&quot; value=&quot;\u4fee\u6b63&quot; \/&gt;&lt;\/div&gt;\r\n&lt;\/form&gt;\r\n\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>book.html \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u3082\u753b\u50cf\u3092\u8ffd\u52a0\u3002<\/p>\n<pre class=\"brush: xml; highlight: [15]; 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;Book - \u672c\u3092\u5165\u308c\u308b\u672c\u68da\u3092\u6307\u5b9a\u3059\u308b&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;h1&gt;Book&lt;\/h1&gt;\r\n\r\n&lt;a href=&quot;\/&quot;&gt;\u30c8\u30c3\u30d7&lt;\/a&gt;\r\n\r\n\r\n&lt;table&gt;\r\n\t&lt;tr&gt;&lt;td&gt;&lt;img th:src=&quot;${book.image}&quot; width=&quot;100&quot; \/&gt;&lt;\/td&gt;&lt;\/tr&gt;\r\n\t&lt;tr&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;&lt;a th:href=&quot;@{'\/book\/edit\/' + ${book.id}}&quot; th:text=&quot;\u4fee\u6b63&quot;&gt;&lt;\/a&gt;&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n:\r\n<\/pre>\n<p>books.html \u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306b\u3082\u753b\u50cf\u3092\u8ffd\u52a0\u3002<\/p>\n<pre class=\"brush: xml; highlight: [15,19]; 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;Book - \u672c\u306e\u30ea\u30b9\u30c8&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;h1&gt;Book list&lt;\/h1&gt;\r\n\r\n&lt;a href=&quot;\/&quot;&gt;\u30c8\u30c3\u30d7&lt;\/a&gt;\r\n\r\n&lt;table&gt;\r\n\t&lt;tr&gt;\r\n\t\t&lt;th&gt;ID&lt;\/th&gt;&lt;th&gt;\u753b\u50cf&lt;\/th&gt;&lt;th&gt;\u30bf\u30a4\u30c8\u30eb&lt;\/th&gt;&lt;th&gt;\u8457\u8005&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 th:text=&quot;${book.id}&quot;&gt;&lt;\/td&gt;\r\n\t\t&lt;td&gt;&lt;img th:src=&quot;${book.image}&quot; height=&quot;50&quot; \/&gt;&lt;\/td&gt;\r\n\t\t&lt;td&gt;&lt;a th:href=&quot;@{'\/book\/' + ${book.id}}&quot; th:text=&quot;${book.title}&quot;&gt;&lt;\/a&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 th:if=&quot;${book.bookshelf != null}&quot; th:text=&quot;${book.bookshelf.name}&quot;&gt;&lt;\/td&gt;\r\n\t\t&lt;td th:if=&quot;${book.bookshelf == null}&quot; th:text=&quot;\u672c\u68da\u306b\u5165\u308c\u3066\u307e\u305b\u3093&quot;&gt;&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n&lt;\/table&gt;\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>CRUD\u306e\u3046\u3061\u3001Create\u3068Read\u306f\u52d5\u3044\u3066\u3044\u308b\u306e\u3067\u3001\u6b21\u306fUpdate\u3092\u4f5c\u3063\u3066\u307f\u308b\u3002 BookContoller.java @RequestMapping(value = &quot;\/book\/edit\/{id}&#038;q &hellip; <a href=\"https:\/\/kpc2020.satoshis.jp\/?p=118\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">Bookshelf\u30a2\u30d7\u30ea\u3092\u4f5c\u6210\u3059\u308b (Update)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-java","category-springboot"],"views":252,"_links":{"self":[{"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=118"}],"version-history":[{"count":8,"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":126,"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions\/126"}],"wp:attachment":[{"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kpc2020.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}