{"id":926,"date":"2023-03-11T17:23:05","date_gmt":"2023-03-11T09:23:05","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=926"},"modified":"2023-04-29T15:46:49","modified_gmt":"2023-04-29T07:46:49","slug":"obtain-record-id-after-inserting-it-into-mybatis","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/obtain-record-id-after-inserting-it-into-mybatis\/","title":{"rendered":"MyBatis\u4e2d\u63d2\u5165\u8bb0\u5f55\u540e\u83b7\u53d6\u8be5\u6761\u8bb0\u5f55ID"},"content":{"rendered":"<p>\u4e3b\u952e\u751f\u6210\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\u4e3b\u952e\u81ea\u589e\u957f\uff0c\u9700\u8981\u5f97\u5230\u63d2\u5165\u6210\u529f\u540e\u6570\u636e\u5e93\u751f\u6210\u7684id<\/li>\n<li>\u81ea\u5b9a\u4e49\u4e3b\u952e\uff0c\u4e3b\u8981\u7528\u4e8e\u5206\u5e03\u5f0f\u5f00\u53d1\uff0c\u4e00\u822c\u662f\u73b0\u5728java\u4ee3\u7801\u4e2d\u751f\u6210id\uff0c\u7136\u540e\u5b58\u5165\u6570\u636e\u5e93<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<p>MyBatis\u4e2d\u4e3b\u952e\u56de\u586b\u7684\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<p>\uff081\uff09\u63a8\u8350\u65b9\u5f0f<\/p>\n<pre><code class=\"language-xml\">&lt;insert id=&quot;insert&quot; useGeneratedKeys=&quot;true&quot;  keyProperty=&quot;id&quot;&gt;\n    insert into t_user (name,password) values (#{name},#{password});\n&lt;\/insert&gt;<\/code><\/pre>\n<p>\u8bbe\u7f6e<code>useGeneratedKeys=&quot;true&quot;<\/code>\uff0c\u8bbe\u7f6e<code>keyProperty=&quot;id&quot;<\/code>\uff0c\u63a5\u6536\u56de\u4f20\u4e3b\u952e\u7684\u5c5e\u6027id\uff0c\u6210\u529f\u540eid\u7684\u503c\u5c31\u662f\u56de\u4f20\u4e3b\u952e\u7684\u503c<\/p>\n<p>\uff082\uff09\u4f7f\u7528 MySQL\u7684<code>LAST_INSERT_ID()<\/code><\/p>\n<pre><code class=\"language-xml\">&lt;insert id=&quot;insert&quot; parameterType=&quot;cn.appblog.pojo.User&quot;&gt;    \n    insert into t_user (name) values (#{user.name})\n    &lt;selectKey resultType=&quot;Integer&quot; order=&quot;AFTER&quot; keyProperty=&quot;user.userId&quot;&gt;\n        SELECT LAST_INSERT_ID() AS userId\n    &lt;\/selectKey&gt;\n&lt;\/insert&gt;<\/code><\/pre>\n<p>\u8fd9\u91cc\u662f\u7528User\u7c7b\u63a5\u6536\uff0c\u4e5f\u53ef\u81ea\u5b9a\u4e49\u5c5e\u6027\u63a5\u6536\u3002<\/p>\n<p>\u5728Mapper.xml\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u5b8c\u540e\uff0c\u5728\u63d2\u5165\u8bb0\u5f55\u540e\uff0c\u5c31\u4f1a\u8fd4\u56deid\u503c\u7ed9\u5bf9\u8c61\uff0c\u76f4\u63a5\u901a\u8fc7\u5bf9\u8c61get\u65b9\u6cd5\u5c31\u53ef\u4ee5\u83b7\u5f97id<\/p>\n<pre><code class=\"language-java\">User user = new User();\nuser.setName(&quot;Joe.Ye&quot;);\ntUserMapper.insert(user);\nuser.getId();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u952e\u751f\u6210\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a \u4e3b\u952e\u81ea\u589e\u957f\uff0c\u9700\u8981\u5f97\u5230\u63d2\u5165\u6210\u529f\u540e\u6570\u636e\u5e93\u751f\u6210\u7684id \u81ea\u5b9a\u4e49\u4e3b\u952e\uff0c\u4e3b\u8981\u7528\u4e8e\u5206\u5e03\u5f0f\u5f00\u53d1\uff0c\u4e00\u822c\u662f\u73b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[111],"class_list":["post-926","post","type-post","status-publish","format-standard","hentry","category-mybatis","tag-mybatis"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=926"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/926\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}