{"id":201055,"date":"2025-05-06T14:57:40","date_gmt":"2025-05-06T06:57:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201055\/"},"modified":"2025-05-06T14:57:40","modified_gmt":"2025-05-06T06:57:40","slug":"springbootmybatismysql%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e5%af%bc%e8%87%b4%e7%9a%84oom%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201055\/","title":{"rendered":"Springboot+Mybatis+Mysql\u4e2d\u5982\u4f55\u907f\u514d\u6279\u91cf\u63d2\u5165\u6570\u636e\u5bfc\u81f4\u7684OOM\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Springboot+Mybatis+Mysql\u4e2d\u5982\u4f55\u907f\u514d\u6279\u91cf\u63d2\u5165\u6570\u636e\u5bfc\u81f4\u7684OOM\u5f02\u5e38\uff1f<\/h1>\n<p>\u201c\u7eb5\u6709\u75be\u98ce\u6765\uff0c\u4eba\u751f\u4e0d\u8a00\u5f03\u201d\uff0c\u8fd9\u53e5\u8bdd\u9001\u7ed9\u6b63\u5728\u5b66\u4e60\u7684\u670b\u53cb\u4eec\uff0c\u4e5f\u5e0c\u671b\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u591f\u771f\u7684\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\u6211\u4e5f\u4f1a\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\uff0c\u9646\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u6280\u672f\u6587\u7ae0\uff0c\u6709\u597d\u7684\u5efa\u8bae\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/173279164467484d5c71804.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u6279\u91cf\u63d2\u5165\u6570\u636e\u907f\u514doom<\/strong><\/p>\n<p>\u5728springboot+mybatis+mysql\u7684\u9879\u76ee\u4e2d\uff0c\u6279\u91cf\u63d2\u5165\u6570\u636e\u65f6\uff0c\u6570\u636e\u91cf\u8fc7\u5927\u53ef\u80fd\u5bfc\u81f4oom\u5f02\u5e38\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n<p><strong>\u95ee\u9898\u63d0\u51fa\uff1a<\/strong><\/p>\n<p>\u5047\u5982\u6709\u4e00\u4e2a\u6279\u91cf\u63d2\u5165\u6570\u636e\u7684\u63a5\u53e3\uff0c\u6570\u636e\u4ee5list\u5f62\u5f0f\u4f20\u5165\uff0c\u4e3a\u4e86\u907f\u514d\u63d2\u5165\u65f6\u51fa\u73b0oom\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/p>\n<p><strong>\u601d\u8def\uff1a<\/strong><\/p>\n<p>\u5bf9\u4f20\u5165\u6570\u636e\u5206\u6279\u63d2\u5165\uff0c\u6bcf\u6279\u63d2\u5165\u4e00\u5b9a\u6570\u91cf\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u4ee3\u7801\uff1a<\/strong><\/p>\n<p>\u524d\u9762\u7684\u5b9e\u73b0\u4ee3\u7801\u53ef\u4ee5\u4f18\u5316\u4e3a\uff1a<\/p>\n<pre>public void insert(List&lt;User&gt; list) {\n    int batchSize = 10000; \/\/ \u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u5206\u6279\u5927\u5c0f\n    int total = list.size();\n    for (int i = 0; i &lt; total; i += batchSize) {\n        int end = Math.min(total, i + batchSize);\n        List&lt;User&gt; subList = list.subList(i, end);\n        userMapper.insertBatch(subList); \/\/ \u4f7f\u7528Mybatis\u63d0\u4f9b\u7684batchInsert\u65b9\u6cd5\n    }\n}<\/pre>\n<p>\u4f7f\u7528mybatis\u7684batchinsert\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4e00\u6b21\u63d2\u5165\u591a\u6761\u6570\u636e\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><strong>\u66f4\u597d\u7684\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u9664\u4e86\u5206\u6279\u63d2\u5165\u5916\uff0c\u8fd8\u6709\u4ee5\u4e0b\u65b9\u6cd5\u53ef\u4ee5\u4f18\u5316\u6279\u91cf\u63d2\u5165\u6570\u636e\u8fc7\u7a0b\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u6d41\u5f0f\u5904\u7406\uff0c\u907f\u514d\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528load data infile\u6216jdbc\u6279\u5904\u7406\u7b49\u66f4\u5feb\u7684\u5927\u6570\u636e\u52a0\u8f7d\u65b9\u6cd5\u3002<\/li>\n<li>\u4f7f\u7528\u7f13\u51b2\u533a\u7ba1\u7406\u5185\u5b58\uff0c\u9632\u6b62\u4e00\u6b21\u6027\u5206\u914d\u8fc7\u591a\u5185\u5b58\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u4e3b\u673a\u5b9d\u8d1d\uff01\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Springboot+Mybat&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201055","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201055"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201055\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}