浏览代码

feat: rich wrapper interface

119431682@qq.com 3 年之前
父节点
当前提交
0452878559

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

@@ -300,6 +300,30 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
             () -> String.format("(%s)", inValue)));
             () -> String.format("(%s)", inValue)));
     }
     }
 
 
+    @Override
+    public Children gtSql(boolean condition, R column, String inValue) {
+        return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), GT,
+            () -> String.format("(%s)", inValue)));
+    }
+
+    @Override
+    public Children geSql(boolean condition, R column, String inValue) {
+        return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), GE,
+            () -> String.format("(%s)", inValue)));
+    }
+
+    @Override
+    public Children ltSql(boolean condition, R column, String inValue) {
+        return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), LT,
+            () -> String.format("(%s)", inValue)));
+    }
+
+    @Override
+    public Children leSql(boolean condition, R column, String inValue) {
+        return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), LE,
+            () -> String.format("(%s)", inValue)));
+    }
+
     @Override
     @Override
     public Children notInSql(boolean condition, R column, String inValue) {
     public Children notInSql(boolean condition, R column, String inValue) {
         return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN,
         return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN,

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

@@ -161,6 +161,82 @@ public interface Func<Children, R> extends Serializable {
      */
      */
     Children inSql(boolean condition, R column, String inValue);
     Children inSql(boolean condition, R column, String inValue);
 
 
+    /**
+     * 字段 &gt; ( sql语句 )
+     * <p>例1: gtSql("id", "1, 2, 3, 4, 5, 6")</p>
+     * <p>例1: gtSql("id", "select id from table where name = 'JunJun'")</p>
+     *
+     * @param condition
+     * @param column
+     * @param inValue
+     * @return
+     */
+    Children gtSql(boolean condition, R column, String inValue);
+
+    /**
+     * ignore
+     */
+    default Children gtSql(R column, String inValue) {
+        return gtSql(true, column, inValue);
+    }
+
+    /**
+     * 字段 >= ( sql语句 )
+     * <p>例1: geSql("id", "1, 2, 3, 4, 5, 6")</p>
+     * <p>例1: geSql("id", "select id from table where name = 'JunJun'")</p>
+     *
+     * @param condition
+     * @param column
+     * @param inValue
+     * @return
+     */
+    Children geSql(boolean condition, R column, String inValue);
+
+    /**
+     * ignore
+     */
+    default Children geSql(R column, String inValue) {
+        return geSql(true, column, inValue);
+    }
+
+    /**
+     * 字段 &lt; ( sql语句 )
+     * <p>例1: ltSql("id", "1, 2, 3, 4, 5, 6")</p>
+     * <p>例1: ltSql("id", "select id from table where name = 'JunJun'")</p>
+     *
+     * @param condition
+     * @param column
+     * @param inValue
+     * @return
+     */
+    Children ltSql(boolean condition, R column, String inValue);
+
+    /**
+     * ignore
+     */
+    default Children ltSql(R column, String inValue) {
+        return ltSql(true, column, inValue);
+    }
+
+    /**
+     * 字段 <= ( sql语句 )
+     * <p>例1: leSql("id", "1, 2, 3, 4, 5, 6")</p>
+     * <p>例1: leSql("id", "select id from table where name = 'JunJun'")</p>
+     *
+     * @param condition
+     * @param column
+     * @param inValue
+     * @return
+     */
+    Children leSql(boolean condition, R column, String inValue);
+
+    /**
+     * ignore
+     */
+    default Children leSql(R column, String inValue) {
+        return leSql(true, column, inValue);
+    }
+
     /**
     /**
      * ignore
      * ignore
      */
      */
@@ -181,7 +257,6 @@ public interface Func<Children, R> extends Serializable {
      */
      */
     Children notInSql(boolean condition, R column, String inValue);
     Children notInSql(boolean condition, R column, String inValue);
 
 
-
     /**
     /**
      * 分组:GROUP BY 字段, ...
      * 分组:GROUP BY 字段, ...
      * <p>例: groupBy("id")</p>
      * <p>例: groupBy("id")</p>
@@ -196,7 +271,6 @@ public interface Func<Children, R> extends Serializable {
         return groupBy(true, column);
         return groupBy(true, column);
     }
     }
 
 
-
     /**
     /**
      * 分组:GROUP BY 字段, ...
      * 分组:GROUP BY 字段, ...
      * <p>例: groupBy(Arrays.asList("id", "name"))</p>
      * <p>例: groupBy(Arrays.asList("id", "name"))</p>
@@ -269,7 +343,6 @@ public interface Func<Children, R> extends Serializable {
         return orderBy(condition, true, column, columns);
         return orderBy(condition, true, column, columns);
     }
     }
 
 
-
     /**
     /**
      * 排序:ORDER BY 字段, ... DESC
      * 排序:ORDER BY 字段, ... DESC
      * <p>例: orderByDesc(true, "id")</p>
      * <p>例: orderByDesc(true, "id")</p>

+ 3 - 1
mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/conditions/QueryWrapperTest.java

@@ -106,9 +106,11 @@ class QueryWrapperTest extends BaseWrapperTest {
             .in("inColl", getList()).or().notIn("notInColl", getList())
             .in("inColl", getList()).or().notIn("notInColl", getList())
             .in("inArray").notIn("notInArray", 5, 6, 7)
             .in("inArray").notIn("notInArray", 5, 6, 7)
             .inSql("inSql", "1,2,3,4,5").notInSql("inSql", "1,2,3,4,5")
             .inSql("inSql", "1,2,3,4,5").notInSql("inSql", "1,2,3,4,5")
+            .gtSql("gtSql", "1,2,3,4,5").ltSql("ltSql", "1,2,3,4,5")
+            .geSql("geSql", "1,2,3,4,5").leSql("leSql", "1,2,3,4,5")
             .having("sum(age) > {0}", 1).having("id is not null")
             .having("sum(age) > {0}", 1).having("id is not null")
             .func(entity.getId() != null, j -> j.eq("id", entity.getId()));// 不会npe,也不会加入sql
             .func(entity.getId() != null, j -> j.eq("id", entity.getId()));// 不会npe,也不会加入sql
-        logSqlWhere("测试 Func 下的方法", queryWrapper, "(nullColumn IS NULL OR notNullColumn IS NOT NULL AND inColl IN (?,?) OR notInColl NOT IN (?,?) AND inArray IN () AND notInArray NOT IN (?,?,?) AND inSql IN (1,2,3,4,5) AND inSql NOT IN (1,2,3,4,5)) GROUP BY id,name,id2,name2 HAVING sum(age) > ? AND id is not null ORDER BY id ASC,name DESC,name2 DESC");
+        logSqlWhere("测试 Func 下的方法", queryWrapper, "(nullColumn IS NULL OR notNullColumn IS NOT NULL AND inColl IN (?,?) OR notInColl NOT IN (?,?) AND inArray IN () AND notInArray NOT IN (?,?,?) AND inSql IN (1,2,3,4,5) AND inSql NOT IN (1,2,3,4,5) AND gtSql > (1,2,3,4,5) AND ltSql < (1,2,3,4,5) AND geSql >= (1,2,3,4,5) AND leSql <= (1,2,3,4,5)) GROUP BY id,name,id2,name2 HAVING sum(age) > ? AND id is not null ORDER BY id ASC,name DESC,name2 DESC");
         logParams(queryWrapper);
         logParams(queryWrapper);
     }
     }
 
 

+ 25 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/conditions/AbstractChainWrapper.java

@@ -38,7 +38,7 @@ import java.util.function.Consumer;
  * @author miemie
  * @author miemie
  * @since 2018-12-19
  * @since 2018-12-19
  */
  */
-@SuppressWarnings({"serial", "unchecked"})
+@SuppressWarnings({"unchecked"})
 public abstract class AbstractChainWrapper<T, R, Children extends AbstractChainWrapper<T, R, Children, Param>, Param extends AbstractWrapper<T, R, Param>>
 public abstract class AbstractChainWrapper<T, R, Children extends AbstractChainWrapper<T, R, Children, Param>, Param extends AbstractWrapper<T, R, Param>>
     extends Wrapper<T> implements Compare<Children, R>, Func<Children, R>, Join<Children>, Nested<Param, Children> {
     extends Wrapper<T> implements Compare<Children, R>, Func<Children, R>, Join<Children>, Nested<Param, Children> {
 
 
@@ -194,6 +194,30 @@ public abstract class AbstractChainWrapper<T, R, Children extends AbstractChainW
         return typedThis;
         return typedThis;
     }
     }
 
 
+    @Override
+    public Children gtSql(boolean condition, R column, String inValue) {
+        getWrapper().gtSql(condition, column, inValue);
+        return typedThis;
+    }
+
+    @Override
+    public Children geSql(boolean condition, R column, String inValue) {
+        getWrapper().geSql(condition, column, inValue);
+        return typedThis;
+    }
+
+    @Override
+    public Children ltSql(boolean condition, R column, String inValue) {
+        getWrapper().ltSql(condition, column, inValue);
+        return typedThis;
+    }
+
+    @Override
+    public Children leSql(boolean condition, R column, String inValue) {
+        getWrapper().leSql(condition, column, inValue);
+        return typedThis;
+    }
+
     @Override
     @Override
     public Children notInSql(boolean condition, R column, String inValue) {
     public Children notInSql(boolean condition, R column, String inValue) {
         getWrapper().notInSql(condition, column, inValue);
         getWrapper().notInSql(condition, column, inValue);