{"id":14167,"date":"2023-12-03T23:29:52","date_gmt":"2023-12-03T15:29:52","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=14167"},"modified":"2023-12-04T00:18:12","modified_gmt":"2023-12-03T16:18:12","slug":"mybatis-zhong-ru-he-zhi-xing-pi-liang-cao-zuo","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/03\/mybatis-zhong-ru-he-zhi-xing-pi-liang-cao-zuo\/","title":{"rendered":"MyBatis \u4e2d\u5982\u4f55\u6267\u884c\u6279\u91cf\u64cd\u4f5c\uff1f"},"content":{"rendered":"<p>\u5728 MyBatis \u4e2d\u6267\u884c\u6279\u91cf\u64cd\u4f5c\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<h3>1. \u4f7f\u7528 <code>foreach<\/code> \u6807\u7b7e<\/h3>\n<p>\u53ef\u4ee5\u5728 SQL \u8bed\u53e5\u4e2d\u4f7f\u7528 <code>&lt;foreach&gt;<\/code> \u6807\u7b7e\u6267\u884c\u6279\u91cf\u64cd\u4f5c\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre data-language=XML><code class=\"language-markup  line-numbers\">&lt;insert id=\"batchInsert\" parameterType=\"java.util.List\"&gt;\n  INSERT INTO my_table (id, name) VALUES\n  &lt;foreach collection=\"list\" item=\"item\" separator=\",\"&gt;\n    (#{item.id}, #{item.name})\n  &lt;\/foreach&gt;\n&lt;\/insert&gt;\n<\/code><\/pre>\n<p>\u8fd9\u91cc <code>list<\/code> \u662f\u4f20\u5165\u7684 <code>List<\/code>\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u5305\u542b\u9700\u8981\u63d2\u5165\u7684\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6267\u884c\u6279\u91cf\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002<\/p>\n<h3>2. \u4f7f\u7528 BatchExecutor<\/h3>\n<p>\u901a\u8fc7 MyBatis \u7684 <code>BatchExecutor<\/code> \u63a5\u53e3\u8fdb\u884c\u6279\u5904\u7406\u64cd\u4f5c\uff0c\u624b\u52a8\u5c06\u591a\u4e2a SQL \u8bed\u53e5\u6dfb\u52a0\u5230\u6279\u5904\u7406\u4e2d\uff0c\u7136\u540e\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-java  line-numbers\">SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);\ntry {\n    Mapper mapper = session.getMapper(Mapper.class);\n    for (YourObject obj : yourObjectList) {\n        mapper.insert(obj);\n    }\n    session.commit();\n} finally {\n    session.close();\n}\n<\/code><\/pre>\n<h3>3. \u4f7f\u7528 BatchExecutorType<\/h3>\n<p>\u5728 MyBatis 3.5.2 \u7248\u672c\u5f15\u5165\u4e86 <code>BatchExecutorType<\/code> \u679a\u4e3e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>ExecutorType<\/code> \u4e3a <code>BATCH<\/code> \u6765\u6267\u884c\u6279\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-java  line-numbers\">Configuration configuration = new Configuration();\nconfiguration.setDefaultExecutorType(ExecutorType.BATCH);\nSqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);\n\ntry (SqlSession session = sqlSessionFactory.openSession()) {\n    Mapper mapper = session.getMapper(Mapper.class);\n    for (YourObject obj : yourObjectList) {\n        mapper.insert(obj);\n    }\n    session.commit();\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u6279\u91cf\u64cd\u4f5c\uff0c\u6839\u636e\u5b9e\u9645\u573a\u666f\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u6279\u91cf\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MyBatis \u4e2d\u6267\u884c\u6279\u91cf\u64cd\u4f5c\u6709\u51e0\u79cd\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 foreach \u6807\u7b7e \u53ef\u4ee5\u5728 SQL \u8bed\u53e5\u4e2d\u4f7f\u7528  [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[113],"tags":[],"class_list":["post-14167","post","type-post","status-publish","format-standard","hentry","category-mybatis"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=14167"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14167\/revisions"}],"predecessor-version":[{"id":14168,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/14167\/revisions\/14168"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=14167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=14167"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=14167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}