Selaa lähdekoodia

新增一个正常嵌套

miemie 7 vuotta sitten
vanhempi
commit
9986d0011c

+ 12 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java

@@ -42,8 +42,6 @@ import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
  */
 public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, This>> extends Wrapper<T>
     implements Compare<This, R>, Nested<This>, Join<This>, Func<This, R> {
-    @SuppressWarnings("unchecked")
-    protected final This typedThis = (This) this;
     /**
      * 前缀
      */
@@ -54,6 +52,8 @@ public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, T
      */
     private static final String PLACE_HOLDER = "{%s}";
     private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}";
+    @SuppressWarnings("unchecked")
+    protected final This typedThis = (This) this;
     /**
      * 必要度量
      */
@@ -192,7 +192,7 @@ public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, T
      * AND 嵌套
      */
     @Override
-    public This and(boolean condition, Function<This, This> func) {
+    public This andNested(boolean condition, Function<This, This> func) {
         return and(condition).addNestedCondition(condition, func);
     }
 
@@ -200,10 +200,18 @@ public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, T
      * OR 嵌套
      */
     @Override
-    public This or(boolean condition, Function<This, This> func) {
+    public This orNested(boolean condition, Function<This, This> func) {
         return or(condition).addNestedCondition(condition, func);
     }
 
+    /**
+     * 正常嵌套 不带 AND 或者 OR
+     */
+    @Override
+    public This nested(boolean condition, Function<This, This> func) {
+        return addNestedCondition(condition, func);
+    }
+
     /**
      * 拼接 AND
      */

+ 18 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/interfaces/Nested.java

@@ -32,24 +32,36 @@ public interface Nested<This> extends Serializable {
     /**
      * AND 嵌套
      */
-    default This and(Function<This, This> func) {
-        return and(true, func);
+    default This andNested(Function<This, This> func) {
+        return andNested(true, func);
     }
 
     /**
      * AND 嵌套
      */
-    This and(boolean condition, Function<This, This> func);
+    This andNested(boolean condition, Function<This, This> func);
 
     /**
      * OR 嵌套
      */
-    default This or(Function<This, This> func) {
-        return or(true, func);
+    default This orNested(Function<This, This> func) {
+        return orNested(true, func);
     }
 
     /**
      * OR 嵌套
      */
-    This or(boolean condition, Function<This, This> func);
+    This orNested(boolean condition, Function<This, This> func);
+
+    /**
+     * 正常嵌套 不带 AND 或者 OR
+     */
+    default This nested(Function<This, This> func) {
+        return nested(true, func);
+    }
+
+    /**
+     * 正常嵌套 不带 AND 或者 OR
+     */
+    This nested(boolean condition, Function<This, This> func);
 }

+ 2 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/query/LambdaQueryWrapper.java

@@ -65,7 +65,8 @@ public class LambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, LambdaQueryW
      * @param columns 查询字段
      * @return
      */
-    public LambdaQueryWrapper<T> select(Property<T, ?>... columns) {
+    @SafeVarargs
+    public final LambdaQueryWrapper<T> select(Property<T, ?>... columns) {
         for (Property<T, ?> column : columns) {
             sqlSelect.add(this.columnToString(column));
         }

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

@@ -15,7 +15,7 @@ public class WrapperTest {
     @Test
     public void test() {
         Wrapper<User> wrapper = new QueryWrapper<User>().lambda().eq(User::getName, 123)
-            .or(c -> c.eq(User::getRoleId, 1).eq(User::getId, 2))
+            .orNested(c -> c.eq(User::getRoleId, 1).eq(User::getId, 2))
             .and().eq(User::getId, 1);
         log(wrapper.getSqlSegment());
 
@@ -25,7 +25,7 @@ public class WrapperTest {
     public void test1() {
         QueryWrapper<User> ew = new QueryWrapper<User>()
             .eq("xxx", 123)
-            .and(i -> i.eq("andx", 65444).and().le("ande", 66666))
+            .andNested(i -> i.eq("andx", 65444).and().le("ande", 66666))
             .and().ne("xxx", 222);
         log(ew.getSqlSegment());
         ew.getParamNameValuePairs().forEach((k, v) -> System.out.println("key = " + k + " ; value = " + v));
@@ -36,7 +36,7 @@ public class WrapperTest {
         UpdateWrapper<User> ew = new UpdateWrapper<User>()
             .set("name", "三毛").set("id", 1)
             .eq("xxx", 123)
-            .and(i -> i.eq("andx", 65444).and().le("ande", 66666))
+            .andNested(i -> i.eq("andx", 65444).and().le("ande", 66666))
             .and().ne("xxx", 222);
         log(ew.getSqlSet());
         log(ew.getSqlSegment());

+ 10 - 3
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -36,15 +36,22 @@ public class MysqlTestDataMapperTest {
         println(testDataMapper.selectList(new QueryWrapper<TestData>()
             .eq("id", 1L)
             .and()
-            .like("test_str", 1)));
+            .like("test_str", 1)
+            .and()
+            .between("test_double", 1L, 2L)));
     }
 
     @Test
     public void specialSelectList() {
-        println(testDataMapper.selectList(new QueryWrapper<TestData>().lambda()
+        println(testDataMapper.selectList(new QueryWrapper<TestData>()
+            .lambda()
+            .select(TestData::getId)
             .eq(TestData::getId, 1L)
+            .andNested(i -> i.between(TestData::getTestDouble, 1L, 2L))
+            .and()
+            .eq(TestData::getTestInt, 1)
             .and()
-            .eq(TestData::getTestInt, 1)));
+            .between(TestData::getTestDouble, 1L, 2L)));
     }
 
     private void println(List<TestData> list) {