Forráskód Böngészése

修复去除`1=1`SQL拼接BUG

Caratacus 6 éve
szülő
commit
58ed3e9858

+ 30 - 9
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/Wrapper.java

@@ -15,13 +15,13 @@
  */
 package com.baomidou.mybatisplus.core.conditions;
 
+import java.util.Objects;
+
 import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
 
-import java.util.Objects;
-
 /**
  * <p>
  * 条件构造抽象类
@@ -62,10 +62,29 @@ public abstract class Wrapper<T> implements ISqlSegment {
     public abstract MergeSegments getExpression();
 
     /**
-     * 查询条件为空
+     * 查询条件为空(包含entity)
      */
     public boolean isEmptyOfWhere() {
-        return CollectionUtils.isEmpty(getExpression().getNormal()) && !nonEntityNull();
+        return isEmptyOfNormal() && isEmptyOfEntity();
+    }
+    /**
+     * 查询条件不为空(包含entity)
+     */
+    public boolean nonEmptyOfWhere() {
+        return !isEmptyOfWhere();
+    }
+
+    /**
+     * 查询条件为空(不包含entity)
+     */
+    public boolean isEmptyOfNormal() {
+        return CollectionUtils.isEmpty(getExpression().getNormal());
+    }
+    /**
+     * 查询条件为空(不包含entity)
+     */
+    public boolean nonEmptyOfNormal() {
+        return !isEmptyOfNormal();
     }
 
     /**
@@ -73,17 +92,19 @@ public abstract class Wrapper<T> implements ISqlSegment {
      *
      * @return true 不为空
      */
-    private boolean nonEntityNull() {
+    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())));
     }
-
     /**
-     * 查询条件不为空
+     * 深层实体判断属性
+     *
+     * @return true 为空
      */
-    public boolean notEmptyOfWhere() {
-        return !isEmptyOfWhere();
+    public boolean isEmptyOfEntity() {
+        return !nonEmptyOfEntity();
     }
+
 }
 

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -193,7 +193,7 @@ public abstract class AbstractMethod {
         sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", Constants.WRAPPER_ENTITY), true);
         sqlScript += StringPool.NEWLINE;
         sqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", Constants.WRAPPER_SQLSEGMENT),
-            String.format("%s != null and %s != '' and ew.notEmptyOfWhere", Constants.WRAPPER_SQLSEGMENT, Constants.WRAPPER_SQLSEGMENT),
+            String.format("%s != null and %s != '' and ew.nonEmptyOfWhere", Constants.WRAPPER_SQLSEGMENT, Constants.WRAPPER_SQLSEGMENT),
             true);
         sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND|OR", null);
         sqlScript += StringPool.NEWLINE;

+ 6 - 5
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/AbstractLogicMethod.java

@@ -57,13 +57,14 @@ public abstract class AbstractLogicMethod extends AbstractMethod {
                 true);
             sqlScript += (StringPool.NEWLINE + table.getLogicDeleteSql(true, false) +
                 StringPool.NEWLINE);
-            sqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", Constants.WRAPPER_SQLSEGMENT),
-                String.format("%s != null and %s != '' and ew.notEmptyOfWhere", Constants.WRAPPER_SQLSEGMENT,
+            String normalsqlScript = SqlScriptUtils.convertIf(String.format("AND ${%s}", Constants.WRAPPER_SQLSEGMENT),
+                String.format("%s != null and %s != '' and ew.nonEmptyOfNormal", Constants.WRAPPER_SQLSEGMENT,
                     Constants.WRAPPER_SQLSEGMENT), true);
-            sqlScript += StringPool.NEWLINE;
-            sqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", Constants.WRAPPER_SQLSEGMENT),
-                String.format("%s != null and %s != '' and ew.emptyOfWhere", Constants.WRAPPER_SQLSEGMENT,
+            normalsqlScript += StringPool.NEWLINE;
+            normalsqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", Constants.WRAPPER_SQLSEGMENT),
+                String.format("%s != null and %s != '' and ew.emptyOfNormal", Constants.WRAPPER_SQLSEGMENT,
                     Constants.WRAPPER_SQLSEGMENT), true);
+            sqlScript += normalsqlScript;
             sqlScript = SqlScriptUtils.convertChoose("ew!=null", sqlScript,
                 table.getLogicDeleteSql(true, false));
             sqlScript = SqlScriptUtils.convertTrim(sqlScript, "WHERE", null, "AND|OR",

+ 1 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2Delete1Eq1Test.java

@@ -47,10 +47,10 @@ public class H2Delete1Eq1Test extends BaseTest {
         for (long i = 0; i < 10L; i++) {
             defaultMapper.insert(new H2Student(i, "Tom长大了", 1));
         }
+        log(logicDeleteMapper.selectList(new QueryWrapper<>(h2User).eq("name","2").orderByAsc("name")));
         log(defaultMapper.selectList(new QueryWrapper<H2Student>().orderByAsc("id")));
         log(defaultMapper.selectOne(new QueryWrapper<H2Student>().last("limit 1")));
 
-
         H2Student h2Student = new H2Student();
         h2Student.setId(1L);
         h2Student.setAge(2);

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/config/DBConfig.java

@@ -1,13 +1,13 @@
 package com.baomidou.mybatisplus.test.mysql.config;
 
+import javax.sql.DataSource;
+
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 import org.springframework.jdbc.datasource.SimpleDriverDataSource;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 
-import javax.sql.DataSource;
-
 /**
  * @author miemie
  * @since 2018/6/7