{"id":1071,"date":"2019-07-15T22:38:15","date_gmt":"2019-07-15T13:38:15","guid":{"rendered":"https:\/\/ito-u-oti.com\/?p=1071"},"modified":"2020-07-10T17:33:38","modified_gmt":"2020-07-10T08:33:38","slug":"post-1071","status":"publish","type":"post","link":"https:\/\/ito-u-oti.com\/?p=1071","title":{"rendered":"\u3010SpringBoot\u3011\u3010MyBatis\u3011SQL\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3067\u81ea\u52d5\u751f\u6210\u3055\u308c\u308b\u30c7\u30fc\u30bf\u3092Entitiy\u306b\u683c\u7d0d\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p>SQL\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5229\u7528\u3057\u3066\u3001\u30c7\u30fc\u30bf\u3092INSERT\u3059\u308b\u6642\u306b\u4e3b\u30ad\u30fc\u306a\u3069\u3092\u81ea\u52d5\u3067\u5272\u308a\u5f53\u3066\u308b\u3068\u3044\u3063\u305f\u6642\u306b\u3001\u5272\u308a\u5f53\u3066\u305f\u5024\u3092Entity\u306b\u683c\u7d0d\u3057\u305f\u3044\u5834\u5408\u306e\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__1\" class=\"wp-block-heading\">\u524d\u63d0<\/h2>\n\n\n\n<h3 id=\"outline__1_1\" class=\"wp-block-heading\">INSERT\u5bfe\u8c61\u30c6\u30fc\u30d6\u30eb\u60c5\u5831<\/h3>\n\n\n\n<p><a href=\"http:\/\/www.postgresqltutorial.com\/postgresql-sample-database\/\">postgresql\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u30c6\u30fc\u30d6\u30eb\u7fa4<\/a>\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h4 id=\"outline__1_1_1\" class=\"wp-block-heading\">\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\uff08Address\u30c6\u30fc\u30d6\u30eb\uff09<\/h4>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/ito-u-oti.com\/wp-content\/uploads\/2019\/07\/image.png\" alt=\"\" class=\"wp-image-1072\" width=\"507\" height=\"208\"\/><figcaption>address_id\u304c\u4e3b\u30ad\u30fc\u3067\u3001\u4eca\u56de\u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u3088\u3063\u3066\u81ea\u52d5\u3067\u5024\u3092\u5272\u308a\u5f53\u3066\u307e\u3059\u3002<\/figcaption><\/figure>\n\n\n\n<h4 id=\"outline__1_1_2\" class=\"wp-block-heading\">\u30b7\u30fc\u30b1\u30f3\u30b9\u30c7\u30fc\u30bf<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE SEQUENCE public.address_address_id_seq\n\tINCREMENT BY 1\n\tMINVALUE 1\n\tMAXVALUE 9223372036854775807\n\tSTART 1\n\tCACHE 1\n\tNO CYCLE;\n<\/pre><\/div>\n\n\n<h2 id=\"outline__2\" class=\"wp-block-heading\">\u5b9f\u88c5<\/h2>\n\n\n\n<h3 id=\"outline__2_1\" class=\"wp-block-heading\">\u30d7\u30ed\u30c0\u30af\u30c8\u30b3\u30fc\u30c9<\/h3>\n\n\n\n<h4 id=\"outline__2_1_1\" class=\"wp-block-heading\">Address.java<\/h4>\n\n\n\n<p>INSERT\u3059\u308bEntity\u3067\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Data\n@NoArgsConstructor\n@AllArgsConstructor\n@Builder\npublic class Address {\n\n    int addressId;\n    String address;\n    String address2;\n    String district;\n    int cityId;\n    String postalCode;\n    String phone;\n    LocalDateTime lastUpdate;\n}\n<\/pre><\/div>\n\n\n<h4 id=\"outline__2_1_2\" class=\"wp-block-heading\">AddressMapper.java<\/h4>\n\n\n\n<p>Mybatis\u3092\u5229\u7528\u3057\u3066Address\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u3092INSERT\u3057\u307e\u3059\u3002<br>\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u30d9\u30fc\u30b9\u3067\u306e\u5b9f\u88c5\u3067\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Mapper\npublic interface AddressMapper {\n\n\n    @Insert(&quot;INSERT INTO address (&quot; +\n            &quot;address_id,&quot; +\n            &quot;address,&quot; +\n            &quot;address2,&quot; +\n            &quot;district,&quot; +\n            &quot;city_id,&quot; +\n            &quot;postal_code,&quot; +\n            &quot;phone,&quot; +\n            &quot;last_update&quot; +\n            &quot;)&quot; +\n            &quot;VALUES (&quot; +\n            &quot;address_address_id_seq.NEXTVAL,&quot; +\n            &quot;#{address.address},&quot; +\n            &quot;#{address.address2},&quot; +\n            &quot;#{address.district},&quot; +\n            &quot;#{address.cityId},&quot; +\n            &quot;#{address.postalCode},&quot; +\n            &quot;#{address.phone},&quot; +\n            &quot;#{address.lastUpdate}&quot; +\n            &quot;)&quot;)\n    @Options(useGeneratedKeys = true, keyColumn = &quot;address_id&quot;, keyProperty = &quot;address.addressId&quot;)\n    void save(@Param(&quot;address&quot;) Address address);\n}\n<\/pre><\/div>\n\n\n<p>\u4e3b\u30ad\u30fc\u3067\u3042\u308baddress_id\u3092\u3001\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u4f7f\u7528\u3057\u3066\u5024\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>@Options\u3067\u6307\u5b9a\u3057\u3066\u3044\u308b\u306e\u304c\u81ea\u52d5\u751f\u6210\u3055\u308c\u305faddress_id\u3092Entity\u306eaddress\u306b\u683c\u7d0d\u3059\u308b\u8a2d\u5b9a\u3067\u3059\u3002<\/p>\n\n\n\n<p>useGeneratedKeys\u30fb\u30fb\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5074\u3067\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u30ad\u30fc\u3092\u53d6\u5f97\u3059\u308b\u3002\u5b9f\u969b\u306b\u306fJDBC\u306egetGeneratedKeys\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3002<br>keyColumn\u30fb\u30fb\u30fb\u30c6\u30fc\u30d6\u30eb\u5185\u3067\u81ea\u52d5\u751f\u6210\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5217\u540d\u3002\u30ab\u30f3\u30de\u533a\u5207\u308a\u3067\u8907\u6570\u6307\u5b9a\u5bfe\u5fdc\u3002<br>keyProperty\u30fb\u30fb\u30fbkeyColumn\u3067\u6307\u5b9a\u3055\u308c\u305f\u5217\u540d\u3092\u3053\u3053\u3067\u6307\u5b9a\u3057\u305f\u30d7\u30ed\u30d1\u30c6\u30a3\u306b\u30bb\u30c3\u30c8\u3059\u308b\u3002\u30ab\u30f3\u30de\u533a\u5207\u308a\u3067\u8907\u6570\u6307\u5b9a\u5bfe\u5fdc\u3002<\/p>\n\n\n\n<h3 id=\"outline__2_2\" class=\"wp-block-heading\">\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9<\/h3>\n\n\n\n<h4 id=\"outline__2_2_1\" class=\"wp-block-heading\">AddressMapperTests.java<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nprivate AddressMapper target;\n\n@DisplayName(&quot;1\u4ef6Insert\u3057\u3001\u30b7\u30fc\u30b1\u30f3\u30b9\u306b\u3088\u3063\u3066\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u5024\u3092\u691c\u8a3c\u3059\u308b&quot;)\n        @Test\n        void test1Insert() {\n\n            \/\/GIVEN\n            Address actualCase = Address.builder()\n                    .address(&quot;613 Korolev Drive&quot;)\n                    .district(&quot;Shibuya&quot;)\n                    .cityId(1)\n                    .phone(&quot;28303384290&quot;)\n                    .lastUpdate(LocalDateTime.of(2015, 5, 2, 8, 50))\n                    .build();\n\n            \/\/WHEN\n            target.save(actualCase);\n\n            \/\/THEN\n            assertThat(actualCase.getAddressId()).isEqualTo(1);\n\n            int expect = jdbcTemplate.queryForObject(&quot;SELECT COUNT(*) FROM address&quot;, Integer.class);\n            assertThat(expect).isEqualTo(1);\n\n        }\n<\/pre><\/div>\n\n\n<p>INSERT\u3059\u308b\u30c7\u30fc\u30bf\u3092\u30bb\u30c3\u30c8\u3057\u3066\u3044\u308bactualCase\u3067\u3001\u4e3b\u30ad\u30fc\u3067\u3042\u308baddressId\u304c\u30bb\u30c3\u30c8\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\uff08int\u306a\u306e\u3067\u521d\u671f\u50240\u304c\u5165\u308a\u307e\u3059\u3002\uff09<\/p>\n\n\n\n<p>target.save\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u5f8c\u3001addressId\u30921\u3067assert\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3064\u307e\u308a\u3001\u30e1\u30bd\u30c3\u30c9\u5b9f\u884c\u5f8c\u306bactualCase\u306e\u4e2d\u306b\u81ea\u52d5\u751f\u6210\u3055\u308c\u305faddressId\u304c\u683c\u7d0d\u3055\u308c\u305f\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n\n\n\n<h2 id=\"outline__3\" class=\"wp-block-heading\">\u5099\u8003<\/h2>\n\n\n\n<h3 id=\"outline__3_1\" class=\"wp-block-heading\">@SelectKey\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3046\u65b9\u6cd5<\/h3>\n\n\n\n<p>@Options\u3067\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3068\u306f\u5225\u306b\u3001@SelectKey\u3067\u81ea\u52d5\u751f\u6210\u3059\u308b\u65b9\u6cd5\u3082\u3042\u308b\u3002<\/p>\n\n\n\n<p>\u305d\u306e\u5834\u5408\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n\n\n\n<h4 id=\"outline__2_1_2\" class=\"wp-block-heading\">AddressMapper.java<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    @Insert(&quot;INSERT INTO address (&quot; +\n            &quot;address_id,&quot; +\n            &quot;address,&quot; +\n            &quot;address2,&quot; +\n            &quot;district,&quot; +\n            &quot;city_id,&quot; +\n            &quot;postal_code,&quot; +\n            &quot;phone,&quot; +\n            &quot;last_update&quot; +\n            &quot;)&quot; +\n            &quot;VALUES (&quot; +\n            &quot;#{address.addressId},&quot; +\n            &quot;#{address.address},&quot; +\n            &quot;#{address.address2},&quot; +\n            &quot;#{address.district},&quot; +\n            &quot;#{address.cityId},&quot; +\n            &quot;#{address.postalCode},&quot; +\n            &quot;#{address.phone},&quot; +\n            &quot;#{address.lastUpdate}&quot; +\n            &quot;)&quot;)\n    @SelectKey(statement = &quot;SELECT address_address_id_seq.NEXTVAL&quot;,\n            keyProperty = &quot;address.addressId&quot;,\n            before = true,\n            resultType = Integer.class)\n    void save(@Param(&quot;address&quot;) Address address);\n<\/pre><\/div>\n\n\n<p>@SelectKey\u3067\u30b7\u30fc\u30b1\u30f3\u30b9\u3092SELECT\u3057\u3066\u3001\u4e8b\u524d\u306baddressId\u306b\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u5024\u3092\u683c\u7d0d\u3055\u305b\u3066\u3001\u305d\u308c\u3092\u5229\u7528\u3057\u3066INSERT\u3057\u3066\u3044\u308b\u3002<\/p>\n\n\n\n<p>\u306a\u306e\u3067\u3001INSERT\u306eVALUES\u53e5\u306eaddress_id\u306e\u90e8\u5206\u3082\u5909\u308f\u3063\u3066\u3044\u308b<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>@Options\u306e\u65b9\u6cd5\u2026SQL\u5074\u3067\u81ea\u52d5\u751f\u6210\u3055\u305b\u3001SQL\u304c\u5b9f\u884c\u3055\u308c\u305f\u5f8c\u3067\u5024\u3092Entity\u306b\u683c\u7d0d\u3059\u308b\u3002<\/li><li>@SelectKey\u306e\u65b9\u6cd5\u2026INSERT\u3088\u308a\u524d\u306bSELECT\u306b\u3088\u3063\u3066\u30b7\u30fc\u30b1\u30f3\u30b9\u304b\u3089\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u5024\u3092Entity\u306b\u683c\u7d0d\u3057\u3001\u305d\u306e\u30c7\u30fc\u30bf\u3092INSERT\u3057\u3066\u3044\u308b\u3002<\/li><\/ul>\n\n\n\n<p>\u3068\u3044\u3046\u611f\u3058\u3002<br>@SelectKey\u3060\u3068SQL\u304c\u4e00\u3064\u591a\u304f\u767a\u884c\u3055\u308c\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002<\/p>\n\n\n\n<h2 id=\"outline__4\" class=\"wp-block-heading\">\u53c2\u8003<\/h2>\n\n\n\n<p><a href=\"http:\/\/www.mybatis.org\/mybatis-3\/ja\/sqlmap-xml.html\">Mybatis\u516c\u5f0f<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL\u306e\u30b7\u30fc\u30b1\u30f3\u30b9\u3092\u5229\u7528\u3057\u3066\u3001\u30c7\u30fc\u30bf\u3092INSERT\u3059\u308b\u6642\u306b\u4e3b\u30ad\u30fc\u306a\u3069\u3092\u81ea\u52d5\u3067\u5272\u308a\u5f53\u3066\u308b\u3068\u3044\u3063\u305f\u6642\u306b\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":273,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,24],"tags":[255,254,13,253,238],"class_list":["post-1071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mybatis","category-spring","tag-options","tag-selectkey","tag-mybatis","tag-sequence","tag-spring-boot"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1071"}],"version-history":[{"count":3,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1071\/revisions"}],"predecessor-version":[{"id":1225,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/posts\/1071\/revisions\/1225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=\/wp\/v2\/media\/273"}],"wp:attachment":[{"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ito-u-oti.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}