Browse Source

持续优化中

miemie 7 years ago
parent
commit
8116fe50f3

+ 9 - 16
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -180,15 +180,6 @@ public abstract class AbstractMethod {
         return set.toString();
     }
 
-    /**
-     * <p>
-     * 拼接字符串 todo 看以后不需要就删了
-     * </p>
-     */
-    protected String joinStr(String... strings) {
-        return StringUtils.appends(null, strings);
-    }
-
     /**
      * <p>
      * SQL 查询所有表字段
@@ -208,8 +199,9 @@ public abstract class AbstractMethod {
         if (!queryWrapper) {
             return selectColumns;
         }
-        return "<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>" +
-            selectColumns + "</otherwise></choose>";
+        return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null",
+            Constants.WRAPPER, Constants.Q_WRAPPER_SQL_SELECT),
+            String.format("${%s}", Constants.Q_WRAPPER_SQL_SELECT), selectColumns);
     }
 
     /**
@@ -220,8 +212,9 @@ public abstract class AbstractMethod {
      * @param table 表信息
      */
     protected String sqlSelectObjsColumns(TableInfo table) {
-        return "<choose><when test=\"ew != null and ew.sqlSelect != null\">${ew.sqlSelect}</when><otherwise>" +
-            table.getAllSqlSelect() + "</otherwise></choose>";
+        return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null",
+            Constants.WRAPPER, Constants.Q_WRAPPER_SQL_SELECT),
+            String.format("${%s}", Constants.Q_WRAPPER_SQL_SELECT), table.getAllSqlSelect());
     }
 
     /**
@@ -318,9 +311,9 @@ public abstract class AbstractMethod {
         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 += (StringPool.NEWLINE + SqlScriptUtils.convertIf(String.format(" AND ${%s}",
-            Constants.WRAPPER_SQLSEGMENT),
-            MessageFormat.format("{0} != null and {0} != ''", Constants.WRAPPER_SQLSEGMENT)));
+        sqlScript += StringPool.NEWLINE;
+        sqlScript += SqlScriptUtils.convertIf(String.format(" AND ${%s}", Constants.WRAPPER_SQLSEGMENT),
+            MessageFormat.format("{0} != null and {0} != ''", Constants.WRAPPER_SQLSEGMENT));
         sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND|OR", null);
         sqlScript = SqlScriptUtils.convertIf(sqlScript, "ew!=null and !ew.emptyOfWhere");
         return sqlScript;

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

@@ -89,8 +89,8 @@ public final class SqlScriptUtils {
      */
     public static String convertChoose(String whenTest, String whenSqlScript, String otherwise) {
         return "<choose>" + StringPool.NEWLINE +
-            "<when test=\"" + whenTest + StringPool.QUOTE + StringPool.RIGHT_CHEV +
-            whenSqlScript + "</when>" + StringPool.NEWLINE +
+            "<when test=\"" + whenTest + StringPool.QUOTE + StringPool.RIGHT_CHEV + StringPool.NEWLINE +
+            whenSqlScript + StringPool.NEWLINE + "</when>" + StringPool.NEWLINE +
             "<otherwise>" + otherwise + "</otherwise>" + StringPool.NEWLINE +
             "</choose>";
     }

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

@@ -104,9 +104,9 @@ public abstract class LogicAbstractMethod extends AbstractMethod {
             sqlScript = StringPool.NEWLINE + sqlScript + StringPool.NEWLINE;
             sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", Constants.WRAPPER_ENTITY));
             sqlScript += (StringPool.NEWLINE + table.getLogicDeleteSql(true, true));
-            sqlScript += (StringPool.NEWLINE + SqlScriptUtils.convertIf(String.format(" AND ${%s}",
-                Constants.WRAPPER_SQLSEGMENT),
-                MessageFormat.format("{0} != null and {0} != ''", Constants.WRAPPER_SQLSEGMENT)));
+            sqlScript += StringPool.NEWLINE;
+            sqlScript += SqlScriptUtils.convertIf(String.format(" AND ${%s}", Constants.WRAPPER_SQLSEGMENT),
+                MessageFormat.format("{0} != null and {0} != ''", Constants.WRAPPER_SQLSEGMENT));
             sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND|OR", null);
             sqlScript = SqlScriptUtils.convertChoose("ew!=null and !ew.emptyOfWhere", sqlScript,
                 "WHERE " + table.getLogicDeleteSql(false, false));