Kaynağa Gözat

增加 eqSql 方法。 (#6029)

Co-authored-by: Muyangmin <yangmin@nineeyestech.com>
Muyangmin 1 yıl önce
ebeveyn
işleme
4162363813

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

@@ -309,6 +309,12 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
         return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN, inExpression(values)));
     }
 
+    @Override
+    public Children eqSql(boolean condition, R column, String eqValue) {
+        return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), EQ,
+            () -> String.format("(%s)", eqValue)));
+    }
+
     @Override
     public Children inSql(boolean condition, R column, String inValue) {
         return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IN,

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

@@ -189,6 +189,33 @@ public interface Func<Children, R> extends Serializable {
      */
     Children notIn(boolean condition, R column, Object... values);
 
+    /**
+     * 字段 EQ ( sql语句 )
+     * <p>!! sql 注入方式的 eq 方法 !!</p>
+     * <p>例1: eqSql("id", "1")</p>
+     * <p>例2: eqSql("id", "select MAX(id) from table")</p>
+     *
+     * @param column    字段
+     * @param sql   sql语句
+     * @return children
+     */
+    default Children eqSql(R column, String sql) {
+        return eqSql(true, column, sql);
+    }
+
+    /**
+     * 字段 EQ ( sql语句 )
+     * <p>!! sql 注入方式的 eq 方法 !!</p>
+     * <p>例1: eqSql("id", "1")</p>
+     * <p>例2: eqSql("id", "select MAX(id) from table")</p>
+     *
+     * @param condition 执行条件
+     * @param column    字段
+     * @param sql   sql语句
+     * @return children
+     */
+    Children eqSql(boolean condition, R column, String sql);
+
     /**
      * 字段 IN ( sql语句 )
      * <p>!! sql 注入方式的 in 方法 !!</p>

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

@@ -107,12 +107,13 @@ class QueryWrapperTest extends BaseWrapperTest {
             .groupBy("id").groupBy("name", "id2", "name2")
             .in("inColl", getList()).or().notIn("notInColl", getList())
             .in("inArray").notIn("notInArray", 5, 6, 7)
+            .eqSql("eqSql", "1")
             .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) 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");
+        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 eqSql = (1) 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);
     }
 

+ 6 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/conditions/AbstractChainWrapper.java

@@ -201,6 +201,12 @@ public abstract class AbstractChainWrapper<T, R, Children extends AbstractChainW
         return typedThis;
     }
 
+    @Override
+    public Children eqSql(boolean condition, R column, String eqValue) {
+        getWrapper().eqSql(condition, column, eqValue);
+        return typedThis;
+    }
+
     @Override
     public Children inSql(boolean condition, R column, String inValue) {
         getWrapper().inSql(condition, column, inValue);