瀏覽代碼

优化秋秋的代码

miemie 6 年之前
父節點
當前提交
8a93abadbc

+ 105 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableFieldInfo.java

@@ -25,6 +25,9 @@ import lombok.AccessLevel;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.ToString;
+import org.apache.ibatis.type.JdbcType;
+import org.apache.ibatis.type.TypeHandler;
+import org.apache.ibatis.type.UnknownTypeHandler;
 
 import java.lang.reflect.Field;
 import java.util.Optional;
@@ -128,6 +131,18 @@ public class TableFieldInfo implements Constants {
      */
     @Getter(AccessLevel.NONE)
     private String sqlSelect;
+    /**
+     * JDBC类型
+     *
+     * @since 3.1.2
+     */
+    private JdbcType jdbcType;
+    /**
+     * 类型处理器
+     *
+     * @since 3.1.2
+     */
+    private Class<? extends TypeHandler<?>> typeHandler;
 
     /**
      * 存在 TableField 注解时, 使用的构造函数
@@ -196,6 +211,95 @@ public class TableFieldInfo implements Constants {
         this.select = tableField.select();
     }
 
+    /**
+     * 全新的 存在 TableField 注解并单独设置了 jdbcType 或者 typeHandler 时使用的构造函数
+     */
+    public TableFieldInfo(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo, Field field, TableField tableField) {
+        this.version = field.getAnnotation(Version.class) != null;
+        this.property = field.getName();
+        this.propertyType = field.getType();
+        this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
+        this.fieldFill = tableField.fill();
+        this.update = tableField.update();
+        JdbcType jdbcType = tableField.jdbcType();
+        Class<? extends TypeHandler<?>> typeHandler = tableField.typeHandler();
+        String el = this.property;
+        if (JdbcType.UNDEFINED != jdbcType) {
+            this.jdbcType = jdbcType;
+            el += (COMMA + "jdbcType=" + jdbcType.name());
+        }
+        if (UnknownTypeHandler.class != typeHandler) {
+            this.typeHandler = typeHandler;
+            el += (COMMA + "typeHandler=" + typeHandler.getName());
+        }
+        this.el = el;
+        tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
+
+        String column = tableField.value();
+        if (StringUtils.isEmpty(column)) {
+            column = this.property;
+            if (tableInfo.isUnderCamel()) {
+                /* 开启字段下划线申明 */
+                column = StringUtils.camelToUnderline(column);
+            }
+            if (dbConfig.isCapitalMode()) {
+                /* 开启字段全大写申明 */
+                column = column.toUpperCase();
+            }
+        }
+        String columnFormat = dbConfig.getColumnFormat();
+        if (StringUtils.isNotEmpty(columnFormat) && tableField.keepGlobalFormat()) {
+            column = String.format(columnFormat, column);
+        }
+
+        this.column = column;
+        this.related = TableInfoHelper.checkRelated(tableInfo.isUnderCamel(), this.property, this.column);
+
+        /*
+         * 优先使用单个字段注解,否则使用全局配置
+         */
+        if (tableField.strategy() == FieldStrategy.DEFAULT) {
+            this.fieldStrategy = dbConfig.getFieldStrategy();
+        } else {
+            this.fieldStrategy = tableField.strategy();
+        }
+        /*
+         * 优先使用单个字段注解,否则使用全局配置
+         */
+        if (tableField.insertStrategy() == FieldStrategy.DEFAULT) {
+            this.insertStrategy = dbConfig.getInsertStrategy();
+        } else {
+            this.insertStrategy = tableField.insertStrategy();
+        }
+        /*
+         * 优先使用单个字段注解,否则使用全局配置
+         */
+        if (tableField.updateStrategy() == FieldStrategy.DEFAULT) {
+            this.updateStrategy = dbConfig.getUpdateStrategy();
+        } else {
+            this.updateStrategy = tableField.updateStrategy();
+        }
+        /*
+         * 优先使用单个字段注解,否则使用全局配置
+         */
+        if (tableField.whereStrategy() == FieldStrategy.DEFAULT) {
+            this.whereStrategy = dbConfig.getSelectStrategy();
+        } else {
+            this.whereStrategy = tableField.whereStrategy();
+        }
+
+        if (StringUtils.isNotEmpty(tableField.condition())) {
+            // 细粒度条件控制
+            this.condition = tableField.condition();
+        } else {
+            // 全局配置
+            this.setCondition(dbConfig);
+        }
+
+        // 字段是否注入查询
+        this.select = tableField.select();
+    }
+
     /**
      * 不存在 TableField 注解时, 使用的构造函数
      */
@@ -204,7 +308,7 @@ public class TableFieldInfo implements Constants {
         this.property = field.getName();
         this.propertyType = field.getType();
         this.isCharSequence = StringUtils.isCharSequence(this.propertyType);
-        this.el = field.getName();
+        this.el = this.property;
         this.fieldStrategy = dbConfig.getFieldStrategy();
         this.insertStrategy = dbConfig.getInsertStrategy();
         this.updateStrategy = dbConfig.getUpdateStrategy();

+ 29 - 31
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/TableInfoHelper.java

@@ -37,12 +37,10 @@ import org.apache.ibatis.type.UnknownTypeHandler;
 
 import java.lang.reflect.Field;
 import java.util.ArrayList;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.stream.Collectors;
 
 import static java.util.stream.Collectors.toList;
 
@@ -399,7 +397,6 @@ public class TableInfoHelper {
      * @param dbConfig  数据库全局配置
      * @param tableInfo 表信息
      * @param fieldList 字段列表
-     * @param clazz     当前表对象类
      * @return true 继续下一个属性判断,返回 continue;
      */
     private static boolean initTableFieldWithAnnotation(GlobalConfig.DbConfig dbConfig, TableInfo tableInfo,
@@ -409,45 +406,46 @@ public class TableInfoHelper {
         if (null == tableField) {
             return false;
         }
+        JdbcType jdbcType = tableField.jdbcType();
+        Class<? extends TypeHandler<?>> typeHandler = tableField.typeHandler();
+        if (JdbcType.UNDEFINED != jdbcType || UnknownTypeHandler.class != typeHandler) {
+            // todo 暂时先这么搞,后面再优化
+            fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field, tableField));
+            return true;
+        }
         String columnName = field.getName();
         boolean columnNameFromTableField = false;
         if (StringUtils.isNotEmpty(tableField.value())) {
             columnName = tableField.value();
             columnNameFromTableField = true;
         }
-        JdbcType jdbcType = tableField.jdbcType();
-        Class<? extends TypeHandler<?>> typeHandler = tableField.typeHandler();
-        //#{property,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler}
-        Map<String,String> els = new LinkedHashMap<>();
-        //兼容下旧代码.
+        /*
+         * el 语法支持,可以传入多个参数以逗号分开
+         */
+        String el = field.getName();
         if (StringUtils.isNotEmpty(tableField.el())) {
-            String[] split = tableField.el().split(StringPool.COMMA);
-            for (String value : split) {
-                String[] arrays = value.split(StringPool.EQUALS);
-                if (arrays.length == 2) {
-                    els.put(arrays[0].trim(), arrays[1].trim());
-                }
-            }
-        }
-        if (JdbcType.UNDEFINED != jdbcType) {
-            els.put("jdbcType", jdbcType.name());
-        }
-        if (UnknownTypeHandler.class != typeHandler) {
-            els.put("typeHandler", typeHandler.getName());
+            el = tableField.el();
         }
+        String[] columns = columnName.split(StringPool.SEMICOLON);
+
         String columnFormat = dbConfig.getColumnFormat();
         if (StringUtils.isNotEmpty(columnFormat) && (!columnNameFromTableField || tableField.keepGlobalFormat())) {
-            columnName = String.format(columnFormat, columnName);
+            for (int i = 0; i < columns.length; i++) {
+                String column = columns[i];
+                column = String.format(columnFormat, column);
+                columns[i] = column;
+            }
+        }
+
+        String[] els = el.split(StringPool.SEMICOLON);
+        if (columns.length == els.length) {
+            for (int i = 0; i < columns.length; i++) {
+                fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field, columns[i], els[i], tableField));
+            }
+            return true;
         }
-        //合并el属性段
-        String elContent = els.entrySet()
-            .stream()
-            .map(entry -> entry.getKey() + StringPool.EQUALS + entry.getValue())
-            .collect(Collectors.joining(StringPool.COMMA));
-        //el属性 = 字段属性 + el属性段
-        String el = StringUtils.isEmpty(elContent) ? field.getName() : field.getName() + StringPool.COMMA + elContent;
-        fieldList.add(new TableFieldInfo(dbConfig, tableInfo, field, columnName, el, tableField));
-        return true;
+        throw ExceptionUtils.mpe("Class: %s, Field: %s, 'value' 'el' Length must be consistent.",
+            clazz.getName(), field.getName());
     }
 
     /**