Browse Source

Merge pull request #3772 from ContantLearn/feature-rich-wrapper

Feature rich wrapper
qmdx 3 years ago
parent
commit
652b0a63c0

+ 27 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java

@@ -42,7 +42,7 @@ import static java.util.stream.Collectors.joining;
  * @author hubin miemie HCL
  * @since 2017-05-26
  */
-@SuppressWarnings({"serial", "unchecked"})
+@SuppressWarnings({"unchecked"})
 public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T, R, Children>> extends Wrapper<T>
     implements Compare<Children, R>, Nested<Children, Children>, Join<Children>, Func<Children, R> {
 
@@ -300,6 +300,30 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
             () -> 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
     public Children notInSql(boolean condition, R column, String inValue) {
         return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN,
@@ -441,6 +465,7 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
      * @param params  参数
      * @return sql片段
      */
+    @SuppressWarnings("SameParameterValue")
     protected final String formatSqlMaybeWithParam(String sqlStr, String mapping, Object... params) {
         if (StringUtils.isBlank(sqlStr)) {
             // todo 何时会这样?
@@ -581,6 +606,7 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
      * @param paramAlias 参数别名
      * @return Children
      */
+    @SuppressWarnings("unused")
     public Children setParamAlias(String paramAlias) {
         Assert.notEmpty(paramAlias, "paramAlias can not be empty!");
         Assert.isEmpty(paramNameValuePairs, "Please call this method before working!");
@@ -616,7 +642,6 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
      *
      * @param columns 多字段
      */
-    @Deprecated
     protected String columnsToString(List<R> columns) {
         return columns.stream().map(this::columnToString).collect(joining(StringPool.COMMA));
     }

+ 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);
 
+    /**
+     * 字段 &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
      */
@@ -181,7 +257,6 @@ public interface Func<Children, R> extends Serializable {
      */
     Children notInSql(boolean condition, R column, String inValue);
 
-
     /**
      * 分组:GROUP BY 字段, ...
      * <p>例: groupBy("id")</p>
@@ -196,7 +271,6 @@ public interface Func<Children, R> extends Serializable {
         return groupBy(true, column);
     }
 
-
     /**
      * 分组:GROUP BY 字段, ...
      * <p>例: groupBy(Arrays.asList("id", "name"))</p>
@@ -269,7 +343,6 @@ public interface Func<Children, R> extends Serializable {
         return orderBy(condition, true, column, columns);
     }
 
-
     /**
      * 排序:ORDER BY 字段, ... DESC
      * <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("inArray").notIn("notInArray", 5, 6, 7)
             .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")
             .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);
     }
 

+ 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
  * @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>>
     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;
     }
 
+    @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
     public Children notInSql(boolean condition, R column, String inValue) {
         getWrapper().notInSql(condition, column, inValue);