Browse Source

启动就缓存 lambdacache

miemie 6 years ago
parent
commit
495deb13dc

+ 18 - 8
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractLambdaWrapper.java

@@ -16,7 +16,7 @@
 package com.baomidou.mybatisplus.core.conditions;
 
 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
-import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
@@ -25,7 +25,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
 import org.apache.ibatis.reflection.property.PropertyNamer;
 
 import java.util.Arrays;
-import java.util.Optional;
+import java.util.Map;
 
 import static java.util.stream.Collectors.joining;
 
@@ -40,9 +40,16 @@ import static java.util.stream.Collectors.joining;
 public abstract class AbstractLambdaWrapper<T, Children extends AbstractLambdaWrapper<T, Children>>
     extends AbstractWrapper<T, SFunction<T, ?>, Children> {
 
+    private Map<String, ColumnCache> columnMap = null;
+    private boolean initColumnMap = false;
+
     @Override
     protected void initEntityClass() {
         super.initEntityClass();
+        if (entityClass != null) {
+            columnMap = LambdaUtils.getColumnMap(entityClass);
+            initColumnMap = true;
+        }
     }
 
     @SuppressWarnings("unchecked")
@@ -79,11 +86,14 @@ public abstract class AbstractLambdaWrapper<T, Children extends AbstractLambdaWr
      */
     private String getColumn(SerializedLambda lambda, boolean onlyColumn) throws MybatisPlusException {
         String fieldName = PropertyNamer.methodToProperty(lambda.getImplMethodName());
-
-        return Optional.ofNullable(LambdaUtils.getColumnOfProperty(lambda.getInstantiatedMethodType(), fieldName))
-            .map(onlyColumn ? ColumnCache::getColumn : ColumnCache::getColumnSelect)
-            .orElseThrow(() ->
-                ExceptionUtils.mpe("Your property named \"%s\" cannot find the corresponding database column name!", fieldName)
-            );
+        Class aClass = lambda.getInstantiatedMethodType();
+        if (!initColumnMap) {
+            columnMap = LambdaUtils.getColumnMap(aClass);
+            Assert.notNull(columnMap, "can not find lambda cache for this entity [%s]", aClass.getName());
+        }
+        ColumnCache columnCache = columnMap.get(LambdaUtils.formatKey(fieldName));
+        Assert.notNull(columnCache, "can not find lambda cache for this property [%s] of entity [%s]",
+            fieldName, aClass.getName());
+        return onlyColumn ? columnCache.getColumn() : columnCache.getColumnSelect();
     }
 }

+ 19 - 14
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;
@@ -39,7 +40,7 @@ public final class LambdaUtils {
     /**
      * 字段映射
      */
-    private static final Map<Class<?>, Map<String, ColumnCache>> LAMBDA_MAP = new ConcurrentHashMap<>();
+    private static final Map<String, Map<String, ColumnCache>> LAMBDA_MAP = new ConcurrentHashMap<>();
 
     /**
      * SerializedLambda 反序列化缓存
@@ -76,23 +77,17 @@ public final class LambdaUtils {
      * @param key key
      * @return 大写的 key
      */
-    private static String formatKey(String key) {
+    public static String formatKey(String key) {
         return key.toUpperCase(ENGLISH);
     }
 
     /**
-     * 获取 clazz 中某字段对应的列信息
+     * 将传入的表信息加入缓存
      *
-     * @param clazz     class 类
-     * @param fieldName 字段名称
-     * @return 如果存在,返回字段信息;否则返回 null
+     * @param tableInfo 表信息
      */
-    public static ColumnCache getColumnOfProperty(Class<?> clazz, String fieldName) {
-        Map<String, ColumnCache> map = LAMBDA_MAP.computeIfAbsent(clazz, c -> {
-            TableInfo ti = TableInfoHelper.getTableInfo(c);
-            return null == ti ? null : createColumnCacheMap(ti);
-        });
-        return null == map ? null : map.get(formatKey(fieldName));
+    public static void installCache(TableInfo tableInfo) {
+        LAMBDA_MAP.put(tableInfo.getEntityType().getName(), createColumnCacheMap(tableInfo));
     }
 
     /**
@@ -102,10 +97,10 @@ public final class LambdaUtils {
      * @return 缓存 map
      */
     private static Map<String, ColumnCache> createColumnCacheMap(TableInfo info) {
-        Map<String, ColumnCache> map = new HashMap<>(info.getFieldList().size() + 1);
+        Map<String, ColumnCache> map = new HashMap<>();
 
         String kp = info.getKeyProperty();
-        if (null != kp) {
+        if (StringUtils.isNotEmpty(kp)) {
             map.put(formatKey(kp), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect()));
         }
 
@@ -114,4 +109,14 @@ public final class LambdaUtils {
         );
         return map;
     }
+
+    /**
+     * 获取实体对应字段 MAP
+     *
+     * @param clazz 实体类
+     * @return 缓存 map
+     */
+    public static Map<String, ColumnCache> getColumnMap(Class<?> clazz) {
+        return LAMBDA_MAP.getOrDefault(clazz.getName(), Collections.emptyMap());
+    }
 }

+ 5 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/TableInfoHelper.java

@@ -141,11 +141,14 @@ public class TableInfoHelper {
         /* 初始化字段相关 */
         initTableFields(clazz, globalConfig, tableInfo);
 
-// todo 暂时不开放  tableInfo.initResultMapIfNeed();
-
         /* 放入缓存 */
         TABLE_INFO_CACHE.put(clazz, tableInfo);
 
+        /* 缓存 lambda */
+        LambdaUtils.installCache(tableInfo);
+
+        // todo 暂时不开放  tableInfo.initResultMapIfNeed();
+
         return tableInfo;
     }