Просмотр исходного кода

根据字段策略来判断实体是否真正为空

Caratacus 6 лет назад
Родитель
Сommit
b828752a8b

+ 42 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/Wrapper.java

@@ -17,9 +17,14 @@ package com.baomidou.mybatisplus.core.conditions;
 
 import java.util.Objects;
 
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
 import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
 
 /**
@@ -67,6 +72,7 @@ public abstract class Wrapper<T> implements ISqlSegment {
     public boolean isEmptyOfWhere() {
         return isEmptyOfNormal() && isEmptyOfEntity();
     }
+
     /**
      * 查询条件不为空(包含entity)
      */
@@ -80,6 +86,7 @@ public abstract class Wrapper<T> implements ISqlSegment {
     public boolean isEmptyOfNormal() {
         return CollectionUtils.isEmpty(getExpression().getNormal());
     }
+
     /**
      * 查询条件为空(不包含entity)
      */
@@ -94,9 +101,42 @@ public abstract class Wrapper<T> implements ISqlSegment {
      */
     public boolean nonEmptyOfEntity() {
         T entity = getEntity();
-        return Objects.nonNull(getEntity()) && TableInfoHelper.getTableInfo(entity.getClass()).getFieldList().stream()
-            .anyMatch(e -> Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty())));
+        Class<?> entityClass = entity.getClass();
+        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
+        return Objects.nonNull(getEntity()) && tableInfo.getFieldList().stream().anyMatch(e -> fieldStrategyMatch(entity, tableInfo, e, true));
+
+    }
+
+    /**
+     * 根据实体FieldStrategy属性来决定判断逻辑
+     *
+     * @param entity
+     * @param tableInfo
+     * @param e
+     * @param isFirst
+     * @return
+     * @see FieldStrategy
+     */
+    private boolean fieldStrategyMatch(T entity, TableInfo tableInfo, TableFieldInfo e, boolean isFirst) {
+        FieldStrategy fieldStrategy = e.getFieldStrategy();
+        switch (fieldStrategy) {
+            case NOT_EMPTY:
+                return StringUtils.checkValNotNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
+            case NOT_NULL:
+                return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
+            case DEFAULT:
+                if (!isFirst) {
+                    throw new MybatisPlusException("Error: Recursive nesting Exception! ");
+                }
+                return fieldStrategyMatch(entity, tableInfo, e, false);
+            case IGNORED:
+                return true;
+            default:
+                return Objects.nonNull(ReflectionKit.getMethodValue(entity, e.getProperty()));
+
+        }
     }
+
     /**
      * 深层实体判断属性
      *