소스 검색

优化取缓存逻辑

miemie 6 년 전
부모
커밋
7254296312

+ 13 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractLambdaWrapper.java

@@ -41,6 +41,15 @@ public abstract class AbstractLambdaWrapper<T, This extends AbstractLambdaWrappe
     private Map<String, String> columnMap = null;
     private boolean initColumnMap = false;
 
+    @Override
+    protected void initEntityClass() {
+        super.initEntityClass();
+        if (entityClass != null) {
+            columnMap = LambdaUtils.getColumnMap(entityClass.getName());
+            initColumnMap = true;
+        }
+    }
+
     @Override
     protected String columnToString(SFunction<T, ?> column) {
         return getColumn(LambdaUtils.resolve(column));
@@ -48,13 +57,14 @@ public abstract class AbstractLambdaWrapper<T, This extends AbstractLambdaWrappe
 
     private String getColumn(SerializedLambda lambda) {
         String fieldName = StringUtils.resolveFieldName(lambda.getImplMethodName());
-        if (!initColumnMap || columnMap.get(fieldName) == null) {
+        if (!initColumnMap || !columnMap.containsKey(fieldName)) {
             String entityClassName = lambda.getImplClassName();
             columnMap = LambdaUtils.getColumnMap(entityClassName);
-            Assert.notEmpty(columnMap, "该模式不能应用于非 baseMapper 的泛型 entity 之外的 entity!");
+            Assert.notEmpty(columnMap, "cannot find column's cache for %s, so you cannot used %s!",
+                entityClassName, typedThis.getClass());
             initColumnMap = true;
         }
         return Optional.ofNullable(columnMap.get(fieldName))
-            .orElseThrow(() -> ExceptionUtils.mpe("该模式不能应用于非数据库字段!"));
+            .orElseThrow(() -> ExceptionUtils.mpe("your property named %s cannot find the corresponding database column name!", fieldName));
     }
 }

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

@@ -47,14 +47,13 @@ public class LambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, LambdaQueryW
     }
 
     public LambdaQueryWrapper(T entity) {
-        this.entity = entity;
-        this.initEntityClass();
+        this.setEntity(entity);
         this.initNeed();
     }
 
     LambdaQueryWrapper(T entity, Class<T> entityClass, String sqlSelect, AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs,
                        MergeSegments mergeSegments) {
-        this.entity = entity;
+        this.setEntity(entity);
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;
         this.expression = mergeSegments;

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

@@ -47,15 +47,13 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
     }
 
     public QueryWrapper(T entity) {
-        this.entity = entity;
-        this.initEntityClass();
+        this.setEntity(entity);
         this.initNeed();
     }
 
     public QueryWrapper(T entity, String... columns) {
-        this.entity = entity;
+        this.setEntity(entity);
         this.select(columns);
-        this.initEntityClass();
         this.initNeed();
     }
 
@@ -66,7 +64,7 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
      */
     private QueryWrapper(T entity, Class<T> entityClass, AtomicInteger paramNameSeq,
                          Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
-        this.entity = entity;
+        this.setEntity(entity);
         this.entityClass = entityClass;
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;

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

@@ -43,7 +43,7 @@ public class LambdaUpdateWrapper<T> extends AbstractLambdaWrapper<T, LambdaUpdat
 
     LambdaUpdateWrapper(T entity, List<String> sqlSet, AtomicInteger paramNameSeq,
                         Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
-        this.entity = entity;
+        super.setEntity(entity);
         this.sqlSet = sqlSet;
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;

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

@@ -47,14 +47,14 @@ public class UpdateWrapper<T> extends AbstractWrapper<T, String, UpdateWrapper<T
     }
 
     public UpdateWrapper(T entity) {
-        this.entity = entity;
+        super.setEntity(entity);
         this.sqlSet = new ArrayList<>();
         this.initNeed();
     }
 
     private UpdateWrapper(T entity, List<String> sqlSet, AtomicInteger paramNameSeq,
                           Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
-        this.entity = entity;
+        super.setEntity(entity);
         this.sqlSet = sqlSet;
         this.paramNameSeq = paramNameSeq;
         this.paramNameValuePairs = paramNameValuePairs;

+ 2 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -21,6 +21,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
 
 import java.lang.ref.WeakReference;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Optional;
@@ -122,6 +123,6 @@ public final class LambdaUtils {
      * @return 缓存 map
      */
     public static Map<String, String> getColumnMap(String entityClassName) {
-        return LAMBDA_CACHE.get(entityClassName);
+        return LAMBDA_CACHE.getOrDefault(entityClassName, Collections.emptyMap());
     }
 }