{"id":155802,"date":"2020-09-09T05:23:51","date_gmt":"2020-09-08T21:23:51","guid":{"rendered":"http:\/\/4563.org\/?p=155802"},"modified":"2020-09-09T05:23:51","modified_gmt":"2020-09-08T21:23:51","slug":"mybatispgsql-%e6%89%a7%e8%a1%8c-insert-%e5%92%8c-update-%e4%b9%8b%e5%90%8e%e8%bf%94%e5%9b%9e-id","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=155802","title":{"rendered":"mybatis+pgsql \u6267\u884c insert \u548c update \u4e4b\u540e\u8fd4\u56de id"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  mybatis+pgsql \u6267\u884c insert \u548c update \u4e4b\u540e\u8fd4\u56de id               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jiobanma <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u770b\u5230\u7f51\u4e0a\u662f\u8fd9\u4e48\u5199\u7684<\/p>\n<pre><code>&lt;insert id=\"insertUser\"&gt;     &lt;selectKey resultType=\"int\"  order=\"AFTER\" keyProperty=\"pid\" &gt;         SELECT currval('tbl_user_pid_seq'::regclass) AS pid     &lt;\/selectKey&gt;     insert into tbl_user(name, age) values(#{name}, #{age}) &lt;\/insert&gt; <\/code><\/pre>\n<p>tbl_user_pid_seq \u662f\u4e00\u4e2a\u5e8f\u5217\uff0c\u4f46\u662f\u81ea\u5df1\u7684\u5e93 id \u6ca1\u6709\u5e8f\u5217\uff0c\u662f\u81ea\u5df1\u5199\u4e86\u4e00\u4e2a next_id \u7684\u51fd\u6570\u3002 \u5e76\u4e14\u56e0\u4e3a\u9700\u8981\uff0cmybatis \u7684 parameterType \u4e5f\u4e0d\u662f\u4e00\u4e2a entity\uff0c\u800c\u662f\u4e00\u4e2a Map \u8fd9\u79cd\u65f6\u5019 xml \u8be5\u600e\u4e48\u5199\u624d\u80fd\u62ff\u5230 insert \u4e4b\u540e\u7684 id<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3301917\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chihiro2014 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 JPA \u5c31\u53ef\u4ee5\u6446\u8131 XML \u5730\u72f1\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3301918\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ourslay <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bd5\u8bd5 spring data jdbc                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3301919\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yongle1117 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e4b\u524d\u9047\u5230\u8fc7\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u7684\u89e3\u51b3\u529e\u6cd5\u662f\u5728 entity \u4e0a<br \/>@Id<br \/> @KeySql(genSql = PostgreGenSql.class, order = ORDER.AFTER)<br \/> @Column(insertable = false)<br \/> private Long id;<\/p>\n<p>public class PostgreGenSql implements GenSql {<\/p>\n<p> private static final String SQL = &#8220;SELECT currval(&#8216;%s_id_seq&#8217;::regclass)&#8221;;<\/p>\n<p> @Override<br \/> public String genSql(EntityTable entityTable, EntityColumn entityColumn) {<br \/> return String.format(SQL, entityTable.getName().toUpperCase());<br \/> }<\/p>\n<p>}                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3301920\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Takamine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u2026\u2026\u53ef\u4ee5\u8bd5\u8bd5\u4e00\u4e2a\u9ed1\u9b54\u6cd5\uff0c\u628a&lt;insert&gt;\u6807\u7b7e\u6539\u6210&lt;select&gt;\u6807\u7b7e\u4f7f\u7528 pg \u7684 returning \u6765\u505a\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3301921\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : jiobanma <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Takamine #4 \u6211\u5728\u7f51\u4e0a\u4e5f\u627e\u5230\u8fd9\u4e2a\u9ed1\u9b54\u6cd5\u4e86\uff0c\u73b0\u5728\u7528\u7684\u5c31\u662f\u8fd9\u4e2a\uff0c\u4f46\u662f\u4e0d\u77e5\u9053\u4f1a\u4e0d\u4f1a\u6709\u4ec0\u4e48\u5176\u4ed6\u9690\u60a3\uff0c\u4f46\u662f\u8fd9\u6837\u505a\u786e\u5b9e\u80fd\u6ee1\u8db3\u6211\u7684\u9700\u6c42                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3301922\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Takamine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @jiobanma \u6211\u89c9\u5f97\u5e94\u8be5\u6ca1\u6709\u95ee\u9898\uff08\u56e0\u4e3a\u6211\u4e5f\u8fd9\u4e48\u5e72\u8fc7 2333 \uff09\uff0c\u672c\u8d28\u4e0a mybatis \u62e6\u622a\u5e94\u8be5\u8fd8\u662f\u770b\u7684\u6267\u884c sql\uff0c\u6807\u7b7e\u8fd9\u4e2a\u53ea\u662f\u76ee\u524d mybatis \u4e0d\u652f\u6301\u8bed\u6cd5\u7684\u95ee\u9898\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3301923\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : jiobanma <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Takamine #6 \u54c8\u54c8\u54c8 \u53cd\u6b63\u4e5f\u6ca1\u6709\u627e\u5230\u5176\u4ed6\u597d\u7684\u65b9\u6cd5 \u5148\u8fd9\u4e48\u51d1\u5408\u7740\u7528\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>mybatis+pgsql \u6267\u884c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/155802"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155802"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/155802\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155802"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}