From ab849f796bdc17236a95ea5fe5c166fb8f24a75c Mon Sep 17 00:00:00 2001
From: sws <15810472099@163.com>
Date: 星期六, 26 十一月 2022 16:12:02 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/extend/UpdateBatchMethod.java |   44 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/lf/server/extend/UpdateBatchMethod.java b/src/main/java/com/lf/server/extend/UpdateBatchMethod.java
new file mode 100644
index 0000000..e935fc0
--- /dev/null
+++ b/src/main/java/com/lf/server/extend/UpdateBatchMethod.java
@@ -0,0 +1,44 @@
+package com.lf.server.extend;
+
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.SqlSource;
+
+/**
+ * 鎵归噺鏇存柊鏂规硶
+ * @author WWW
+ */
+@SuppressWarnings("ALL")
+public class UpdateBatchMethod extends AbstractMethod {
+    /**
+     * update user set name = "a", age = 17 where id = 1;
+     * update user set name = "b", age = 18 where id = 2;
+     * <script>
+     * <foreach collection="list" item="item" separator=";">
+     * update user
+     * <set>
+     * <if test="item.name != null and item.name != ''">
+     * name = #{item.name,jdbcType=VARCHAR},
+     * </if>
+     * <if test="item.age != null">
+     * age = #{item.age,jdbcType=INTEGER},
+     * </if>
+     * </set>
+     * where id = #{item.id,jdbcType=INTEGER}
+     * </foreach>
+     * </script>
+     */
+    @Override
+    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
+        String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>";
+        String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true);
+        String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item.");
+        String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional);
+
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
+
+        // 绗笁涓弬鏁板繀椤诲拰RootMapper鐨勮嚜瀹氫箟鏂规硶鍚嶄竴鑷�
+        return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatch", sqlSource);
+    }
+}

--
Gitblit v1.9.3