{"id":9,"date":"2022-11-17T13:38:25","date_gmt":"2022-11-17T04:38:25","guid":{"rendered":"https:\/\/kpc2022.satoshis.jp\/?p=9"},"modified":"2022-11-17T16:21:54","modified_gmt":"2022-11-17T07:21:54","slug":"%e6%9c%ac%e6%a3%9a%e3%82%a2%e3%83%97%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%ae%e4%bd%9c%e6%88%90","status":"publish","type":"post","link":"https:\/\/kpc2022.satoshis.jp\/?p=9","title":{"rendered":"\u672c\u68da\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u4f5c\u6210"},"content":{"rendered":"<p>SpringBoot\u3092\u4f7f\u7528\u3057\u3066\u672c\u68da\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<p>\u65b0\u898f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067 spring\u30b9\u30bf\u30fc\u30bf\u30fc\u30d7\u30ed\u30b8\u30a7\u30af\u30c8 \u3092\u4f5c\u6210\u3057\u3066\u3001pom.xml \u306e\u4ee5\u4e0b\u306e\u7b87\u6240\u3092\u4fee\u6b63\u3059\u308b\u3002<\/p>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d: bookshelf<br \/>\n\u30d1\u30c3\u30b1\u30fc\u30b8: jp.bookshelf<\/p>\n<p>pom.xml<\/p>\n<pre class=\"brush: xml; highlight: [8]; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;&gt;\r\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n\t&lt;parent&gt;\r\n\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n\t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\r\n\t\t&lt;version&gt;2.2.1.RELEASE&lt;\/version&gt;\r\n\t\t&lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;\r\n\t&lt;\/parent&gt;\r\n\t&lt;groupId&gt;jp.bookshelf&lt;\/groupId&gt;\r\n\t&lt;artifactId&gt;bookshelf&lt;\/artifactId&gt;\r\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n\t&lt;name&gt;bookshelf&lt;\/name&gt;\r\n\t&lt;description&gt;Demo project for Spring Boot&lt;\/description&gt;\r\n\t&lt;properties&gt;\r\n\t\t&lt;java.version&gt;1.8&lt;\/java.version&gt;\r\n\t&lt;\/properties&gt;\r\n\t&lt;dependencies&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;\/artifactId&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;\/artifactId&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.hsqldb&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;hsqldb&lt;\/artifactId&gt;\r\n\t\t\t&lt;scope&gt;runtime&lt;\/scope&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;\r\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\t&lt;\/dependencies&gt;\r\n\r\n\t&lt;build&gt;\r\n\t\t&lt;plugins&gt;\r\n\t\t\t&lt;plugin&gt;\r\n\t\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n\t\t\t\t&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\r\n\t\t\t&lt;\/plugin&gt;\r\n\t\t&lt;\/plugins&gt;\r\n\t&lt;\/build&gt;\r\n\r\n&lt;\/project&gt;\r\n<\/pre>\n<h4>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u95a2\u3059\u308b\u8a2d\u5b9a<\/h4>\n<p>src\/main\/resources<br \/>\napplication.properties<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nspring.datasource.url=jdbc:hsqldb:file:.\/db\/bookshelf\r\nspring.datasource.username=SA\r\nspring.datasource.password=\r\nspring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver\r\nspring.jpa.hibernate.ddl-auto=update\r\n<\/pre>\n<p>src\/main\/java<\/p>\n<p>\/bookshelf\/{id} \u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u3089\u3001\u305d\u306e\u672c\u68da\u306e\u5185\u5bb9\u3092\u8868\u793a\u3059\u308b\u3002<\/p>\n<p>IndexController.java<\/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.PathVariable;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestParam;\r\nimport org.springframework.web.servlet.ModelAndView;\r\n\r\n@Controller\r\npublic class IndexController {\r\n\t@Autowired\r\n\tprivate BookshelfRepository repository;\r\n\r\n\t@RequestMapping(&quot;\/&quot;)\r\n\tpublic ModelAndView index(ModelAndView mav) {\r\n\t\tList&lt;Bookshelf&gt; list = repository.findAll();\r\n\t\tmav.setViewName(&quot;index&quot;);\r\n\t\tmav.addObject(&quot;list&quot;, list);\r\n\t\treturn mav;\r\n\t}\r\n\r\n\t@RequestMapping(&quot;\/new-bookshelf&quot;)\r\n\tpublic ModelAndView shindan2(ModelAndView mav,\r\n\t\t\t@RequestParam(&quot;name&quot;) String name) {\r\n\t\tBookshelf bookshelf = new Bookshelf();\r\n\t\tbookshelf.setName(name);\r\n\t\trepository.saveAndFlush(bookshelf);\r\n\t\tmav.setViewName(&quot;bookshelf&quot;);\r\n\t\tmav.addObject(&quot;bookshelf&quot;, bookshelf);\r\n\t\treturn mav;\r\n\t}\r\n\r\n\t@RequestMapping(&quot;\/bookshelf\/{id}&quot;)\r\n\tpublic ModelAndView bookshelf(ModelAndView mav,\r\n\t\t\t@PathVariable long id) {\r\n\t\tmav.setViewName(&quot;bookshelf&quot;);\r\n\t\tOptional&lt;Bookshelf&gt; data = repository.findById(id);\r\n\t\tmav.addObject(&quot;bookshelf&quot;, data.get());\r\n\t\treturn mav;\r\n\t}\r\n}\r\n<\/pre>\n<p>\u672c\u68da\u306e\u540d\u524d\u3092\u30ea\u30f3\u30af\u306b\u3057\u3066\u305d\u308c\u305e\u308c\u306e\u672c\u68da\u306b\u98db\u3079\u308b\u3088\u3046\u306b\u3059\u308b\u3002<br \/>\n\u672c\u306e\u4e00\u89a7\u3078\u306e\u30ea\u30f3\u30af\u3082\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<p>src\/main\/resources\/templates<br \/>\nindex.html<\/p>\n<pre class=\"brush: xml; highlight: [13,14,15,23]; 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\u68da&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;h1&gt;\u672c\u68da&lt;\/h1&gt;\r\n&lt;p&gt;\u672c\u68da\u306e\u30ea\u30b9\u30c8\u3092\u8868\u793a\u3057\u307e\u3059\u3002&lt;\/p&gt;\r\n\r\n&lt;ul th:each=&quot;bs : ${list}&quot;&gt;\r\n\t&lt;li&gt;\r\n\t\t&lt;a th:href=&quot;@{&#039;\/bookshelf\/&#039; + ${bs.id}}&quot; th:text=&quot;${bs.name}&quot;&gt;&lt;\/a&gt;\r\n\t&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n\r\n&lt;form class=&quot;center&quot; action=&quot;\/new-bookshelf&quot; method=&quot;post&quot;&gt;\r\n\t\u540d\u524d: &lt;input class=&quot;w100&quot; type=&quot;text&quot; name=&quot;name&quot; \/&gt;\r\n\t&lt;input type=&quot;submit&quot; value=&quot;\u8ffd\u52a0&quot; \/&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;h3&gt;&lt;a href=&quot;\/book&quot;&gt;\u672c\u306e\u4e00\u89a7&lt;\/a&gt;&lt;\/h3&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>src\/main\/java<br \/>\nBookController.java<\/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.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}\r\n<\/pre>\n<p>src\/main\/resources\/templates<br \/>\nbooks.html<\/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\u672c\u68da&lt;select name=&quot;bookshelf&quot;&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 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\u79fb\u52d5\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&lt;\/td&gt;\r\n\t&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>books.html \u306b form\u30bf\u30b0\u3092\u8ffd\u52a0\u3057\u3066\u3001\u6307\u5b9a\u3057\u305f\u672c\u68da\u3078\u306e\u79fb\u52d5\u3092\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; highlight: [39,40,41,42,43,44,45,46,47]; 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&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;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\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>src\/main\/java<br \/>\nBookController.java<\/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\tOptional&lt;Bookshelf&gt; bs = bookshelfRepository.findById(bookshelfId);\r\n\t\tBookshelf bookshelf = bs.get();\r\n\t\tbookshelf.addBook(book);\r\n\t\tbookshelfRepository.saveAndFlush(bookshelf);\r\n\t\treturn new ModelAndView(&quot;redirect:\/bookshelf\/&quot; + bookshelfId);\r\n\t}\r\n}\r\n<\/pre>\n<p>src\/main\/resources\/templates<br \/>\nbookshelf.html<\/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\u68da&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;h1&gt;\u672c\u68da : &lt;span th:text=&quot;${bookshelf.name}&quot;&gt;&lt;\/span&gt;&lt;\/h1&gt;\r\n&lt;p&gt;\u3053\u306e\u672c\u68da\u306b\u5165\u3063\u3066\u3044\u308b\u672c\u306e\u30ea\u30b9\u30c8&lt;\/p&gt;\r\n\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;\r\n\t&lt;\/tr&gt;\r\n\t&lt;tr th:each=&quot;book : ${bookshelf.books}&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&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<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>SpringBoot\u3092\u4f7f\u7528\u3057\u3066\u672c\u68da\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3059\u308b\u3002 \u65b0\u898f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067 spring\u30b9\u30bf\u30fc\u30bf\u30fc\u30d7\u30ed\u30b8\u30a7\u30af\u30c8 \u3092\u4f5c\u6210\u3057\u3066\u3001pom.xml \u306e\u4ee5\u4e0b\u306e\u7b87\u6240\u3092\u4fee\u6b63\u3059\u308b\u3002 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d: book &#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-9","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\/9","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=9"}],"version-history":[{"count":12,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kpc2022.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}