Browse Source

优化完毕,待测试

miemie 7 years ago
parent
commit
b8b79d094e

+ 1 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/config/GlobalConfig.java

@@ -110,6 +110,7 @@ public class GlobalConfig implements Serializable {
         private boolean tableUnderline = true;
         /**
          * 字段名、是否使用下划线命名(默认 true:默认数据库字段下划线命名)
+         * 与 Mybatis 的属性 mapUnderscoreToCamelCase 意义一致,并会设置到 configuration
          */
         private boolean columnUnderline = true;
         /**

+ 26 - 34
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.core.metadata;
 import com.baomidou.mybatisplus.annotation.*;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
 import lombok.Data;
 import lombok.experimental.Accessors;
 
@@ -90,29 +91,24 @@ public class TableFieldInfo {
      * 存在 TableField 注解构造函数
      * </p>
      */
-    public TableFieldInfo(boolean underCamel, GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
-                          String column, String el, Field field, TableField tableField, Class<?> clazz) {
+    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field,
+                          String column, String el, TableField tableField) {
         this.property = field.getName();
         this.propertyType = field.getType();
-        /*
-         * 1、注解 value 不存在,开启字段下划线申明<br>
-         * 2、没有开启下划线申明,但是column与property不等的情况<br>
-         * 设置 related 为 true
-         */
+        this.fieldFill = tableField.fill();
+        this.clazz = field.getDeclaringClass();
+        this.update = tableField.update();
+        this.el = el;
+        tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
+
         if (StringUtils.isEmpty(tableField.value())) {
-            /* 开启字段下划线申明 */
             if (dbConfig.isColumnUnderline()) {
                 column = StringUtils.camelToUnderline(column);
             }
-            /* 未开启下划线转驼峰模式 AS 转换 */
-            if (!underCamel) {
-                this.related = true;
-            }
-        } else if (!column.equals(this.property)) {
-            this.related = true;
         }
         this.column = column;
-        this.el = el;
+        this.related = TableInfoHelper.checkRelated(dbConfig.isColumnUnderline(), this.property, this.column);
+
         /*
          * 优先使用单个字段注解,否则使用全局配置
          */
@@ -121,8 +117,7 @@ public class TableFieldInfo {
         } else {
             this.fieldStrategy = dbConfig.getFieldStrategy();
         }
-        tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
-        this.update = tableField.update();
+
         if (StringUtils.isNotEmpty(tableField.condition())) {
             // 细粒度条件控制
             this.condition = tableField.condition();
@@ -130,31 +125,28 @@ public class TableFieldInfo {
             // 全局配置
             this.setCondition(dbConfig);
         }
-        /*
-         * 保存当前字段的填充策略
-         */
-        this.fieldFill = tableField.fill();
-        this.clazz = clazz;
     }
 
-    public TableFieldInfo(boolean underCamel, GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, Class<?> clazz) {
-        if (dbConfig.isColumnUnderline()) {
-            /* 开启字段下划线申明 */
-            this.column = StringUtils.camelToUnderline(field.getName());
-            /* 未开启下划线转驼峰模式 AS 转换 */
-            if (!underCamel) {
-                this.related = true;
-            }
-        } else {
-            this.column = field.getName();
-        }
+    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field) {
         this.property = field.getName();
         this.el = field.getName();
         this.fieldStrategy = dbConfig.getFieldStrategy();
         this.propertyType = field.getType();
         this.setCondition(dbConfig);
-        this.clazz = clazz;
+        this.clazz = field.getDeclaringClass();
         tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
+
+        String column = field.getName();
+        if (dbConfig.isColumnUnderline()) {
+            /* 开启字段下划线申明 */
+            column = StringUtils.camelToUnderline(column);
+        }
+        if (dbConfig.isCapitalMode()) {
+            /* 开启字段全大写申明 */
+            column = column.toUpperCase();
+        }
+        this.column = column;
+        this.related = TableInfoHelper.checkRelated(dbConfig.isColumnUnderline(), this.property, this.column);
     }
 
     /**

+ 78 - 65
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/TableInfoHelper.java

@@ -156,14 +156,11 @@ public class TableInfoHelper {
             globalConfig = GlobalConfigUtils.defaults();
         }
 
-        /* 是否开启下划线转驼峰模式(开启后数据库不管大小写都能自动映射) */
-        boolean underCamel = globalConfig.getDbConfig().isColumnUnderline();
-
         /* 初始化表名相关 */
         initTableName(clazz, globalConfig, tableInfo);
 
         /* 初始化字段相关 */
-        initTableFields(clazz, underCamel, globalConfig, tableInfo);
+        initTableFields(clazz, globalConfig, tableInfo);
 
         /* 放入缓存 */
         TABLE_INFO_CACHE.put(clazz.getName(), tableInfo);
@@ -178,7 +175,7 @@ public class TableInfoHelper {
      * 初始化 表数据库类型,表名,resultMap
      * </p>
      *
-     * @param clazz        class of entity
+     * @param clazz        实体类
      * @param globalConfig 全局配置
      * @param tableInfo    数据库表反射信息
      */
@@ -228,18 +225,17 @@ public class TableInfoHelper {
      * 初始化 表主键,表字段
      * </p>
      *
-     * @param clazz        class of entity
-     * @param underCamel   下划线转驼峰
+     * @param clazz        实体类
      * @param globalConfig 全局配置
      * @param tableInfo    数据库表反射信息
      */
-    public static void initTableFields(Class<?> clazz, boolean underCamel, GlobalConfig globalConfig,
-                                       TableInfo tableInfo) {
+    public static void initTableFields(Class<?> clazz, GlobalConfig globalConfig, TableInfo tableInfo) {
         /* 数据库全局配置 */
         GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
         List<Field> list = getAllFields(clazz);
         // 标记是否读取到主键
         boolean isReadPK = false;
+        // 是否存在 @TableId 注解
         boolean existTableId = isExistTableId(list);
 
         List<TableFieldInfo> fieldList = new ArrayList<>();
@@ -249,24 +245,21 @@ public class TableInfoHelper {
              */
             if (!isReadPK) {
                 if (existTableId) {
-                    isReadPK = initTableId(underCamel, dbConfig, tableInfo, field, clazz);
+                    isReadPK = initTableIdWithAnnotation(dbConfig, tableInfo, field, clazz);
                 } else {
-                    isReadPK = initFieldId(dbConfig, tableInfo, field, clazz);
+                    isReadPK = initTableIdWithoutAnnotation(dbConfig, tableInfo, field, clazz);
                 }
                 if (isReadPK) {
                     continue;
                 }
             }
-            /*
-             * 字段初始化
-             */
-            if (initTableField(underCamel, dbConfig, tableInfo, fieldList, field, clazz)) {
+            /* 有 @TableField 注解的字段初始化 */
+            if (initTableFieldWithAnnotation(dbConfig, tableInfo, fieldList, field, clazz)) {
                 continue;
             }
-            /*
-             * 字段, 使用 camelToUnderline 转换驼峰写法为下划线分割法, 如果已指定 TableField , 便不会执行这里
-             */
-            fieldList.add(new TableFieldInfo(underCamel, dbConfig, tableInfo, field, field.getDeclaringClass()));
+
+            /* 无 @TableField 注解的字段初始化 */
+            fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field));
         }
 
         /* 字段列表 */
@@ -302,49 +295,44 @@ public class TableInfoHelper {
      * 主键属性初始化
      * </p>
      *
-     * @param underCamel 下划线转驼峰模式
-     * @param tableInfo  表信息
-     * @param field      字段
-     * @param clazz      实体类
+     * @param dbConfig  全局配置信息
+     * @param tableInfo 表信息
+     * @param field     字段
+     * @param clazz     实体类
      * @return true 继续下一个属性判断,返回 continue;
      */
-    private static boolean initTableId(boolean underCamel, GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
-                                       Field field, Class<?> clazz) {
+    private static boolean initTableIdWithAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
+                                                     Field field, Class<?> clazz) {
         TableId tableId = field.getAnnotation(TableId.class);
+        boolean underCamel = dbConfig.isColumnUnderline();
         if (tableId != null) {
             if (StringUtils.isEmpty(tableInfo.getKeyColumn())) {
-                /*
-                 * 主键策略( 注解 > 全局 > 默认 )
-                 */
+                /* 主键策略( 注解 > 全局 ) */
                 // 设置 Sequence 其他策略无效
-                if (IdType.NONE != tableId.type()) {
-                    tableInfo.setIdType(tableId.type());
-                } else {
+                if (IdType.NONE == tableId.type()) {
                     tableInfo.setIdType(dbConfig.getIdType());
+                } else {
+                    tableInfo.setIdType(tableId.type());
                 }
 
                 /* 字段 */
                 String column = field.getName();
                 if (StringUtils.isNotEmpty(tableId.value())) {
                     column = tableId.value();
-                    tableInfo.setKeyRelated(true);
                 } else {
                     // 开启字段下划线申明
-                    if (dbConfig.isColumnUnderline()) {
+                    if (underCamel) {
                         column = StringUtils.camelToUnderline(column);
-                        // 未开启下户线转驼峰 AS 转换
-                        if (!underCamel) {
-                            tableInfo.setKeyRelated(true);
-                        }
                     }
                     // 全局大写命名
                     if (dbConfig.isCapitalMode()) {
                         column = column.toUpperCase();
                     }
                 }
-                tableInfo.setClazz(field.getDeclaringClass());
-                tableInfo.setKeyColumn(column);
-                tableInfo.setKeyProperty(field.getName());
+                tableInfo.setKeyRelated(checkRelated(underCamel, field.getName(), column))
+                    .setClazz(field.getDeclaringClass())
+                    .setKeyColumn(column)
+                    .setKeyProperty(field.getName());
                 return true;
             } else {
                 throwExceptionId(clazz);
@@ -363,18 +351,19 @@ public class TableInfoHelper {
      * @param clazz     实体类
      * @return true 继续下一个属性判断,返回 continue;
      */
-    private static boolean initFieldId(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
-                                       Field field, Class<?> clazz) {
+    private static boolean initTableIdWithoutAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
+                                                        Field field, Class<?> clazz) {
         String column = field.getName();
         if (dbConfig.isCapitalMode()) {
             column = column.toUpperCase();
         }
         if (DEFAULT_ID_NAME.equalsIgnoreCase(column)) {
             if (StringUtils.isEmpty(tableInfo.getKeyColumn())) {
-                tableInfo.setIdType(dbConfig.getIdType());
-                tableInfo.setKeyColumn(column);
-                tableInfo.setKeyProperty(field.getName());
-                tableInfo.setClazz(field.getDeclaringClass());
+                tableInfo.setKeyRelated(checkRelated(dbConfig.isColumnUnderline(), field.getName(), column))
+                    .setIdType(dbConfig.getIdType())
+                    .setKeyColumn(column)
+                    .setKeyProperty(field.getName())
+                    .setClazz(field.getDeclaringClass());
                 return true;
             } else {
                 throwExceptionId(clazz);
@@ -383,30 +372,19 @@ public class TableInfoHelper {
         return false;
     }
 
-    /**
-     * <p>
-     * 发现设置多个主键注解抛出异常
-     * </p>
-     */
-    private static void throwExceptionId(Class<?> clazz) {
-        throw ExceptionUtils.mpe("There must be only one, Discover multiple @TableId annotation in " +
-            clazz.getName());
-    }
-
     /**
      * <p>
      * 字段属性初始化
      * </p>
      *
-     * @param underCamel 下划线转驼峰模式
-     * @param dbConfig   数据库全局配置
-     * @param tableInfo  表信息
-     * @param fieldList  字段列表
-     * @param clazz      当前表对象类
+     * @param dbConfig  数据库全局配置
+     * @param tableInfo 表信息
+     * @param fieldList 字段列表
+     * @param clazz     当前表对象类
      * @return true 继续下一个属性判断,返回 continue;
      */
-    private static boolean initTableField(boolean underCamel, GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
-                                          List<TableFieldInfo> fieldList, Field field, Class<?> clazz) {
+    private static boolean initTableFieldWithAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
+                                                        List<TableFieldInfo> fieldList, Field field, Class<?> clazz) {
         /* 获取注解属性,自定义字段 */
         TableField tableField = field.getAnnotation(TableField.class);
         if (null == tableField) {
@@ -427,8 +405,7 @@ public class TableInfoHelper {
         String[] els = el.split(StringPool.SEMICOLON);
         if (columns.length == els.length) {
             for (int i = 0; i < columns.length; i++) {
-                fieldList.add(new TableFieldInfo(underCamel, dbConfig, tableInfo,
-                    columns[i], els[i], field, tableField, field.getDeclaringClass()));
+                fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field, columns[i], els[i], tableField));
             }
             return true;
         }
@@ -436,6 +413,42 @@ public class TableInfoHelper {
             clazz.getName(), field.getName()));
     }
 
+    /**
+     * <p>
+     * 判定 related 的值
+     * </p>
+     *
+     * @param underCamel 驼峰命名
+     * @param property   属性名
+     * @param column     字段名
+     * @return related
+     */
+    public static boolean checkRelated(boolean underCamel, String property, String column) {
+        if (underCamel) {
+            /**
+             * 开启了驼峰,判断 property 下划线后是否与 column 相同 (全部转为小写)
+             * 相同则不需要 as ,则 related 为 false
+             */
+            return !StringUtils.underlineToCamel(property).equals(column.toLowerCase());
+        } else {
+            /**
+             * 未开启驼峰,判断 property 是否与 column 相同
+             * 相同则不需要 as ,则 related 为 false
+             */
+            return !property.equals(column);
+        }
+    }
+
+    /**
+     * <p>
+     * 发现设置多个主键注解抛出异常
+     * </p>
+     */
+    private static void throwExceptionId(Class<?> clazz) {
+        throw ExceptionUtils.mpe("There must be only one, Discover multiple @TableId annotation in " +
+            clazz.getName());
+    }
+
     /**
      * 获取该类的所有属性列表
      *