Browse Source

修正一波bug,以及去除最后一位的or

miemie 7 years ago
parent
commit
78b5078acb

+ 0 - 10
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java

@@ -214,16 +214,6 @@ public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, T
         return doIt(condition, OR);
     }
 
-    @Override
-    public This in(boolean condition, String sql) {
-        return addNestedCondition(condition, sql, IN);
-    }
-
-    @Override
-    public This notIn(boolean condition, String sql) {
-        return not(condition).in(condition, sql);
-    }
-
     @Override
     public This apply(boolean condition, String applySql) {
         return doIt(condition, () -> applySql);

+ 37 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/interfaces/Func.java

@@ -15,11 +15,13 @@
  */
 package com.baomidou.mybatisplus.core.conditions.interfaces;
 
+import static java.util.stream.Collectors.toList;
+
 import java.io.Serializable;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Optional;
-import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -81,7 +83,7 @@ public interface Func<This, R> extends Serializable {
      */
     default This in(boolean condition, R column, Object... values) {
         return in(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
-            .collect(Collectors.toList()));
+            .collect(toList()));
     }
 
     /**
@@ -108,7 +110,39 @@ public interface Func<This, R> extends Serializable {
      */
     default This notIn(boolean condition, R column, Object... values) {
         return notIn(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
-            .collect(Collectors.toList()));
+            .collect(toList()));
+    }
+
+    /**
+     * 拼接 IN ( sql 语句 )
+     * 例: in("id", "1,2,3,4,5,6")
+     */
+    default This in(R column, String inValue) {
+        return in(true, column, inValue);
+    }
+
+    /**
+     * 拼接 IN ( sql 语句 )
+     * 例: in("id", "1,2,3,4,5,6")
+     */
+    default This in(boolean condition, R column, String inValue) {
+        return in(condition, column, Collections.singleton(inValue));
+    }
+
+    /**
+     * 拼接 NOT IN ( sql 语句 )
+     * 例: notIn("id", "1,2,3,4,5,6")
+     */
+    default This notIn(R column, String inValue) {
+        return notIn(true, column, inValue);
+    }
+
+    /**
+     * 拼接 NOT IN ( sql 语句 )
+     * 例: notIn("id", "1,2,3,4,5,6")
+     */
+    default This notIn(boolean condition, R column, String inValue) {
+        return notIn(condition, column, Collections.singleton(inValue));
     }
 
     /**

+ 0 - 28
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/interfaces/Join.java

@@ -40,34 +40,6 @@ public interface Join<This> extends Serializable {
      */
     This or(boolean condition);
 
-    /**
-     * 拼接 IN ( sql 语句 )
-     * 例: in("1,2,3,4,5,6")
-     */
-    default This in(String sql) {
-        return in(true, sql);
-    }
-
-    /**
-     * 拼接 IN ( sql 语句 )
-     * 例: in("1,2,3,4,5,6")
-     */
-    This in(boolean condition, String sql);
-
-    /**
-     * 拼接 NOT IN ( sql 语句 )
-     * 例: notIn("1,2,3,4,5,6")
-     */
-    default This notIn(String sql) {
-        return notIn(true, sql);
-    }
-
-    /**
-     * 拼接 NOT IN ( sql 语句 )
-     * 例: notIn("1,2,3,4,5,6")
-     */
-    This notIn(boolean condition, String sql);
-
     /**
      * 拼接 sql
      * 例: apply("date_format(column,'%Y-%m-%d') = '2008-08-08'")

+ 16 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/segments/NormalSegmentList.java

@@ -38,6 +38,7 @@ public class NormalSegmentList extends ArrayList<ISqlSegment> implements ISqlSeg
      * 最后一个值
      */
     private ISqlSegment lastValue = null;
+    private boolean executeNot = true;
 
     @Override
     public boolean addAll(Collection<? extends ISqlSegment> c) {
@@ -66,9 +67,18 @@ public class NormalSegmentList extends ArrayList<ISqlSegment> implements ISqlSeg
                         removeLast();
                     }
                 }
+            } else {
+                executeNot = false;
+                return false;
+            }
+        } else {
+            if (!executeNot) {
+                list.add(1, SqlKeyword.NOT);
+                executeNot = true;
+            }
+            if (!MatchSegment.AND_OR.match(lastValue) && !isEmpty()) {
+                add(SqlKeyword.AND);
             }
-        } else if (!MatchSegment.AND_OR.match(lastValue) && !isEmpty()) {
-            add(SqlKeyword.AND);
         }
         //后置处理
         this.flushLastValue(list);
@@ -79,12 +89,15 @@ public class NormalSegmentList extends ArrayList<ISqlSegment> implements ISqlSeg
         lastValue = list.get(list.size() - 1);
     }
 
-    private void removeLast() {//todo
+    private void removeLast() {
         remove(size() - 1);
     }
 
     @Override
     public String getSqlSegment() {
+        if (MatchSegment.AND_OR.match(lastValue)) {
+            removeLast();
+        }
         return this.stream().map(ISqlSegment::getSqlSegment).collect(Collectors.joining(" "));
     }
 }

+ 11 - 8
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/WrapperTest.java

@@ -73,28 +73,28 @@ public class WrapperTest {
 
     @Test
     public void testQueryWrapper() {
-        logSqlSegment("去除第一个 or,以及自动拼接 and,以及手动拼接 or", new QueryWrapper<User>().or()
-            .ge("age", 3).or().ge("age", 3).ge("age", 3));
+        logSqlSegment("去除第一个 or,以及自动拼接 and,以及手动拼接 or,以及去除最后的多个or", new QueryWrapper<User>().or()
+            .ge("age", 3).or().ge("age", 3).ge("age", 3).or().or().or().or());
 
         logSqlSegment("多个 or 相连接,去除多余的 or", new QueryWrapper<User>()
             .ge("age", 3).or().or().or().ge("age", 3).or().or().ge("age", 3));
 
-        logSqlSegment("嵌套测试,正常嵌套", new QueryWrapper<User>()
+        logSqlSegment("嵌套,正常嵌套", new QueryWrapper<User>()
             .nested(i -> i.eq("id", 1)).eq("id", 1));
 
-        logSqlSegment("嵌套测试,第一个套外的 and 自动消除", new QueryWrapper<User>()
+        logSqlSegment("嵌套,第一个套外的 and 自动消除", new QueryWrapper<User>()
             .and(i -> i.eq("id", 1)).eq("id", 1));
 
-        logSqlSegment("嵌套测试,多层嵌套", new QueryWrapper<User>()
+        logSqlSegment("嵌套,多层嵌套", new QueryWrapper<User>()
             .and(i -> i.eq("id", 1).and(j -> j.eq("id", 1))));
 
-        logSqlSegment("嵌套测试,第一个套外的 or 自动消除", new QueryWrapper<User>()
+        logSqlSegment("嵌套,第一个套外的 or 自动消除", new QueryWrapper<User>()
             .or(i -> i.eq("id", 1)).eq("id", 1));
 
-        logSqlSegment("嵌套测试,套内外自动拼接 and", new QueryWrapper<User>()
+        logSqlSegment("嵌套,套内外自动拼接 and", new QueryWrapper<User>()
             .eq("id", 11).and(i -> i.eq("id", 1)).eq("id", 1));
 
-        logSqlSegment("嵌套测试,套内外手动拼接 or,去除套内第一个 or", new QueryWrapper<User>()
+        logSqlSegment("嵌套,套内外手动拼接 or,去除套内第一个 or", new QueryWrapper<User>()
             .eq("id", 11).or(i -> i.or().eq("id", 1)).or().eq("id", 1));
 
         logSqlSegment("多个 order by 和 group by 拼接,自动优化顺序,last方法拼接在最后", new QueryWrapper<User>()
@@ -108,6 +108,9 @@ public class WrapperTest {
 
         logSqlSegment("只存在 group by", new QueryWrapper<User>()
             .groupBy("id", "name", "sex").groupBy("id", "name"));
+
+        logSqlSegment("not... 自动拼接 and,手动拼接 or", new QueryWrapper<User>()
+            .notBetween("id", 1, 6).or().notIn("id", "1,2,3,4,5,6"));
     }
 
 //    public void test() {