Jelajahi Sumber

去除 wrapper 的一些变量,wrapper 内部 string 传递优化

miemie 6 tahun lalu
induk
melakukan
30474ab5ac

+ 33 - 75
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java

@@ -20,14 +20,16 @@ import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
 import com.baomidou.mybatisplus.core.conditions.interfaces.Join;
 import com.baomidou.mybatisplus.core.conditions.interfaces.Nested;
 import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
-import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList;
 import com.baomidou.mybatisplus.core.enums.SqlKeyword;
 import com.baomidou.mybatisplus.core.enums.SqlLike;
 import com.baomidou.mybatisplus.core.toolkit.*;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
 
-import java.util.*;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.BiPredicate;
 import java.util.function.Function;
@@ -46,24 +48,20 @@ import static java.util.stream.Collectors.joining;
 public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T, R, Children>> extends Wrapper<T>
     implements Compare<Children, R>, Nested<Children, Children>, Join<Children>, Func<Children, R> {
 
-    /**
-     * 前缀
-     */
-    private static final String MP_GENERAL_PARAMNAME = "MPGENVAL";
-    private static final String DEFAULT_PARAM_ALIAS = Constants.WRAPPER;
     /**
      * 占位符
      */
-    private static final String PLACE_HOLDER = "{%s}";
-    private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}";
     protected final Children typedThis = (Children) this;
-    protected final String paramAlias = null;
     /**
      * 必要度量
      */
     protected AtomicInteger paramNameSeq;
     protected Map<String, Object> paramNameValuePairs;
-    protected String lastSql = StringPool.EMPTY;
+    protected SharedString lastSql;
+    /**
+     * SQL注释
+     */
+    protected SharedString sqlComment;
     /**
      * 数据库表映射实体类
      */
@@ -73,10 +71,6 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
      * 实体类型
      */
     protected Class<T> entityClass;
-    /**
-     * SQL注释
-     */
-    private String sqlCommentBody;
 
     @Override
     public T getEntity() {
@@ -100,27 +94,6 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
         return entityClass;
     }
 
-    protected String getSqlCommentBody() {
-        return sqlCommentBody;
-    }
-
-    /**
-     * @param sqlCommentBody SQL注释body
-     */
-    public Children setSqlCommentBody(String sqlCommentBody) {
-        this.sqlCommentBody = sqlCommentBody;
-        return typedThis;
-    }
-
-    @Override
-    public String getSqlComment() {
-        if (sqlCommentBody != null && !sqlCommentBody.isEmpty()) {
-            return "/*" + StringEscape.escapeRawString(sqlCommentBody) + "*/";
-        } else {
-            return null;
-        }
-    }
-
     @Override
     public <V> Children allEq(boolean condition, Map<R, V> params, boolean null2IsNull) {
         if (condition && CollectionUtils.isNotEmpty(params)) {
@@ -244,7 +217,7 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
     @Override
     public Children last(boolean condition, String lastSql) {
         if (condition) {
-            this.lastSql = StringPool.SPACE + lastSql;
+            this.lastSql = new SharedString(StringPool.SPACE + lastSql);
         }
         return typedThis;
     }
@@ -400,9 +373,9 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
         }
         if (ArrayUtils.isNotEmpty(params)) {
             for (int i = 0; i < params.length; ++i) {
-                String genParamName = MP_GENERAL_PARAMNAME + paramNameSeq.incrementAndGet();
-                sqlStr = sqlStr.replace(String.format(PLACE_HOLDER, i),
-                    String.format(MYBATIS_PLUS_TOKEN, getParamAlias(), genParamName));
+                String genParamName = Constants.WRAPPER_PARAM + paramNameSeq.incrementAndGet();
+                sqlStr = sqlStr.replace(String.format("{%s}", i),
+                    String.format(Constants.WRAPPER_PARAM_FORMAT, Constants.WRAPPER, genParamName));
                 paramNameValuePairs.put(genParamName, params[i]);
             }
         }
@@ -426,6 +399,8 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
         paramNameSeq = new AtomicInteger(0);
         paramNameValuePairs = new HashMap<>(16);
         expression = new MergeSegments();
+        lastSql = SharedString.emptyString();
+        sqlComment = SharedString.emptyString();
     }
 
     /**
@@ -442,58 +417,41 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
         return typedThis;
     }
 
-    @SuppressWarnings("all")
-    public String getParamAlias() {
-        return StringUtils.isEmpty(paramAlias) ? DEFAULT_PARAM_ALIAS : paramAlias;
-    }
-
     @Override
     public String getSqlSegment() {
         String sqlSegment = expression.getSqlSegment();
         if (StringUtils.isNotEmpty(sqlSegment)) {
-            return sqlSegment + lastSql;
+            return sqlSegment + lastSql.getStringValue();
         }
-        if (StringUtils.isNotEmpty(lastSql)) {
-            return lastSql;
+        if (StringUtils.isNotEmpty(lastSql.getStringValue())) {
+            return lastSql.getStringValue();
         }
         return null;
     }
 
     @Override
-    public String getCustomSqlSegment() {
-        MergeSegments expression = getExpression();
-        if (Objects.nonNull(expression)) {
-            NormalSegmentList normal = expression.getNormal();
-            String sqlSegment = getSqlSegment();
-            if (StringUtils.isNotEmpty(sqlSegment)) {
-                if (normal.isEmpty()) {
-                    return sqlSegment;
-                } else {
-                    return concatWhere(sqlSegment);
-                }
-            }
-        }
-        return StringUtils.EMPTY;
+    public MergeSegments getExpression() {
+        return expression;
     }
 
-    /**
-     * 拼接`WHERE`至SQL前
-     *
-     * @param sql sql
-     * @return 带 where 的 sql
-     */
-    private String concatWhere(String sql) {
-        return Constants.WHERE + " " + sql;
+    public Map<String, Object> getParamNameValuePairs() {
+        return paramNameValuePairs;
     }
 
-
     @Override
-    public MergeSegments getExpression() {
-        return expression;
+    public String getSqlComment() {
+        if (StringUtils.isNotEmpty(sqlComment.getStringValue())) {
+            return "/*" + StringEscape.escapeRawString(sqlComment.getStringValue()) + "*/";
+        }
+        return null;
     }
 
-    public Map<String, Object> getParamNameValuePairs() {
-        return paramNameValuePairs;
+    /**
+     * @param sqlComment SQL注释body
+     */
+    public Children setSqlComment(String sqlComment) {
+        this.sqlComment = new SharedString(sqlComment);
+        return typedThis;
     }
 
     /**

+ 14 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/SharedString.java

@@ -15,7 +15,11 @@
  */
 package com.baomidou.mybatisplus.core.conditions;
 
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.experimental.Accessors;
 
 import java.io.Serializable;
 
@@ -26,6 +30,9 @@ import java.io.Serializable;
  * @since 2018-11-20
  */
 @Data
+@Accessors(chain = true)
+@NoArgsConstructor
+@AllArgsConstructor
 public class SharedString implements Serializable {
     private static final long serialVersionUID = -1536422416594422874L;
 
@@ -33,4 +40,11 @@ public class SharedString implements Serializable {
      * 共享的 string 值
      */
     private String stringValue;
+
+    /**
+     * SharedString 里是 ""
+     */
+    public static SharedString emptyString() {
+        return new SharedString(StringPool.EMPTY);
+    }
 }

+ 19 - 7
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/Wrapper.java

@@ -15,15 +15,13 @@
  */
 package com.baomidou.mybatisplus.core.conditions;
 
-import java.util.Objects;
-
 import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
+import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList;
 import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
-import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
-import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
-import com.baomidou.mybatisplus.core.toolkit.StringUtils;
-import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
+import com.baomidou.mybatisplus.core.toolkit.*;
+
+import java.util.Objects;
 
 /**
  * 条件构造抽象类
@@ -67,7 +65,21 @@ public abstract class Wrapper<T> implements ISqlSegment {
      * <p>3.用法 ${ew.customSqlSegment} (不需要where标签包裹,切记!)</p>
      * <p>4.ew是wrapper定义别名,可自行替换</p>
      */
-    public abstract String getCustomSqlSegment();
+    public String getCustomSqlSegment() {
+        MergeSegments expression = getExpression();
+        if (Objects.nonNull(expression)) {
+            NormalSegmentList normal = expression.getNormal();
+            String sqlSegment = getSqlSegment();
+            if (StringUtils.isNotEmpty(sqlSegment)) {
+                if (normal.isEmpty()) {
+                    return sqlSegment;
+                } else {
+                    return Constants.WHERE + " " + sqlSegment;
+                }
+            }
+        }
+        return StringUtils.EMPTY;
+    }
 
     /**
      * 查询条件为空(包含entity)

+ 10 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/query/LambdaQueryWrapper.java

@@ -15,10 +15,6 @@
  */
 package com.baomidou.mybatisplus.core.conditions.query;
 
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.function.Predicate;
-
 import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper;
 import com.baomidou.mybatisplus.core.conditions.SharedString;
 import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
@@ -27,6 +23,10 @@ import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Predicate;
+
 /**
  * Lambda 语法使用 Wrapper
  *
@@ -61,13 +61,16 @@ public class LambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, LambdaQueryW
      * 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(...)
      */
     LambdaQueryWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
-                       Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
+                       Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
+                       SharedString lastSql, SharedString sqlComment) {
         super.setEntity(entity);
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;
         this.expression = mergeSegments;
         this.sqlSelect = sqlSelect;
         this.entityClass = entityClass;
+        this.lastSql = lastSql;
+        this.sqlComment = sqlComment;
     }
 
     /**
@@ -118,6 +121,7 @@ public class LambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, LambdaQueryW
      */
     @Override
     protected LambdaQueryWrapper<T> instance() {
-        return new LambdaQueryWrapper<>(entity, entityClass, null, paramNameSeq, paramNameValuePairs, new MergeSegments());
+        return new LambdaQueryWrapper<>(entity, entityClass, null, paramNameSeq, paramNameValuePairs,
+            new MergeSegments(), null, null);
     }
 }

+ 12 - 7
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/query/QueryWrapper.java

@@ -15,10 +15,6 @@
  */
 package com.baomidou.mybatisplus.core.conditions.query;
 
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.function.Predicate;
-
 import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
 import com.baomidou.mybatisplus.core.conditions.SharedString;
 import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
@@ -27,6 +23,10 @@ import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
 
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Predicate;
+
 /**
  * Entity 对象封装操作类
  *
@@ -63,12 +63,15 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
      * @param entityClass 本不应该需要的
      */
     private QueryWrapper(T entity, Class<T> entityClass, AtomicInteger paramNameSeq,
-                         Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
+                         Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
+                         SharedString lastSql, SharedString sqlComment) {
         super.setEntity(entity);
         this.entityClass = entityClass;
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;
         this.expression = mergeSegments;
+        this.lastSql = lastSql;
+        this.sqlComment = sqlComment;
     }
 
     @Override
@@ -100,7 +103,8 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
      * 返回一个支持 lambda 函数写法的 wrapper
      */
     public LambdaQueryWrapper<T> lambda() {
-        return new LambdaQueryWrapper<>(entity, entityClass, sqlSelect, paramNameSeq, paramNameValuePairs, expression).setSqlCommentBody(getSqlCommentBody());
+        return new LambdaQueryWrapper<>(entity, entityClass, sqlSelect, paramNameSeq, paramNameValuePairs, expression,
+            lastSql, sqlComment);
     }
 
     /**
@@ -111,6 +115,7 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
      */
     @Override
     protected QueryWrapper<T> instance() {
-        return new QueryWrapper<>(entity, entityClass, paramNameSeq, paramNameValuePairs, new MergeSegments());
+        return new QueryWrapper<>(entity, entityClass, paramNameSeq, paramNameValuePairs, new MergeSegments(),
+            null, null);
     }
 }

+ 12 - 7
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/update/LambdaUpdateWrapper.java

@@ -15,18 +15,19 @@
  */
 package com.baomidou.mybatisplus.core.conditions.update;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicInteger;
-
 import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper;
+import com.baomidou.mybatisplus.core.conditions.SharedString;
 import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
 /**
  * Lambda 更新封装
  *
@@ -63,12 +64,15 @@ public class LambdaUpdateWrapper<T> extends AbstractLambdaWrapper<T, LambdaUpdat
      * 不建议直接 new 该实例,使用 Wrappers.lambdaUpdate(...)
      */
     LambdaUpdateWrapper(T entity, List<String> sqlSet, AtomicInteger paramNameSeq,
-                        Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
+                        Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
+                        SharedString lastSql, SharedString sqlComment) {
         super.setEntity(entity);
         this.sqlSet = sqlSet;
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;
         this.expression = mergeSegments;
+        this.lastSql = lastSql;
+        this.sqlComment = sqlComment;
     }
 
     @Override
@@ -97,6 +101,7 @@ public class LambdaUpdateWrapper<T> extends AbstractLambdaWrapper<T, LambdaUpdat
 
     @Override
     protected LambdaUpdateWrapper<T> instance() {
-        return new LambdaUpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs, new MergeSegments());
+        return new LambdaUpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs, new MergeSegments(),
+            null, null);
     }
 }

+ 8 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/update/UpdateWrapper.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.core.conditions.update;
 
 import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
+import com.baomidou.mybatisplus.core.conditions.SharedString;
 import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
@@ -53,12 +54,15 @@ public class UpdateWrapper<T> extends AbstractWrapper<T, String, UpdateWrapper<T
     }
 
     private UpdateWrapper(T entity, List<String> sqlSet, AtomicInteger paramNameSeq,
-                          Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
+                          Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
+                          SharedString lastSql, SharedString sqlComment) {
         super.setEntity(entity);
         this.sqlSet = sqlSet;
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;
         this.expression = mergeSegments;
+        this.lastSql = lastSql;
+        this.sqlComment = sqlComment;
     }
 
     @Override
@@ -89,11 +93,12 @@ public class UpdateWrapper<T> extends AbstractWrapper<T, String, UpdateWrapper<T
      * 返回一个支持 lambda 函数写法的 wrapper
      */
     public LambdaUpdateWrapper<T> lambda() {
-        return new LambdaUpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs, expression).setSqlCommentBody(getSqlCommentBody());
+        return new LambdaUpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs, expression, lastSql, sqlComment);
     }
 
     @Override
     protected UpdateWrapper<T> instance() {
-        return new UpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs, new MergeSegments());
+        return new UpdateWrapper<>(entity, sqlSet, paramNameSeq, paramNameValuePairs, new MergeSegments(),
+            null, null);
     }
 }

+ 3 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Constants.java

@@ -115,4 +115,7 @@ public interface Constants extends StringPool {
     String MP_OPTLOCK_VERSION_ORIGINAL = "MP_OPTLOCK_VERSION_ORIGINAL";
     String MP_OPTLOCK_VERSION_COLUMN = "MP_OPTLOCK_VERSION_COLUMN";
     String MP_OPTLOCK_ET_ORIGINAL = "MP_OPTLOCK_ET_ORIGINAL";
+
+    String WRAPPER_PARAM = "MPGENVAL";
+    String WRAPPER_PARAM_FORMAT = "#{%s.paramNameValuePairs.%s}";
 }