Browse Source

继续补全注释,删除 apply的两个方法中的其中一个

miemie 7 years ago
parent
commit
e6d4040923

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

@@ -215,11 +215,6 @@ public abstract class AbstractWrapper<T, R, This extends AbstractWrapper<T, R, T
         return doIt(condition, OR);
     }
 
-    @Override
-    public This apply(boolean condition, String applySql) {
-        return doIt(condition, APPLY, () -> applySql);
-    }
-
     @Override
     public This apply(boolean condition, String applySql, Object... value) {
         return doIt(condition, APPLY, () -> formatSql(applySql, value));

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

@@ -43,6 +43,10 @@ public interface Func<This, R> extends Serializable {
 
     /**
      * 字段 IS NULL
+     * 例: isNull("name")
+     *
+     * @param condition 执行条件
+     * @param column    字段
      */
     This isNull(boolean condition, R column);
 
@@ -55,6 +59,10 @@ public interface Func<This, R> extends Serializable {
 
     /**
      * 字段 IS NOT NULL
+     * 例: isNotNull("name")
+     *
+     * @param condition 执行条件
+     * @param column    字段
      */
     This isNotNull(boolean condition, R column);
 
@@ -67,6 +75,11 @@ public interface Func<This, R> extends Serializable {
 
     /**
      * 字段 IN (value.get(0), value.get(1), ...)
+     * 例: in("id", Arrays.asList(1,2,3,4,5))
+     *
+     * @param condition 执行条件
+     * @param column    字段
+     * @param value     数据集合
      */
     This in(boolean condition, R column, Collection<?> value);
 
@@ -79,6 +92,11 @@ public interface Func<This, R> extends Serializable {
 
     /**
      * 字段 IN (v0, v1, ...)
+     * 例: in("id", 1, 2, 3, 4, 5)
+     *
+     * @param condition 执行条件
+     * @param column    字段
+     * @param values    数据数组
      */
     default This in(boolean condition, R column, Object... values) {
         return in(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
@@ -94,6 +112,11 @@ public interface Func<This, R> extends Serializable {
 
     /**
      * 字段 NOT IN (value.get(0), value.get(1), ...)
+     * 例: notIn("id", Arrays.asList(1,2,3,4,5))
+     *
+     * @param condition 执行条件
+     * @param column    字段
+     * @param value     数据集合
      */
     This notIn(boolean condition, R column, Collection<?> value);
 
@@ -106,6 +129,11 @@ public interface Func<This, R> extends Serializable {
 
     /**
      * 字段 NOT IN (v0, v1, ...)
+     * 例: notIn("id", 1, 2, 3, 4, 5)
+     *
+     * @param condition 执行条件
+     * @param column    字段
+     * @param values    数据数组
      */
     default This notIn(boolean condition, R column, Object... values) {
         return notIn(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
@@ -120,12 +148,14 @@ public interface Func<This, R> extends Serializable {
     }
 
     /**
-     * <p>
+     * 字段 IN ( sql语句 )
      * !! sql 注入方式的 in 方法 !!
-     * </p>
-     * 拼接 IN ( sql 语句 )
-     * 例1: in("id", "1,2,3,4,5,6")
-     * 例2: in("id", "select id from table where id < 3")
+     * 例1: inSql("id", "1,2,3,4,5,6")
+     * 例2: inSql("id", "select id from table where id < 3")
+     *
+     * @param condition 执行条件
+     * @param column    字段
+     * @param inValue   sql语句
      */
     This inSql(boolean condition, R column, String inValue);
 
@@ -137,8 +167,14 @@ public interface Func<This, R> extends Serializable {
     }
 
     /**
-     * 拼接 NOT IN ( sql 语句 )
-     * 例: notIn("id", "1,2,3,4,5,6")
+     * 字段 NOT IN ( sql语句 )
+     * !! sql 注入方式的 not in 方法 !!
+     * 例1: notInSql("id", "1,2,3,4,5,6")
+     * 例2: notInSql("id", "select id from table where id < 3")
+     *
+     * @param condition 执行条件
+     * @param column    字段
+     * @param inValue   sql语句 ---> 1,2,3,4,5,6 或者 select id from table where id < 3
      */
     This notInSql(boolean condition, R column, String inValue);
 
@@ -150,12 +186,11 @@ public interface Func<This, R> extends Serializable {
     }
 
     /**
-     * <p>
      * 分组:GROUP BY 字段, ...
-     * </p>
+     * 例: groupBy("id", "name")
      *
      * @param condition 执行条件
-     * @param columns   分组字段【可多个】
+     * @param columns   字段数组
      */
     This groupBy(boolean condition, R... columns);
 
@@ -167,12 +202,11 @@ public interface Func<This, R> extends Serializable {
     }
 
     /**
-     * <p>
      * 排序:ORDER BY 字段, ... ASC
-     * </p>
+     * 例: orderByAsc("id", "name")
      *
      * @param condition 执行条件
-     * @param columns   排序字段【可多个】
+     * @param columns   字段数组
      */
     default This orderByAsc(boolean condition, R... columns) {
         return orderBy(condition, true, columns);
@@ -186,12 +220,11 @@ public interface Func<This, R> extends Serializable {
     }
 
     /**
-     * <p>
      * 排序:ORDER BY 字段, ... DESC
-     * </p>
+     * 例: orderByDesc("id", "name")
      *
      * @param condition 执行条件
-     * @param columns   排序字段【可多个】
+     * @param columns   字段数组
      */
     default This orderByDesc(boolean condition, R... columns) {
         return orderBy(condition, false, columns);
@@ -199,6 +232,11 @@ public interface Func<This, R> extends Serializable {
 
     /**
      * 排序:ORDER BY 字段, ...
+     * 例: orderBy(true, "id", "name")
+     *
+     * @param condition 执行条件
+     * @param isAsc     是否是 ASC 排序
+     * @param columns   字段数组
      */
     This orderBy(boolean condition, boolean isAsc, R... columns);
 
@@ -210,7 +248,13 @@ public interface Func<This, R> extends Serializable {
     }
 
     /**
-     * HAVING ( sql 语句 )
+     * HAVING ( sql语句 )
+     * 例1: having("sum(age) > 10")
+     * 例2: having("sum(age) > {0}", 10)
+     *
+     * @param condition 执行条件
+     * @param sqlHaving sql 语句
+     * @param params    参数数组
      */
     This having(boolean condition, String sqlHaving, Object... params);
 }

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

@@ -37,22 +37,11 @@ public interface Join<This> extends Serializable {
 
     /**
      * 拼接 OR
+     *
+     * @param condition 执行条件
      */
     This or(boolean condition);
 
-    /**
-     * ignore
-     */
-    default This apply(String applySql) {
-        return apply(true, applySql);
-    }
-
-    /**
-     * 拼接 sql
-     * 例: apply("date_format(column,'%Y-%m-%d') = '2008-08-08'")
-     */
-    This apply(boolean condition, String applySql);
-
     /**
      * ignore
      */
@@ -61,8 +50,13 @@ public interface Join<This> extends Serializable {
     }
 
     /**
+     * !! 会有 sql 注入风险 !!
      * 拼接 sql
-     * 例: apply("date_format(column,'%Y-%m-%d') = {0}", LocalDate.now())
+     * 例1: apply("id = 1")
+     * 例2: apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
+     * 例3: apply("date_format(dateColumn,'%Y-%m-%d') = {0}", LocalDate.now())
+     *
+     * @param condition 执行条件
      */
     This apply(boolean condition, String applySql, Object... value);
 
@@ -76,6 +70,10 @@ public interface Join<This> extends Serializable {
     /**
      * 无视优化规则直接拼接到 sql 的最后(有sql注入的风险,请谨慎使用)
      * 例: last("limit 1")
+     * 注意只能调用一次,多次调用以最后一次为准
+     *
+     * @param condition 执行条件
+     * @param lastSql   sql语句
      */
     This last(boolean condition, String lastSql);
 
@@ -87,8 +85,12 @@ public interface Join<This> extends Serializable {
     }
 
     /**
-     * EXISTS ( sql 语句 )
+     * !! sql 注入方法 !!
+     * 拼接 EXISTS ( sql语句 )
      * 例: exists("select id from table where age = 1")
+     *
+     * @param condition 执行条件
+     * @param existsSql sql语句
      */
     This exists(boolean condition, String existsSql);
 
@@ -100,8 +102,12 @@ public interface Join<This> extends Serializable {
     }
 
     /**
-     * NOT EXISTS ( sql 语句 )
+     * !! sql 注入方法 !!
+     * 拼接 NOT EXISTS ( sql语句 )
      * 例: notExists("select id from table where age = 1")
+     *
+     * @param condition    执行条件
+     * @param notExistsSql sql语句
      */
     This notExists(boolean condition, String notExistsSql);
 }

+ 9 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/mysql/MysqlTestDataMapperTest.java

@@ -109,8 +109,8 @@ public class MysqlTestDataMapperTest {
 //                .notIn("test_int", Arrays.asList(1, 2, 3)//ok
 //                .in("test_int", 1, 2, 3)//ok
 //                .notIn("test_int", 1, 2, 3)//ok
-//                .inSql("test_int", "1,2,3")//ok
-                .notInSql("test_int", "1,2,3")//ok
+                .inSql("test_int", "1,2,3")//ok
+                .notInSql("test_int", "2,3")//ok
         ));
     }
 
@@ -124,6 +124,13 @@ public class MysqlTestDataMapperTest {
         /* exists 连着用是可行的 */
     }
 
+    @Test
+    public void testApply() {
+        println(testDataMapper.selectList(new QueryWrapper<TestData>()
+            .apply("test_int = 1")
+        ));
+    }
+
     private void println(List<TestData> list) {
         list.forEach(System.out::println);
     }