Browse Source

优化格式

miemie 7 years ago
parent
commit
f3fc5802ec

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

@@ -126,7 +126,7 @@ public abstract class AbstractMethod {
         if (ew) {
             sqlScript += StringPool.NEWLINE;
             sqlScript += SqlScriptUtils.convertIf(String.format("${%s}", Constants.U_WRAPPER_SQL_SET),
-                String.format("%s != null and %s != null", Constants.WRAPPER, Constants.U_WRAPPER_SQL_SET));
+                String.format("%s != null and %s != null", Constants.WRAPPER, Constants.U_WRAPPER_SQL_SET), false);
         }
         sqlScript = SqlScriptUtils.convertTrim(sqlScript, "SET", null, null, ",");
         return sqlScript;
@@ -179,9 +179,8 @@ public abstract class AbstractMethod {
             " ${k} = #{v} ");
         sqlScript = SqlScriptUtils.convertForeach(sqlScript, Constants.COLUMN_MAP, "k", "v", "AND");
         sqlScript = SqlScriptUtils.convertWhere(sqlScript);
-        sqlScript = StringPool.NEWLINE + sqlScript + StringPool.NEWLINE;
         sqlScript = SqlScriptUtils.convertIf(sqlScript,
-            String.format("%s != null and !%s", Constants.COLUMN_MAP, Constants.COLUMN_MAP_IS_EMPTY));
+            String.format("%s != null and !%s", Constants.COLUMN_MAP, Constants.COLUMN_MAP_IS_EMPTY), true);
         return sqlScript;
     }
 
@@ -195,13 +194,13 @@ public abstract class AbstractMethod {
      */
     protected String sqlWhereEntityWrapper(TableInfo table) {
         String sqlScript = table.getAllSqlWhere(false, true, Constants.WRAPPER_ENTITY_SPOT);
-        sqlScript = StringPool.NEWLINE + sqlScript + StringPool.NEWLINE;
-        sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", Constants.WRAPPER_ENTITY));
+        sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", Constants.WRAPPER_ENTITY), true);
         sqlScript += StringPool.NEWLINE;
         sqlScript += SqlScriptUtils.convertIf(String.format(" AND ${%s}", Constants.WRAPPER_SQLSEGMENT),
-            String.format("%s != null and %s != ''", Constants.WRAPPER_SQLSEGMENT, Constants.WRAPPER_SQLSEGMENT));
+            String.format("%s != null and %s != ''", Constants.WRAPPER_SQLSEGMENT, Constants.WRAPPER_SQLSEGMENT),
+            false);
         sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND|OR", null);
-        sqlScript = SqlScriptUtils.convertIf(sqlScript, "ew != null and !ew.emptyOfWhere");
+        sqlScript = SqlScriptUtils.convertIf(sqlScript, "ew != null and !ew.emptyOfWhere", false);
         return sqlScript;
     }
 

+ 4 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -308,7 +308,7 @@ public class TableFieldInfo {
         // 默认:  AND column=#{prefix + el}
         String sqlScript = " AND " + String.format(condition, column, prefix + el);
         // 查询的时候只判非空
-        return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", prefix + property));
+        return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", prefix + property), false);
     }
 
     /**
@@ -323,8 +323,9 @@ public class TableFieldInfo {
             return sqlScript;
         }
         if (fieldStrategy == FieldStrategy.NOT_EMPTY && isCharSequence) {
-            return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and %s != ''", property, property));
+            return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and %s != ''", property, property),
+                false);
         }
-        return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", property));
+        return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", property), false);
     }
 }

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -280,7 +280,7 @@ public class TableInfo {
         String newKeyProperty = newPrefix + keyProperty;
         String keySqlScript = keyColumn + StringPool.EQUALS +
             SqlScriptUtils.HASH_LEFT_BRACE + newKeyProperty + StringPool.RIGHT_BRACE;
-        return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty)) +
+        return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty), false) +
             StringPool.NEWLINE + filedSqlScript;
     }
 

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

@@ -45,8 +45,12 @@ public final class SqlScriptUtils {
      * @param sqlScript sql 脚本片段
      * @return if 脚本
      */
-    public static String convertIf(String sqlScript, String ifTest) {
-        return String.format("<if test=\"%s\">%s</if>", ifTest, sqlScript);
+    public static String convertIf(final String sqlScript, final String ifTest, boolean newLine) {
+        String newSqlScript = sqlScript;
+        if (newLine) {
+            newSqlScript = StringPool.NEWLINE + newSqlScript + StringPool.NEWLINE;
+        }
+        return String.format("<if test=\"%s\">%s</if>", ifTest, newSqlScript);
     }
 
     /**

+ 9 - 8
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/AbstractLogicMethod.java

@@ -53,13 +53,15 @@ public abstract class AbstractLogicMethod extends AbstractMethod {
     protected String sqlWhereEntityWrapper(TableInfo table) {
         if (table.isLogicDelete()) {
             String sqlScript = table.getAllSqlWhere(true, true, Constants.WRAPPER_ENTITY_SPOT);
-            sqlScript = StringPool.NEWLINE + sqlScript + StringPool.NEWLINE;
-            sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", Constants.WRAPPER_ENTITY));
-            sqlScript += (StringPool.NEWLINE + table.getLogicDeleteSql(true, false));
-            sqlScript += StringPool.NEWLINE;
+            sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", Constants.WRAPPER_ENTITY),
+                true);
+            sqlScript += (StringPool.NEWLINE + table.getLogicDeleteSql(true, false) +
+                StringPool.NEWLINE);
             sqlScript += SqlScriptUtils.convertIf(String.format(" AND ${%s}", Constants.WRAPPER_SQLSEGMENT),
-                String.format("%s!=null and %s!=''", Constants.WRAPPER_SQLSEGMENT, Constants.WRAPPER_SQLSEGMENT));
-            sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND|OR", null);
+                String.format("%s!=null and %s!=''", Constants.WRAPPER_SQLSEGMENT, Constants.WRAPPER_SQLSEGMENT),
+                false);
+            sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND|OR",
+                null);
             sqlScript = SqlScriptUtils.convertChoose("ew!=null and !ew.emptyOfWhere", sqlScript,
                 "WHERE " + table.getLogicDeleteSql(false, false));
             return sqlScript;
@@ -75,8 +77,7 @@ public abstract class AbstractLogicMethod extends AbstractMethod {
             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");
+            sqlScript = SqlScriptUtils.convertIf(sqlScript, "cm != null and !cm.isEmpty", true);
             sqlScript += (StringPool.NEWLINE + table.getLogicDeleteSql(true, false));
             sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND", null);
             return sqlScript;