Browse Source

tableinfo 初始化 处理, 填充接口新增 3个 default 方法

miemie 5 years ago
parent
commit
ff5bd37d3f

+ 61 - 17
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/handlers/MetaObjectHandler.java

@@ -34,6 +34,20 @@ import java.util.Optional;
  */
 public interface MetaObjectHandler {
 
+    /**
+     * 是否开启了插入填充
+     */
+    default boolean openInsertFill() {
+        return true;
+    }
+
+    /**
+     * 是否开启了更新填充
+     */
+    default boolean openUpdateFill() {
+        return true;
+    }
+
     /**
      * 插入元对象字段填充(用于插入时对公共字段的填充)
      *
@@ -141,6 +155,52 @@ public interface MetaObjectHandler {
         return null;
     }
 
+    /**
+     * check if the entity has some property will fill for insert </p>
+     * 表字段是否启用了插入填充
+     *
+     * @param metaObject metaObject meta object parameter
+     * @return yes or no
+     * @since 3.2.1
+     */
+    default boolean isWithInsertFill(MetaObject metaObject) {
+        TableInfo tableInfo = findTableInfo(metaObject);
+        if (Objects.nonNull(tableInfo)) {
+            return tableInfo.isWithInsertFill();
+        }
+        return false;
+    }
+
+    /**
+     * check if the entity has some property will fill for update </p>
+     * 表字段是否启用了更新填充
+     *
+     * @param metaObject metaObject meta object parameter
+     * @return yes or no
+     * @since 3.2.1
+     */
+    default boolean isWithUpdateFill(MetaObject metaObject) {
+        TableInfo tableInfo = findTableInfo(metaObject);
+        if (Objects.nonNull(tableInfo)) {
+            return tableInfo.isWithUpdateFill();
+        }
+        return false;
+    }
+
+    /**
+     * find the tableInfo cache by metaObject </p>
+     * 获取 TableInfo 缓存
+     *
+     * @param metaObject meta object parameter
+     * @return TableInfo
+     * @since 3.2.1
+     */
+    default TableInfo findTableInfo(MetaObject metaObject) {
+        return metaObject.hasGetter(Constants.MP_OPTLOCK_ET_ORIGINAL) ?
+            TableInfoHelper.getTableInfo(metaObject.getValue(Constants.MP_OPTLOCK_ET_ORIGINAL).getClass())
+            : TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass());
+    }
+
     /**
      * 填充判断
      * <li> 如果是主键,不填充 </li>
@@ -157,9 +217,7 @@ public interface MetaObjectHandler {
      * @since 3.0.7
      */
     default boolean isFill(String fieldName, Object fieldVal, MetaObject metaObject, FieldFill fieldFill) {
-        TableInfo tableInfo = metaObject.hasGetter(Constants.MP_OPTLOCK_ET_ORIGINAL) ?
-            TableInfoHelper.getTableInfo(metaObject.getValue(Constants.MP_OPTLOCK_ET_ORIGINAL).getClass())
-            : TableInfoHelper.getTableInfo(metaObject.getOriginalObject().getClass());
+        TableInfo tableInfo = findTableInfo(metaObject);
         if (Objects.nonNull(tableInfo)) {
             Optional<TableFieldInfo> first = tableInfo.getFieldList().stream()
                 //v_3.1.1+ 设置子类的值也可以通过
@@ -172,18 +230,4 @@ public interface MetaObjectHandler {
         }
         return false;
     }
-
-    /**
-     * 是否开启了插入填充
-     */
-    default boolean openInsertFill() {
-        return true;
-    }
-
-    /**
-     * 是否开启了更新填充
-     */
-    default boolean openUpdateFill() {
-        return true;
-    }
 }

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

@@ -156,7 +156,7 @@ public class TableFieldInfo implements Constants {
             el += (COMMA + "numericScale=" + numericScale);
         }
         this.el = el;
-        tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
+        this.initLogicDelete(dbConfig, field);
 
         String column = tableField.value();
         if (StringUtils.isBlank(column)) {
@@ -232,7 +232,7 @@ public class TableFieldInfo implements Constants {
         this.insertStrategy = dbConfig.getInsertStrategy();
         this.updateStrategy = dbConfig.getUpdateStrategy();
         this.whereStrategy = dbConfig.getSelectStrategy();
-        tableInfo.setLogicDelete(this.initLogicDelete(dbConfig, field));
+        this.initLogicDelete(dbConfig, field);
 
         String column = field.getName();
         if (tableInfo.isUnderCamel()) {
@@ -268,7 +268,7 @@ public class TableFieldInfo implements Constants {
      * @param dbConfig 数据库全局配置
      * @param field    字段属性对象
      */
-    private boolean initLogicDelete(GlobalConfig.DbConfig dbConfig, Field field) {
+    private void initLogicDelete(GlobalConfig.DbConfig dbConfig, Field field) {
         /* 获取注解属性,逻辑处理字段 */
         TableLogic tableLogic = field.getAnnotation(TableLogic.class);
         if (null != tableLogic) {
@@ -282,23 +282,20 @@ public class TableFieldInfo implements Constants {
             } else {
                 this.logicDeleteValue = dbConfig.getLogicDeleteValue();
             }
-            return true;
         } else {
             String globalLogicDeleteField = dbConfig.getLogicDeleteField();
             if (StringUtils.isNotBlank(globalLogicDeleteField) && globalLogicDeleteField.equalsIgnoreCase(field.getName())) {
                 this.logicNotDeleteValue = dbConfig.getLogicNotDeleteValue();
                 this.logicDeleteValue = dbConfig.getLogicDeleteValue();
-                return true;
             }
         }
-        return false;
     }
 
     /**
-     * 是否注解了逻辑删除
+     * 是否启用了逻辑删除
      */
     public boolean isLogicDelete() {
-        return StringUtils.isNotBlank(logicDeleteValue);
+        return StringUtils.isNotBlank(logicDeleteValue) && StringUtils.isNotBlank(logicNotDeleteValue);
     }
 
     /**

+ 25 - 13
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -15,6 +15,7 @@
  */
 package com.baomidou.mybatisplus.core.metadata;
 
+import com.baomidou.mybatisplus.annotation.FieldFill;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.KeySequence;
 import com.baomidou.mybatisplus.core.MybatisConfiguration;
@@ -77,7 +78,7 @@ public class TableInfo implements Constants {
      * 主键是否有存在字段名与属性名关联
      * <p>true: 表示要进行 as</p>
      */
-    private boolean keyRelated = false;
+    private boolean keyRelated;
     /**
      * 表主键ID 字段名
      */
@@ -110,11 +111,12 @@ public class TableInfo implements Constants {
     /**
      * 是否开启逻辑删除
      */
-    private boolean logicDelete = false;
+    @Setter(AccessLevel.NONE)
+    private boolean logicDelete;
     /**
      * 是否开启下划线转驼峰
      */
-    private boolean underCamel = true;
+    private boolean underCamel;
     /**
      * 缓存包含主键及字段的 sql select
      */
@@ -127,6 +129,18 @@ public class TableInfo implements Constants {
     @Setter(AccessLevel.NONE)
     @Getter(AccessLevel.NONE)
     private String sqlSelect;
+    /**
+     * 表字段是否启用了插入填充
+     */
+    @Getter
+    @Setter(AccessLevel.NONE)
+    private boolean withInsertFill;
+    /**
+     * 表字段是否启用了更新填充
+     */
+    @Getter
+    @Setter(AccessLevel.NONE)
+    private boolean withUpdateFill;
 
     public TableInfo(Class<?> entityType) {
         this.entityType = entityType;
@@ -151,15 +165,6 @@ public class TableInfo implements Constants {
         this.underCamel = configuration.isMapUnderscoreToCamelCase();
     }
 
-    /**
-     * 设置逻辑删除
-     */
-    void setLogicDelete(boolean logicDelete) {
-        if (logicDelete) {
-            this.logicDelete = true;
-        }
-    }
-
     /**
      * 获取主键的 select sql 片段
      *
@@ -348,7 +353,7 @@ public class TableInfo implements Constants {
      * @param isWhere true: logicDeleteValue, false: logicNotDeleteValue
      * @return
      */
-    protected String formatLogicDeleteSql(TableFieldInfo field, boolean isWhere) {
+    private String formatLogicDeleteSql(TableFieldInfo field, boolean isWhere) {
         final String value = isWhere ? field.getLogicNotDeleteValue() : field.getLogicDeleteValue();
         if (isWhere) {
             if (NULL.equalsIgnoreCase(value)) {
@@ -385,4 +390,11 @@ public class TableInfo implements Constants {
             this.resultMap = id;
         }
     }
+
+    void setFieldList(List<TableFieldInfo> fieldList) {
+        this.fieldList = fieldList;
+        this.logicDelete = fieldList.parallelStream().anyMatch(TableFieldInfo::isLogicDelete);
+        this.withInsertFill = fieldList.parallelStream().anyMatch(i -> i.getFieldFill() == FieldFill.INSERT || i.getFieldFill() == FieldFill.INSERT_UPDATE);
+        this.withUpdateFill = fieldList.parallelStream().anyMatch(i -> i.getFieldFill() == FieldFill.UPDATE || i.getFieldFill() == FieldFill.INSERT_UPDATE);
+    }
 }