{"id":218211,"date":"2021-05-22T08:57:51","date_gmt":"2021-05-22T00:57:51","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=218211"},"modified":"2021-05-05T15:58:42","modified_gmt":"2021-05-05T07:58:42","slug":"primary-key-backfill","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/primary-key-backfill.html","title":{"rendered":"MyBatis\u4e2d\u4e3b\u952e\u56de\u586b\u7684\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e3b\u952e\u56de\u586b\u5176\u5b9e\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u6dfb\u52a0\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u6dfb\u52a0\u5b8c\u6570\u636e\u4e4b\u540e\uff0c\u9700\u8981\u83b7\u53d6\u521a\u521a\u6dfb\u52a0\u7684\u6570\u636e id\uff0c\u65e0\u8bba\u662f Jdbc \u8fd8\u662f\u5404\u79cd\u5404\u6837\u7684\u6570\u636e\u5e93\u6846\u67b6\u90fd\u5bf9\u6b64\u63d0\u4f9b\u4e86\u76f8\u5173\u7684\u652f\u6301\uff0c\u672c\u6587\u6211\u5c31\u6765\u548c\u548c\u5927\u5bb6\u5206\u4eab\u4e0b\u6570\u636e\u5e93\u4e3b\u952e\u56de\u586b\u5728 MyBatis \u4e2d\u7684\u4e24\u79cd\u5b9e\u73b0\u601d\u8def\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u539f\u751f\u5199\u6cd5<\/strong><\/div>\n

\u6846\u67b6\u6765\u6e90\u4e8e\u6211\u4eec\u5b66\u8fc7\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4e3b\u952e\u56de\u586b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5728 JDBC \u4e2d\u5c31\u88ab\u652f\u6301\u7684\u5199\u6cd5\uff0c\u6709\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u4e0d\u77e5\u9053\u8fd9\u4e00\u70b9\uff0c\u56e0\u6b64\u8fd9\u91cc\u6211\u5148\u6765\u8bf4\u8bf4\u5728 JDBC \u4e2d\u5982\u4f55\u5b9e\u73b0\u4e3b\u952e\u56de\u586b\u3002<\/p>\n

JDBC \u4e2d\u5b9e\u73b0\u4e3b\u952e\u56de\u586b\u5176\u5b9e\u975e\u5e38\u5bb9\u6613\uff0c\u4e3b\u8981\u662f\u5728\u6784\u9020 PreparedStatement \u65f6\u6307\u5b9a\u9700\u8981\u4e3b\u952e\u56de\u586b\uff0c\u7136\u540e\u5728\u63d2\u5165\u6210\u529f\u540e\uff0c\u67e5\u8be2\u521a\u521a\u63d2\u5165\u6570\u636e\u7684 id \uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\npublic int insert(Person person) {\r\n    Connection con = null;\r\n    PreparedStatement ps = null;\r\n    ResultSet rs = null;\r\n    con = DBUtils.getConnection();\r\n    ps = con.prepareStatement(\"INSERT INTO person(username,password,money) VALUES(?,?,?)\", PreparedStatement.RETURN_GENERATED_KEYS);\r\n    ps.setObject(1, person.getUsername());\r\n    ps.setObject(2, person.getPassword());\r\n    ps.setObject(3, person.getMoney());\r\n    int i = ps.executeUpdate();\r\n    rs = ps.getGeneratedKeys();\r\n    int id = -1;\r\n    if (rs.next()) {\r\n        id = rs.getInt(1);\r\n    }\r\n    return id;\r\n}\r\n<\/pre>\n

\u548c\u666e\u901a\u7684\u63d2\u5165 SQL \u4e0d\u540c\u4e4b\u5904\u4e3b\u8981\u4f53\u73b0\u5728\u4e24\u4e2a\u5730\u65b9\uff1a<\/p>\n

\u7b2c\u4e00\u4e2a\u662f\u6784\u9020 PreparedStatement \u65f6\uff0c\u591a\u4e86\u4e00\u4e2a\u53c2\u6570\uff0c\u6307\u5b9a\u4e86\u9700\u8981\u4e3b\u952e\u56de\u586b\u3002
\n\u5728\u66f4\u65b0\u64cd\u4f5c\u6267\u884c\u5b8c\u6210\u4e4b\u540e\uff0c\u8c03\u7528 getGeneratedKeys \uff0c\u7136\u540e\u53c8\u4f1a\u83b7\u53d6\u5230\u4e00\u4e2a ResultSet \u5bf9\u8c61\uff0c\u4ece\u8fd9\u4e2a\u6e38\u6807\u96c6\u4e2d\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230\u521a\u521a\u63d2\u5165\u6570\u636e\u7684id\u3002
\n\u8fd9\u4e2a\u662f\u539f\u751f\u7684\u5199\u6cd5\uff0c\u5728 MyBatis \u4e2d\uff0c\u5bf9\u6b64\u9700\u6c42\u63d0\u4f9b\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u6848\uff0c\u4e0b\u9762\u5206\u522b\u6765\u770b\u3002<\/p>\n

\u6846\u67b6\u5199\u6cd5<\/strong><\/div>\n

\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4e3b\u952e\u6709\u4e24\u79cd\u751f\u6210\u65b9\u5f0f\uff1a<\/p>\n

    \n
  • \u4e3b\u952e\u81ea\u589e\u957f<\/li>\n
  • \u81ea\u5b9a\u4e49\u4e3b\u952e\uff08\u4e00\u822c\u53ef\u4ee5\u4f7f\u7528UUID\uff0c\u6216\u8005\u7c7bUUID\uff09<\/li>\n<\/ul>\n

    \u5982\u679c\u662f\u7b2c\u4e8c\u79cd\uff0c\u4e3b\u952e\u4e00\u822c\u662f\u5728Java\u4ee3\u7801\u4e2d\u751f\u6210\uff0c\u7136\u540e\u4f20\u5165\u6570\u636e\u5e93\u6267\u884c\u63d2\u5165\u64cd\u4f5c\uff0c\u5982\u679c\u662f\u7b2c\u4e00\u4e2a\u4e3b\u952e\u81ea\u589e\u957f\uff0c\u6b64\u65f6\uff0cJava \u53ef\u80fd\u9700\u8981\u77e5\u9053\u6570\u636e\u6dfb\u52a0\u6210\u529f\u540e\u7684\u4e3b\u952e\u3002<\/p>\n

    MyBatis \u7684\u57fa\u672c\u7528\u6cd5\u5c31\u65e0\u9700\u591a\u8bf4\u4e86\uff0c\u8fd9\u4e5f\u4e0d\u662f\u672c\u6587\u7684\u91cd\u70b9\uff0c\u6211\u4eec\u8fd8\u662f\u6765\u770b\u770b MyBatis \u4e2d\u4e3b\u952e\u56de\u586b\u7684\u4e24\u79cd\u4e0d\u540c\u5b9e\u73b0\u65b9\u5f0f\u5427\uff01<\/p>\n

    \u65b9\u5f0f\u4e00<\/strong><\/span><\/div>\n

    \u7b2c\u4e00\u79cd\u65b9\u5f0f\u6bd4\u8f83\u7b80\u5355\uff0c\u4e5f\u662f\u677e\u54e5\u63a8\u8350\u7684\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n

    \r\n<insert id="insertBook" useGeneratedKeys="true" keyProperty="id">\r\n    insert into t_book (b_name,author) values (#{name},#{author});\r\n<\/insert>\r\n<\/pre>\n

    \u8fd9\u79cd\u65b9\u5f0f\u6bd4\u8f83\u7b80\u5355\uff0c\u5c31\u662f\u5728\u63d2\u5165\u8282\u70b9\u4e0a\u6dfb\u52a0 useGeneratedKeys \u5c5e\u6027\uff0c\u540c\u65f6\u8bbe\u7f6e\u63a5\u6536\u56de\u4f20\u4e3b\u952e\u7684\u5c5e\u6027\u3002\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u6267\u884c\u4e00\u4e2a\u63d2\u5165\u64cd\u4f5c\uff0c\u63d2\u5165\u65f6\u4f20\u5165\u4e00\u4e2a\u5bf9\u8c61\uff0c\u63d2\u5165\u5b8c\u6210\u540e\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u7684 id \u5c31\u4f1a\u88ab\u81ea\u52a8\u8d4b\u503c\uff0c\u503c\u5c31\u662f\u521a\u521a\u63d2\u5165\u6210\u529f\u7684id\u3002<\/p>\n

    \u677e\u54e5\u63a8\u8350\u5927\u5bb6\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u539f\u56e0\u5f88\u7b80\u5355\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7b80\u4fbf\u7701\u4e8b\u3002<\/p>\n

    \u65b9\u5f0f\u4e8c<\/strong><\/span><\/div>\n

    \u7b2c\u4e8c\u79cd\u65b9\u5f0f\u5219\u662f\u5229\u7528MySQL\u81ea\u5e26\u7684 last_insert_id() \u51fd\u6570\u67e5\u8be2\u521a\u521a\u63d2\u5165\u7684id\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

    \r\n<insert id="insertBook">\r\n    <selectkey keyProperty="id" resultType="java.lang.Integer">\r\n        SELECT LAST_INSERT_ID()\r\n    <\/selectkey>\r\n    insert into t_book (b_name,author) values (#{name},#{author});\r\n<\/insert>\r\n<\/pre>\n

    \u8fd9\u79cd\u65b9\u5f0f\u662f\u5728 insert \u8282\u70b9\u4e2d\u6dfb\u52a0 selectKey \u6765\u5b9e\u73b0\u4e3b\u952e\u56de\u586b\uff0c\u5b9e\u9645\u4e0a\u8fd9\u79cd\u65b9\u5f0f\u7684\u529f\u80fd\u66f4\u52a0\u4e30\u5bcc\uff0c\u56e0\u4e3a selectKey \u8282\u70b9\u4e2d\u7684 SQL \u6211\u4eec\u65e2\u53ef\u4ee5\u5728\u63d2\u5165\u4e4b\u524d\u6267\u884c\uff0c\u4e5f\u53ef\u4ee5\u5728\u63d2\u5165\u4e4b\u540e\u6267\u884c\uff08\u901a\u8fc7\u8bbe\u7f6e\u8282\u70b9\u7684 Order \u5c5e\u6027\u4e3a AFTER \u6216\u8005 BEFORE \u53ef\u4ee5\u5b9e\u73b0\uff09\uff0c\u5177\u4f53\u4ec0\u4e48\u65f6\u5019\u6267\u884c\uff0c\u8fd8\u662f\u8981\u770b\u5177\u4f53\u7684\u9700\u6c42\uff0c\u5982\u679c\u662f\u505a\u4e3b\u952e\u56de\u586b\uff0c\u6211\u4eec\u5f53\u7136\u9700\u8981\u5728\u63d2\u5165 SQL \u6267\u884c\u4e4b\u540e\u6267\u884c selectKey \u8282\u70b9\u4e2d\u7684 SQL\u3002<\/p>\n

    \u6ce8\u610f\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u4e00\u6837\u4e5f\u8981\u901a\u8fc7\u8bbe\u7f6e keyProperty \u6765\u6307\u5b9a\u5c06\u67e5\u8be2\u5230\u7684\u6570\u636e\u7ed1\u5b9a\u5230\u54ea\u4e2a\u5c5e\u6027\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u6846\u67b6\u6765\u6e90\u4e8e\u6211\u4eec\u5b66\u8fc7\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4e3b\u952e\u56de\u586b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5728 JDBC \u4e2d\u5c31\u88ab\u652f\u6301\u7684\u5199\u6cd5\uff0c\u6709\u7684\u5c0f\u4f19\u4f34\u53ef\u80fd\u4e0d\u77e5\u9053\u8fd9\u4e00\u70b9\uff0c […]<\/p>\n","protected":false},"author":1903,"featured_media":218248,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-218211","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=218211"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218211\/revisions"}],"predecessor-version":[{"id":218251,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218211\/revisions\/218251"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/218248"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=218211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=218211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=218211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}