{"id":1274,"date":"2015-07-21T21:59:05","date_gmt":"2015-07-21T13:59:05","guid":{"rendered":"http:\/\/coderbee.net\/?p=1274"},"modified":"2019-01-06T21:21:10","modified_gmt":"2019-01-06T13:21:10","slug":"mybatis-%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5-%e6%8f%92%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/open-source\/20150721\/1274","title":{"rendered":"mybatis \u6279\u91cf\u63d2\u5165 \u63d2\u4ef6"},"content":{"rendered":"<pre>\n2017-01-07 \u66f4\u65b0\uff1a\u8fd9\u4e2a\u63d2\u4ef6\u505a\u4e86\u91cd\u547d\u540d\u3001\u68b3\u7406\uff0c\u65b0\u7684 github \u5730\u5740\u4e3a <a target=\"blank\" href=\"https:\/\/github.com\/wen866595\/MyBatis-batch\">MyBatis-Batch<\/a>\n\n\u53ef\u76f4\u63a5\u4ece Maven \u4e2d\u592e\u4ed3\u5e93\u5f15\u7528\uff1a<br\/>\n\n&lt;dependency&gt;<Br\/>\n    &lt;groupId&gt;net.coderbee&lt;\/groupId&gt;<Br\/>\n    &lt;artifactId&gt;mybatis-batch&lt;\/artifactId&gt;<Br\/>\n    &lt;version&gt;1.1.0&lt;\/version&gt;<Br\/>\n&lt;\/dependency&gt;<Br\/>\n\n<\/pre>\n<h3>\u80cc\u666f<\/h3>\n<p>\u9879\u76ee\u4e2d\u6709\u4e2a\u8bbe\u8ba1\u4e0d\u5408\u7406\u7684\u8868\uff0c\u603b\u5171 8 \u4e2a\u5b57\u6bb5\uff0c\u6709 5 \u4e2a\u7d22\u5f15\uff0c\u6709\u51e0\u4e2a\u7d22\u5f15\u5b57\u6bb5\u8fd8\u662f 32 \u4f4d\u7684\u5b57\u7b26\u4e32\u3002\u8be5\u8868\u6570\u636e\u91cf\u5df2\u8fbe 1 \u4ebf\uff0c\u6700\u8fd1\u6bcf\u5929\u65b0\u589e 100 \u4e07\u3002\u6839\u636e\u65e5\u5fd7\u770b\uff0c\u6709\u6b21\u7528\u6237\u4e0a\u4f20\u4e00\u4e2a\u6709 200 \u884c\u8bb0\u5f55\u7684 excel\uff0c\u9700\u8981\u5f80\u8fd9\u4e2a\u8868\u63d2\u5165 3940 \u6761\u8bb0\u5f55\uff0c\u8017\u65f6 72 \u79d2\u3002\u8fd9\u4e48\u5927\u5ef6\u8fdf\u662f\u6ca1\u6cd5\u63a5\u53d7\u7684\u3002<\/p>\n<p>\u8981\u5206\u6790\u6570\u636e\u5e93\u65b9\u9762\u7684\u95ee\u9898\uff0c\u9996\u5148\u662f\u627e DBA \u5206\u6790\u4e0b\u8868\u7684\u60c5\u51b5\uff0c\u8bf4\u8ddf\u4ee5\u5f80\u6ca1\u591a\u5927\u533a\u522b\uff0c\u53ea\u662f\u8ddf\u8fd9\u4e2a\u8868\u6709\u5173\u7684\u63d2\u5165\u7684\u6267\u884c\u8ba1\u5212\u5f88\u591a\u3002\u56e0\u4e3a\u8fd9\u4e2a\u8868\u7684\u6279\u91cf\u63d2\u5165\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code class=\"xml\">&lt;select id=\"batchSave\" parameterType=\"java.util.List\"&gt;\n    INSERT INTO TABLE_NAME(ID,NAME) \n&lt;foreach collection=\"list\"item=\"itm\" separator=\"union all\"&gt;\n    (SELECT #{itm.id}, #{itm.name} FROM DUAL)\n&lt;\/foreach&gt;&lt;\/select&gt;\n<\/code><\/pre>\n<p>\u8fd9\u662f\u7528 MyBatis \u5bf9 Oracle \u505a\u6279\u91cf\u63d2\u5165\u7684\u552f\u4e00\u65b9\u6cd5\u3002\u526f\u4f5c\u7528\u662f\uff1a\u5047\u5b9a <code>List<\/code> \u7684\u6700\u5927\u957f\u5ea6\u662f <code>N<\/code>\uff0c\u90a3\u4e48 Oracle \u670d\u52a1\u5668\u7aef\u5c31\u53ef\u80fd\u6709 <code>N<\/code> \u4e2a\u63d2\u5165\u7684\u6267\u884c\u8ba1\u5212\u3002\u8fd9\u4e48\u591a\u6267\u884c\u8ba1\u5212\uff0cDBA \u4e5f\u4e0d\u4e50\u610f\u53bb\u5206\u6790\u5440\uff0c\u800c\u4e14\u786e\u5b9e\u751f\u6210\u7684\u6bcf\u4e2a\u6267\u884c\u8ba1\u5212\u90fd\u662f\u5f88\u7b80\u5355\u7684\u3002<\/p>\n<h3>\u4f18\u5316<\/h3>\n<p>\u5982\u679c\u653e\u5f03 <code>union all<\/code> \u7684\u65b9\u5f0f\uff0c\u5219\u6bcf\u6761\u8bb0\u5f55\u90fd\u9700\u8981\u5404\u63d0\u4ea4\u4e00\u6b21\u5230\u6570\u636e\u5e93\uff0c\u663e\u7136\u4e5f\u4e0d\u597d\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3 <code>N<\/code> \u4e2a\u6267\u884c\u8ba1\u5212\u7684\u95ee\u9898\uff0c\u505a\u5230\u771f\u6b63\u7684\u6279\u91cf\u63d2\u5165\uff0c\u53ea\u80fd\u4fee\u6539 MyBatis \u7684\u6267\u884c\u903b\u8f91\uff0c\u56e0\u6b64\u5c31\u6709\u4e86\u8fd9\u4e2a\u9879\u76ee\uff1a<a href=\"https:\/\/github.com\/wen866595\/mybatis-batchinsert-plugin\">mybatis-batchinsert-plugin<\/a><\/p>\n<p>\u76ee\u524d\u53ef\u4ee5\u6279\u91cf\u63d2\u5165\uff0c\u4f46\u4e0d\u652f\u6301\u8fd4\u56de\u4e3b\u952e\u7b49\u5176\u4ed6\u7684\u529f\u80fd\uff0c\u6709\u7a7a\u518d\u5b8c\u5584\u3002<\/p>\n<p><h4>\u6211\u505a\u8fd9\u4e2a\u63d2\u4ef6\u4e3b\u8981\u662f\u5e0c\u671b\u8fbe\u5230\uff1a<\/h4>\n<li>1\u3001\u89e3\u51b3 Oracle \u670d\u52a1\u5668\u7aef\u5bf9\u4e00\u4e2a\u8868\u505a\u63d2\u5165\u7684\u6267\u884c\u8ba1\u5212\u8fc7\u591a\u7684\u95ee\u9898\uff1b\u7528 union all \u7684\u65b9\u5f0f\u8fd8\u53ef\u80fd\u5bfc\u81f4\u786c\u89e3\u6790\u589e\u591a\uff0c\u6bd4\u5982\u9700\u8981\u63d2\u5165\u7684\u603b\u8bb0\u5f55\u6709 123 \u6761\uff0c\u4ee5 100 \u6761\u4e3a\u4e00\u6279\u62fc\u63a5\u6210\u4e00\u6761 sql A\uff0c\u90a3\u4e48\u5269\u4e0b\u7684 23 \u6761\u4e5f\u4f1a\u62fc\u63a5\u6210\u4e00\u6761 B\uff0c\u800c B \u7684\u9891\u7387\u80af\u5b9a\u6bd4 A \u7684\u9891\u7387\u4f4e\u5f88\u591a\uff0c\u56e0\u4e3a\u8981\u505a\u6279\u91cf\u63d2\u5165\u7684\u8bb0\u5f55\u603b\u6570\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u6240\u4ee5\u4f59\u6570\u4e5f\u4e0d\u662f\u56fa\u5b9a\u7684\uff0c\u5bb9\u6613\u5bfc\u81f4 B \u7684\u89e3\u6790\u8fc7\u671f\uff0c\u88ab\u6e05\u9664\u51fa\u53bb\uff0c\u7136\u540e\u53c8\u6765\u4e86\u6761\u4f59\u6570 23 \u7684\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u786c\u89e3\u6790\u4e86\u3002<\/li>\n<li>2\u3001\u907f\u514d\u7528 union all \u62fc\u63a5\u6210\u4e00\u6761 sql\uff0c\u5bfc\u81f4\u8fd9\u6761 sql \u91cc\u7684\u7ed1\u5b9a\u53d8\u91cf\u8fc7\u591a\u3002<\/li>\n<li>3\u3001\u517c\u5bb9 MyBatis \u7684\u7528\u6cd5\uff0c\u5728\u4f7f\u7528\u4e0a\u66f4\u7b80\u5355\u4e86\uff0c\u56e0\u4e3a\u4e0d\u9700\u8981\u7528 MyBatis \u7684 for \u5faa\u73af\uff0c\u7531\u63d2\u4ef6\u6765\u505a\u5faa\u73af\uff1b\u7528 union all \u65b9\u5f0f\u7684\u8bdd\uff0cJava \u4ee3\u7801\u8981\u63a7\u5236\u6279\u7684\u5927\u5c0f\uff0c\u9700\u8981\u4e00\u4e2a\u5faa\u73af\uff0cMyBatis \u91cc\u9700\u8981\u4e00\u4e2a\uff0c\u7528\u4e8e\u62fc\u63a5 union all\u3002<\/li>\n<li>4\u3001\u4e00\u4e2a\u5bf9\u4e8e\u6027\u80fd\u6709\u76ca\u7684\u662f\uff1a\u6bcf\u6b21\u8c03\u7528 MyBatis \u7684 insert \u8bed\u53e5\uff0c\u90fd\u9700\u8981\u8c03\u7528\u4e00\u6b21 Connection.prepareStatement \u65b9\u6cd5\uff0c\u4e5f\u5c31\u9700\u8981\u8bbf\u95ee\u6570\u636e\u5e93\u4e00\u6b21\uff0c\u5982\u679c\u5bf9\u8bed\u53e5\u6ca1\u6709\u7f13\u5b58\u7684\u8bdd\uff1b\u7528\u63d2\u4ef6\u7684\u8bdd\uff0c\u4e0d\u7ba1\u591a\u5c11\u8bb0\u5f55\uff0c\u53ea\u9700\u8981\u4e00\u6b21\u3002<\/li>\n<p><\/p>\n<p>\n\u8fd9\u4e2a\u63d2\u4ef6\u5bf9\u6027\u80fd\u63d0\u5347\u4e0d\u4f1a\u5f88\u663e\u8457\uff0c\u5728\u6211\u4eec\u7684\u5f00\u53d1\u6570\u636e\u5e93\u4e0a\u6d4b\u8bd5\u65f6\uff0c\u4ee5 1000 \u6761\u4e3a 1 \u6279\uff0c\u5df2\u6709\u6570\u636e\u91cf 160w\uff0c\u63d2\u5165 40w \u6570\u636e\uff0c\u7528 union all \u7684\u65b9\u5f0f\u5e73\u5747\u4e00\u6761\u9700\u8981 32ms\uff0c\u7528\u63d2\u4ef6\u5927\u6982\u662f 27ms\u3002\n<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2017-01-07 \u66f4\u65b0\uff1a\u8fd9\u4e2a\u63d2\u4ef6\u505a\u4e86\u91cd\u547d\u540d\u3001\u68b3\u7406\uff0c\u65b0\u7684 github \u5730\u5740\u4e3a &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/open-source\/20150721\/1274\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[228,229],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1274"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1274"}],"version-history":[{"count":9,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1274\/revisions"}],"predecessor-version":[{"id":1279,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1274\/revisions\/1279"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}