浏览代码

TableInfo 构建优化

miemie 5 年之前
父节点
当前提交
481959035b

+ 10 - 8
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -23,6 +23,7 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
 import lombok.*;
 import org.apache.ibatis.mapping.ResultMapping;
+import org.apache.ibatis.reflection.Reflector;
 import org.apache.ibatis.type.JdbcType;
 import org.apache.ibatis.type.TypeHandler;
 import org.apache.ibatis.type.TypeHandlerRegistry;
@@ -88,15 +89,15 @@ public class TableFieldInfo implements Constants {
      * @since added v_3.1.2 @2019-5-7
      */
     private final FieldStrategy whereStrategy;
+    /**
+     * 是否是乐观锁字段
+     */
+    private final boolean version;
     /**
      * 是否进行 select 查询
      * <p>大字段可设置为 false 不加入 select 查询范围</p>
      */
     private boolean select = true;
-    /**
-     * 是否是乐观锁字段
-     */
-    private boolean version;
     /**
      * 逻辑删除值
      */
@@ -151,12 +152,13 @@ public class TableFieldInfo implements Constants {
      * 全新的 存在 TableField 注解时使用的构造函数
      */
     @SuppressWarnings("unchecked")
-    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, TableField tableField) {
+    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, TableField tableField,
+                          Reflector reflector) {
         field.setAccessible(true);
         this.field = field;
         this.version = field.getAnnotation(Version.class) != null;
         this.property = field.getName();
-        this.propertyType = field.getType();
+        this.propertyType = reflector.getGetterType(this.property);
         this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
         this.fieldFill = tableField.fill();
         this.withInsertFill = this.fieldFill == FieldFill.INSERT || this.fieldFill == FieldFill.INSERT_UPDATE;
@@ -233,12 +235,12 @@ public class TableFieldInfo implements Constants {
     /**
      * 不存在 TableField 注解时, 使用的构造函数
      */
-    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field) {
+    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, Reflector reflector) {
         field.setAccessible(true);
         this.field = field;
         this.version = field.getAnnotation(Version.class) != null;
         this.property = field.getName();
-        this.propertyType = field.getType();
+        this.propertyType = reflector.getGetterType(this.property);
         this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
         this.el = this.property;
         this.insertStrategy = dbConfig.getInsertStrategy();

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

@@ -378,7 +378,7 @@ public class TableInfo implements Constants {
      *
      * @param field   TableFieldInfo
      * @param isWhere true: logicDeleteValue, false: logicNotDeleteValue
-     * @return
+     * @return sql
      */
     private String formatLogicDeleteSql(TableFieldInfo field, boolean isWhere) {
         final String value = isWhere ? field.getLogicNotDeleteValue() : field.getLogicDeleteValue();
@@ -436,4 +436,8 @@ public class TableInfo implements Constants {
             }
         });
     }
+
+    public List<TableFieldInfo> getFieldList() {
+        return Collections.unmodifiableList(fieldList);
+    }
 }

+ 9 - 28
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfoHelper.java

@@ -100,7 +100,7 @@ public class TableInfoHelper {
      */
     @SuppressWarnings("unused")
     public static List<TableInfo> getTableInfos() {
-        return new ArrayList<>(TABLE_INFO_CACHE.values());
+        return Collections.unmodifiableList(new ArrayList<>(TABLE_INFO_CACHE.values()));
     }
 
     /**
@@ -260,7 +260,8 @@ public class TableInfoHelper {
                     if (isReadPK) {
                         throw ExceptionUtils.mpe("@TableId can't more than one in Class: \"%s\".", clazz.getName());
                     } else {
-                        isReadPK = initTableIdWithAnnotation(dbConfig, tableInfo, field, tableId, reflector);
+                        initTableIdWithAnnotation(dbConfig, tableInfo, field, tableId, reflector);
+                        isReadPK = true;
                         continue;
                     }
                 }
@@ -270,14 +271,16 @@ public class TableInfoHelper {
                     continue;
                 }
             }
+            final TableField tableField = field.getAnnotation(TableField.class);
 
             /* 有 @TableField 注解的字段初始化 */
-            if (initTableFieldWithAnnotation(dbConfig, tableInfo, fieldList, field)) {
+            if (tableField != null) {
+                fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field, tableField, reflector));
                 continue;
             }
 
             /* 无 @TableField 注解的字段初始化 */
-            fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field));
+            fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field, reflector));
         }
 
         /* 检查逻辑删除字段只能有最多一个 */
@@ -316,8 +319,8 @@ public class TableInfoHelper {
      * @param tableId   注解
      * @param reflector Reflector
      */
-    private static boolean initTableIdWithAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
-                                                     Field field, TableId tableId, Reflector reflector) {
+    private static void initTableIdWithAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
+                                                  Field field, TableId tableId, Reflector reflector) {
         boolean underCamel = tableInfo.isUnderCamel();
         final String property = field.getName();
         if (field.getAnnotation(TableField.class) != null) {
@@ -350,7 +353,6 @@ public class TableInfoHelper {
             .setKeyColumn(column)
             .setKeyProperty(property)
             .setKeyType(reflector.getGetterType(property));
-        return true;
     }
 
     /**
@@ -385,27 +387,6 @@ public class TableInfoHelper {
         return false;
     }
 
-    /**
-     * <p>
-     * 字段属性初始化
-     * </p>
-     *
-     * @param dbConfig  数据库全局配置
-     * @param tableInfo 表信息
-     * @param fieldList 字段列表
-     * @return true 继续下一个属性判断,返回 continue;
-     */
-    private static boolean initTableFieldWithAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
-                                                        List<TableFieldInfo> fieldList, Field field) {
-        /* 获取注解属性,自定义字段 */
-        TableField tableField = field.getAnnotation(TableField.class);
-        if (null == tableField) {
-            return false;
-        }
-        fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field, tableField));
-        return true;
-    }
-
     /**
      * 判定 related 的值
      * <p>

+ 3 - 5
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/LambdaUtils.java

@@ -97,13 +97,11 @@ public final class LambdaUtils {
      * @return 缓存 map
      */
     private static Map<String, ColumnCache> createColumnCacheMap(TableInfo info) {
-
-        String kp = info.getKeyProperty();
         Map<String, ColumnCache> map;
 
-        if (StringUtils.isNotBlank(kp)) {
+        if (info.havePK()) {
             map = Maps.newHashMapWithExpectedSize(info.getFieldList().size() + 1);
-            map.put(formatKey(kp), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect()));
+            map.put(formatKey(info.getKeyProperty()), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect()));
         } else {
             map = Maps.newHashMapWithExpectedSize(info.getFieldList().size());
         }
@@ -126,5 +124,5 @@ public final class LambdaUtils {
             return info == null ? null : createColumnCacheMap(info);
         });
     }
-    
+
 }