Ver código fonte

再度优化2处

miemie 7 anos atrás
pai
commit
295e0f78eb

+ 6 - 8
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -230,14 +230,12 @@ public abstract class AbstractMethod {
      * </p>
      */
     protected String sqlWhereByMap(TableInfo table) {
-        return "<if test=\"cm!=null and !cm.isEmpty\">" +
-            "<where>" +
-            "<foreach collection=\"cm\" index=\"k\" item=\"v\" separator=\"AND\">" +
-            "<choose><when test=\"v==null\"> ${k} IS NULL </when>" +
-            "<otherwise> ${k}=#{v} </otherwise></choose>" +
-            "</foreach>" +
-            "</where>" +
-            "</if>";
+        String sqlScript = SqlScriptUtils.convertChoose("v == null", " ${k} IS NULL ",
+            " ${k} = #{v} ");
+        sqlScript = SqlScriptUtils.convertForeach(sqlScript, "cm", "k", "v", "AND");
+        sqlScript = StringPool.NEWLINE + sqlScript + StringPool.NEWLINE;
+        sqlScript = SqlScriptUtils.convertIf(sqlScript, "cm != null and !cm.isEmpty");
+        return sqlScript;
     }
 
     /**

+ 23 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/sql/SqlScriptUtils.java

@@ -63,7 +63,7 @@ public final class SqlScriptUtils {
      */
     public static String convertTrim(String sqlScript, String prefix, String suffix, String prefixOverrides,
                                      String suffixOverrides) {
-        StringBuilder sb = new StringBuilder(StringPool.NEWLINE).append("<trim");
+        StringBuilder sb = new StringBuilder("<trim");
         if (StringUtils.isNotEmpty(prefix)) {
             sb.append(StringPool.SPACE).append("prefix=\"").append(prefix).append(StringPool.QUOTE);
         }
@@ -77,7 +77,7 @@ public final class SqlScriptUtils {
             sb.append(StringPool.SPACE).append("suffixOverrides=\"").append(suffixOverrides).append(StringPool.QUOTE);
         }
         return sb.append(StringPool.RIGHT_CHEV).append(StringPool.NEWLINE).append(sqlScript)
-            .append(StringPool.NEWLINE).append("</trim>").append(StringPool.NEWLINE).toString();
+            .append(StringPool.NEWLINE).append("</trim>").toString();
     }
 
     /**
@@ -88,10 +88,29 @@ public final class SqlScriptUtils {
      * @return choose 脚本
      */
     public static String convertChoose(String whenTest, String whenSqlScript, String otherwise) {
-        return StringPool.NEWLINE + "<choose>" + StringPool.NEWLINE +
+        return "<choose>" + StringPool.NEWLINE +
             "<when test=\"" + whenTest + StringPool.QUOTE + StringPool.RIGHT_CHEV +
             whenSqlScript + "</when>" + StringPool.NEWLINE +
             "<otherwise>" + otherwise + "</otherwise>" + StringPool.NEWLINE +
-            "</choose>" + StringPool.NEWLINE;
+            "</choose>";
+    }
+
+    public static String convertForeach(String sqlScript, String collection, String index, String item,
+                                        String separator) {
+        StringBuilder sb = new StringBuilder("<foreach");
+        if (StringUtils.isNotEmpty(collection)) {
+            sb.append(StringPool.SPACE).append("collection=\"").append(collection).append(StringPool.QUOTE);
+        }
+        if (StringUtils.isNotEmpty(index)) {
+            sb.append(StringPool.SPACE).append("index=\"").append(index).append(StringPool.QUOTE);
+        }
+        if (StringUtils.isNotEmpty(item)) {
+            sb.append(StringPool.SPACE).append("item=\"").append(item).append(StringPool.QUOTE);
+        }
+        if (StringUtils.isNotEmpty(separator)) {
+            sb.append(StringPool.SPACE).append("separator=\"").append(separator).append(StringPool.QUOTE);
+        }
+        return sb.append(StringPool.RIGHT_CHEV).append(StringPool.NEWLINE).append(sqlScript)
+            .append(StringPool.NEWLINE).append("</foreach>").toString();
     }
 }

+ 5 - 13
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/LogicAbstractMethod.java

@@ -118,20 +118,12 @@ public abstract class LogicAbstractMethod extends AbstractMethod {
 
     @Override
     protected String sqlWhereByMap(TableInfo table) {
+        String sqlScript = super.sqlWhereByMap(table);
         if (table.isLogicDelete()) {
-            return new StringBuilder()
-                .append("<trim prefix=\"WHERE\" prefixOverrides=\"AND\">")
-                .append("<if test=\"cm!=null and !cm.isEmpty\">")
-                .append("<foreach collection=\"cm\" index=\"k\" item=\"v\" separator=\"AND\">")
-                .append("<choose><when test=\"v==null\"> ${k} IS NULL </when>")
-                .append("<otherwise> ${k}=#{v} </otherwise></choose>")
-                .append("</foreach>")
-                .append("</if>")
-                .append(getLogicDeleteSql(true, table))
-                .append("</trim>")
-                .toString();
+            // 逻辑删除
+            sqlScript += (StringPool.NEWLINE + table.getLogicDeleteSql(true, false));
+            sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND", null);
         }
-        // 正常逻辑
-        return super.sqlWhereByMap(table);
+        return sqlScript;
     }
 }