{"id":266,"date":"2019-08-22T10:11:02","date_gmt":"2019-08-22T01:11:02","guid":{"rendered":"http:\/\/2019se3.satoshis.jp\/?p=266"},"modified":"2019-08-22T11:15:14","modified_gmt":"2019-08-22T02:15:14","slug":"8%e6%9c%8822%e6%97%a5","status":"publish","type":"post","link":"https:\/\/2019se3.satoshis.jp\/?p=266","title":{"rendered":"8\u670822\u65e5"},"content":{"rendered":"<h3>Bookshelf\u3092\u767b\u9332\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b<\/h3>\n<p>Repository\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<p>src\/main\/java\/<br \/>\n\u2514jp.abc<br \/>\n\u3000\u2514BookshelfRepository.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage jp.abc;\r\n\r\nimport org.springframework.data.jpa.repository.JpaRepository;\r\n\r\npublic interface BookshelfRepository extends JpaRepository&lt;Bookshelf, Long&gt; {\r\n\r\n}\r\n<\/pre>\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u304cPOST\u30e1\u30bd\u30c3\u30c9\u3092\u53d7\u3051\u53d6\u308c\u308b\u3088\u3046\u306b\u3059\u308b\u3002<\/p>\n<p>src\/main\/java\/<br \/>\n\u2514jp.abc<br \/>\n\u3000\u2514BookshelfController.java<\/p>\n<pre class=\"brush: java; highlight: [15,16,22,26,27,28,29,30,31,32,33,34,35,36,37]; title: ; notranslate\" title=\"\">\r\npackage jp.abc;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.validation.Errors;\r\nimport org.springframework.validation.annotation.Validated;\r\nimport org.springframework.web.bind.annotation.ModelAttribute;\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 BookshelfController {\r\n\r\n\t@Autowired\r\n\tprivate BookshelfRepository repository;\r\n\r\n\t@RequestMapping(value = &quot;\/bookshelf&quot;, method = RequestMethod.GET)\r\n\tpublic ModelAndView index(ModelAndView mav) {\r\n\t\tmav.setViewName(&quot;bookshelf&quot;);\r\n\t\tmav.addObject(&quot;formModel&quot;, new Bookshelf());\r\n\t\tmav.addObject(&quot;datalist&quot;, repository.findAll());\r\n\t\treturn mav;\r\n\t}\r\n\r\n\t@RequestMapping(value = &quot;\/bookshelf&quot;, method = RequestMethod.POST)\r\n\tpublic ModelAndView post(\r\n\t\t\t@ModelAttribute(&quot;formModel&quot;) @Validated Bookshelf bookshelf,\r\n\t\t\tErrors errors,\r\n\t\t\tModelAndView mav) {\r\n\t\tif (errors.hasErrors()) {\r\n\t\t\tmav.addObject(&quot;msg&quot;, &quot;\u30a8\u30e9\u30fc\u3067\u3059\u3088&quot;);\r\n\t\t\treturn mav;\r\n\t\t}\r\n\t\trepository.saveAndFlush(bookshelf);\r\n\t\treturn new ModelAndView(&quot;redirect:\/bookshelf&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u8a2d\u5b9a\u3092 application.properties \u306b\u8a18\u8ff0\u3059\u308b\u3002<\/p>\n<p>src\/main\/resources\/<br \/>\n\u2514application.properties<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nspring.datasource.url=jdbc:hsqldb:hsql:\/\/localhost\/bookshelf\r\nspring.datasource.username=sa\r\nspring.datasource.password=\r\nspring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver\r\nspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.HSQLDialect\r\nspring.jpa.hibernate.ddl-auto=update\r\n<\/pre>\n<p>\u672c\u68da\u306b\u53ce\u7d0d\u3059\u308b\u305f\u3081\u306e\u672c\u306e\u30af\u30e9\u30b9 Book \u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<p>src\/main\/java\/<br \/>\n\u2514jp.abc<br \/>\n\u3000\u2514Book.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage jp.abc;\r\n\r\nimport javax.persistence.Column;\r\nimport javax.persistence.Entity;\r\nimport javax.persistence.GeneratedValue;\r\nimport javax.persistence.GenerationType;\r\nimport javax.persistence.Id;\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(strategy = GenerationType.AUTO)\r\n\t@Column\r\n\t@NotNull\r\n\tprivate long id;\r\n\r\n\t@Column(length = 200, nullable = false)\r\n\t@NotEmpty\r\n\tprivate String title;\r\n\r\n\t@Column(length = 200, nullable = false)\r\n\t@NotEmpty\r\n\tprivate String author;\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}\r\n<\/pre>\n<p>Book\u7528\u306eHTML\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<p>src\/main\/resources\/<br \/>\n\u2514templates<br \/>\n\u3000\u2514book.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;Book&lt;\/title&gt;\r\n&lt;style type=&quot;text\/css&quot;&gt;\r\nh1 {\r\n  font-size: 18pt;\r\n  font-weight: bold;\r\n  color: gray;\r\n}\r\nbody {\r\n  font-size: 13pt;\r\n  color: gray;\r\n  margin: 5px 25px;\r\n}\r\ntr {\r\n  margin: 5px;\r\n}\r\nth {\r\n  padding: 5px;\r\n  color: white;\r\n  background: darkgray;\r\n}\r\ntd {\r\n  padding: 5px;\r\n  color: black;\r\n  background: #f0f0f0;\r\n}\r\n.err {\r\n  color: red;\r\n}\r\n&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h1&gt;Book&lt;\/h1&gt;\r\n&lt;p th:text=&quot;${msg}&quot;&gt;&lt;\/p&gt;\r\n&lt;form method=&quot;post&quot; action=&quot;\/book&quot; th:object=&quot;${formModel}&quot;&gt;\r\n  &lt;table&gt;\r\n    &lt;tr&gt;\r\n      &lt;td&gt;&lt;label for=&quot;title&quot;&gt;\u30bf\u30a4\u30c8\u30eb&lt;\/label&gt;&lt;\/td&gt;\r\n      &lt;td&gt;\r\n        &lt;input type=&quot;text&quot; name=&quot;title&quot; th:value=&quot;*{title}&quot;\r\n      \t\t\tth:errorclass=&quot;err&quot; \/&gt;\r\n      \t&lt;div th:if=&quot;${#fields.hasErrors('title')}&quot; th:errors=&quot;*{title}&quot;\r\n      \t\tth:errorclass=&quot;err&quot;&gt;&lt;\/div&gt;\r\n      &lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n    &lt;tr&gt;\r\n      &lt;td&gt;&lt;label for=&quot;author&quot;&gt;\u8457\u8005&lt;\/label&gt;&lt;\/td&gt;\r\n      &lt;td&gt;\r\n        &lt;input type=&quot;text&quot; name=&quot;author&quot; th:value=&quot;*{author}&quot;\r\n      \t\t\tth:errorclass=&quot;err&quot; \/&gt;\r\n      \t&lt;div th:if=&quot;${#fields.hasErrors('author')}&quot; th:errors=&quot;*{author}&quot;\r\n      \t\tth:errorclass=&quot;err&quot;&gt;&lt;\/div&gt;\r\n      &lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n    &lt;tr&gt;\r\n      &lt;td&gt;&lt;\/td&gt;\r\n      &lt;td&gt;&lt;input type=&quot;submit&quot; \/&gt;&lt;\/td&gt;\r\n    &lt;\/tr&gt;\r\n  &lt;\/table&gt;\r\n&lt;\/form&gt;\r\n\r\n&lt;hr \/&gt;\r\n&lt;table&gt;\r\n  &lt;tr&gt;\r\n    &lt;th&gt;ID&lt;\/th&gt;&lt;th&gt;\u30bf\u30a4\u30c8\u30eb&lt;\/th&gt;&lt;th&gt;\u8457\u8005&lt;\/th&gt;\r\n  &lt;\/tr&gt;\r\n  &lt;tr th:each=&quot;obj : ${datalist}&quot;&gt;\r\n    &lt;td th:text=&quot;${obj.id}&quot;&gt;&lt;\/td&gt;\r\n    &lt;td th:text=&quot;${obj.title}&quot;&gt;&lt;\/td&gt;\r\n    &lt;td th:text=&quot;${obj.author}&quot;&gt;&lt;\/td&gt;\r\n  &lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Book\u7528\u306e\u30ea\u30dd\u30b8\u30c8\u30ea\u3092\u4f5c\u6210\u3059\u308b\u3002<br \/>\nsrc\/main\/java\/<br \/>\n\u2514jp.abc<br \/>\n\u3000\u2514BookRepository.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage jp.abc;\r\n\r\nimport org.springframework.data.jpa.repository.JpaRepository;\r\n\r\npublic interface BookRepository extends JpaRepository&lt;Book, Long&gt; {\r\n\r\n}\r\n<\/pre>\n<p>Book\u7528\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u3092\u4f5c\u6210\u3059\u308b\u3002<\/p>\n<p>src\/main\/java\/<br \/>\n\u2514jp.abc<br \/>\n\u3000\u2514BookController.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage jp.abc;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.validation.Errors;\r\nimport org.springframework.validation.annotation.Validated;\r\nimport org.springframework.web.bind.annotation.ModelAttribute;\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\r\n\t@Autowired\r\n\tprivate BookRepository repository;\r\n\r\n\t@RequestMapping(value = &quot;\/book&quot;, method = RequestMethod.GET)\r\n\tpublic ModelAndView index(ModelAndView mav) {\r\n\t\tmav.setViewName(&quot;book&quot;);\r\n\t\tmav.addObject(&quot;formModel&quot;, new Book());\r\n\t\tmav.addObject(&quot;datalist&quot;, repository.findAll());\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(\r\n\t\t\t@ModelAttribute(&quot;formModel&quot;) @Validated Book book,\r\n\t\t\tErrors errors,\r\n\t\t\tModelAndView mav) {\r\n\t\tif (errors.hasErrors()) {\r\n\t\t\tmav.addObject(&quot;msg&quot;, &quot;\u30a8\u30e9\u30fc\u3067\u3059\u3088&quot;);\r\n\t\t\treturn mav;\r\n\t\t}\r\n\t\trepository.saveAndFlush(book);\r\n\t\treturn new ModelAndView(&quot;redirect:\/book&quot;);\r\n\t}\r\n}\r\n<\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Bookshelf\u3092\u767b\u9332\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b Repository\u3092\u4f5c\u6210\u3059\u308b\u3002 src\/main\/java\/ \u2514jp.abc \u3000\u2514BookshelfRepository.java \u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u304cPOST\u30e1\u30bd\u30c3\u30c9\u3092\u53d7\u3051\u53d6\u308c\u308b\u3088 &hellip; <a href=\"https:\/\/2019se3.satoshis.jp\/?p=266\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;8\u670822\u65e5&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,3],"tags":[],"_links":{"self":[{"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/266"}],"collection":[{"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=266"}],"version-history":[{"count":10,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":276,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions\/276"}],"wp:attachment":[{"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2019se3.satoshis.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}