Forráskód Böngészése

优化查询参数

zhou-hao 5 éve
szülő
commit
c39a449950

+ 17 - 5
hsweb-commons/hsweb-commons-api/src/main/java/org/hswebframework/web/api/crud/entity/QueryParamEntity.java

@@ -1,6 +1,8 @@
 package org.hswebframework.web.api.crud.entity;
 
 import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections.CollectionUtils;
 import org.hswebframework.ezorm.core.NestConditional;
 import org.hswebframework.ezorm.core.dsl.Query;
@@ -26,11 +28,13 @@ import java.util.function.Consumer;
  * @see QueryParam
  * @since 3.0
  */
+@Slf4j
 public class QueryParamEntity extends QueryParam {
 
     private static final long serialVersionUID = 8097500947924037523L;
 
     @Getter
+    @Deprecated
     private String termExpression;
 
     @Getter
@@ -39,6 +43,11 @@ public class QueryParamEntity extends QueryParam {
     @Getter
     private String orderBy;
 
+    //总数,设置了此值时,在分页查询的时候将不执行count.
+    @Getter
+    @Setter
+    private Integer total;
+
     /**
      * 创建一个空的查询参数实体,该实体无任何参数.
      *
@@ -126,18 +135,20 @@ public class QueryParamEntity extends QueryParam {
      * </pre>
      *
      * @param termExpression 表达式
-     * @see 3.0.5
+     * @since 3.0.5
      */
+    @Deprecated
     public void setTermExpression(String termExpression) {
         this.termExpression = termExpression;
-        setTerms(TermExpressionParser.parse(termExpression));
+        log.warn("termExpression is deprecated,please use where.");
+        setWhere(termExpression);
     }
 
     /**
      * 表达式方式排序
      *
      * @param orderBy 表达式
-     * @see 4.0.1
+     * @since 4.0.1
      */
     public void setOrderBy(String orderBy) {
         this.orderBy = orderBy;
@@ -145,13 +156,14 @@ public class QueryParamEntity extends QueryParam {
     }
 
     /**
-     * 表达式查询条件
+     * 表达式查询条件,没有SQL注入问题,放心使用
      *
      * @param where 表达式
+     * @since 4.0.1
      */
     public void setWhere(String where) {
         this.where = where;
-        setTermExpression(where);
+        setTerms(TermExpressionParser.parse(termExpression));
     }
 
     @Override

+ 24 - 13
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/ReactiveCrudService.java

@@ -7,6 +7,7 @@ import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
 import org.hswebframework.ezorm.rdb.mapping.ReactiveUpdate;
 import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
 import org.hswebframework.web.api.crud.entity.PagerResult;
+import org.hswebframework.web.api.crud.entity.QueryParamEntity;
 import org.reactivestreams.Publisher;
 import org.springframework.transaction.ReactiveTransactionManager;
 import org.springframework.transaction.annotation.Transactional;
@@ -88,13 +89,13 @@ public interface ReactiveCrudService<E, K> {
     }
 
     @Transactional(readOnly = true)
-    default Flux<E> query(Mono<? extends QueryParam> queryParamMono) {
+    default Flux<E> query(Mono<? extends QueryParamEntity> queryParamMono) {
         return queryParamMono
                 .flatMapMany(this::query);
     }
 
     @Transactional(readOnly = true)
-    default Flux<E> query(QueryParam param) {
+    default Flux<E> query(QueryParamEntity param) {
         return getRepository()
                 .createQuery()
                 .setParam(param)
@@ -102,40 +103,50 @@ public interface ReactiveCrudService<E, K> {
     }
 
     @Transactional(readOnly = true)
-    default Mono<PagerResult<E>> queryPager(QueryParam queryParamMono) {
+    default Mono<PagerResult<E>> queryPager(QueryParamEntity queryParamMono) {
         return queryPager(queryParamMono, Function.identity());
     }
 
     @Transactional(readOnly = true)
-    default <T> Mono<PagerResult<T>> queryPager(QueryParam param, Function<E, T> mapper) {
+    default <T> Mono<PagerResult<T>> queryPager(QueryParamEntity query, Function<E, T> mapper) {
+        if (query.getTotal() != null) {
+            return getRepository()
+                    .createQuery()
+                    .setParam(query.rePaging(query.getTotal()))
+                    .fetch()
+                    .map(mapper)
+                    .collectList()
+                    .map(list -> PagerResult.of(query.getTotal(), list, query));
+        }
         return getRepository()
                 .createQuery()
-                .setParam(param)
+                .setParam(query)
                 .count()
                 .flatMap(total -> {
                     if (total == 0) {
-                        return Mono.just(PagerResult.of(0, Collections.emptyList(), param));
+                        return Mono.just(PagerResult.empty());
                     }
-                    return query(Mono.just(param.rePaging(total))).map(mapper)
+                    return query(query.clone().rePaging(total))
+                            .map(mapper)
                             .collectList()
-                            .map(list -> PagerResult.of(total, list, param));
+                            .map(list -> PagerResult.of(total, list, query));
                 });
     }
 
     @Transactional(readOnly = true)
-    default <T> Mono<PagerResult<T>> queryPager(Mono<? extends QueryParam> queryParamMono, Function<E, T> mapper) {
+    default <T> Mono<PagerResult<T>> queryPager(Mono<? extends QueryParamEntity> queryParamMono, Function<E, T> mapper) {
         return queryParamMono
-                .cast(QueryParam.class)
+                .cast(QueryParamEntity.class)
                 .flatMap(param -> queryPager(param, mapper));
     }
 
     @Transactional(readOnly = true)
-    default Mono<PagerResult<E>> queryPager(Mono<? extends QueryParam> queryParamMono) {
+    default Mono<PagerResult<E>> queryPager(Mono<? extends QueryParamEntity> queryParamMono) {
         return queryPager(queryParamMono, Function.identity());
     }
 
     @Transactional(readOnly = true)
-    default Mono<Integer> count(QueryParam queryParam) {
+    default Mono<Integer> count(QueryParamEntity queryParam) {
         return getRepository()
                 .createQuery()
                 .setParam(queryParam)
@@ -143,7 +154,7 @@ public interface ReactiveCrudService<E, K> {
     }
 
     @Transactional(readOnly = true)
-    default Mono<Integer> count(Mono<? extends QueryParam> queryParamMono) {
+    default Mono<Integer> count(Mono<? extends QueryParamEntity> queryParamMono) {
         return queryParamMono.flatMap(this::count);
     }
 

+ 86 - 18
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveQueryController.java

@@ -1,10 +1,8 @@
 package org.hswebframework.web.crud.web.reactive;
 
-import org.hswebframework.ezorm.core.param.QueryParam;
 import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
 import org.hswebframework.web.api.crud.entity.PagerResult;
 import org.hswebframework.web.api.crud.entity.QueryParamEntity;
-import org.hswebframework.web.authorization.Permission;
 import org.hswebframework.web.authorization.annotation.Authorize;
 import org.hswebframework.web.authorization.annotation.QueryAction;
 import org.hswebframework.web.exception.NotFoundException;
@@ -14,13 +12,29 @@ import org.springframework.web.bind.annotation.PostMapping;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
-import java.util.Collections;
-
+/**
+ * 基于{@link ReactiveRepository}的响应式查询控制器.
+ *
+ * @param <E> 实体类
+ * @param <K> 主键类型
+ * @see ReactiveRepository
+ */
 public interface ReactiveQueryController<E, K> {
 
     @Authorize(ignore = true)
     ReactiveRepository<E, K> getRepository();
 
+    /**
+     * 查询,但是不返回分页结果.
+     *
+     * <pre>
+     *     GET /_query/no-paging?pageIndex=0&pageSize=20&where=name is 张三&orderBy=id desc
+     * </pre>
+     *
+     * @param query 动态查询条件
+     * @return 结果流
+     * @see QueryParamEntity
+     */
     @GetMapping("/_query/no-paging")
     @QueryAction
     default Flux<E> query(QueryParamEntity query) {
@@ -30,12 +44,47 @@ public interface ReactiveQueryController<E, K> {
                 .fetch();
     }
 
+    /**
+     * POST方式查询.不返回分页结果
+     *
+     * <pre>
+     *     POST /_query/no-paging
+     *
+     *     {
+     *         "pageIndex":0,
+     *         "pageSize":20,
+     *         "where":"name like 张%", //放心使用,没有SQL注入
+     *         "orderBy":"id desc",
+     *         "terms":[ //高级条件
+     *             {
+     *                 "column":"name",
+     *                 "termType":"like",
+     *                 "value":"张%"
+     *             }
+     *         ]
+     *     }
+     * </pre>
+     *
+     * @param query 查询条件
+     * @return 结果流
+     * @see QueryParamEntity
+     */
     @PostMapping("/_query/no-paging")
     @QueryAction
     default Flux<E> query(Mono<QueryParamEntity> query) {
         return query.flatMapMany(this::query);
     }
 
+    /**
+     * 统计查询
+     *
+     * <pre>
+     *     GET /_count
+     * </pre>
+     *
+     * @param query 查询条件
+     * @return 统计结果
+     */
     @GetMapping("/_count")
     @QueryAction
     default Mono<Integer> count(QueryParamEntity query) {
@@ -45,29 +94,48 @@ public interface ReactiveQueryController<E, K> {
                 .count();
     }
 
+    /**
+     * GET方式分页查询
+     *
+     * <pre>
+     *    GET /_query/no-paging?pageIndex=0&pageSize=20&where=name is 张三&orderBy=id desc
+     * </pre>
+     *
+     * @param query 查询条件
+     * @return 分页查询结果
+     * @see PagerResult
+     */
     @GetMapping("/_query")
     @QueryAction
     default Mono<PagerResult<E>> queryPager(QueryParamEntity query) {
-        return queryPager(Mono.just(query));
+        if (query.getTotal() != null) {
+            return getRepository()
+                    .createQuery()
+                    .setParam(query.rePaging(query.getTotal()))
+                    .fetch()
+                    .collectList()
+                    .map(list -> PagerResult.of(query.getTotal(), list, query));
+        }
+        return getRepository()
+                .createQuery()
+                .setParam(query)
+                .count()
+                .flatMap(total -> {
+                    if (total == 0) {
+                        return Mono.just(PagerResult.empty());
+                    }
+                    return query(query.clone().rePaging(total))
+                            .collectList()
+                            .map(list -> PagerResult.of(total, list, query));
+                });
     }
 
+
     @PostMapping("/_query")
     @QueryAction
     @SuppressWarnings("all")
     default Mono<PagerResult<E>> queryPager(Mono<QueryParamEntity> query) {
-        return  query
-                .flatMap(param->{
-                    return getRepository().createQuery().setParam(param).count()
-                            .flatMap(total->{
-                                if (total == 0) {
-                                    return Mono.just(PagerResult.empty());
-                                }
-                                return query
-                                        .map(QueryParam::clone)
-                                        .flatMap(q -> query(Mono.just(q.rePaging(total))).collectList())
-                                        .map(list->PagerResult.of(total,list,param));
-                            });
-                });
+        return query.flatMap(q -> queryPager(q));
     }
 
     @PostMapping("/_count")

+ 1 - 1
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/web/reactive/ReactiveServiceQueryController.java

@@ -45,7 +45,7 @@ public interface ReactiveServiceQueryController<E, K> {
     @GetMapping("/_query")
     @QueryAction
     default Mono<PagerResult<E>> queryPager(QueryParamEntity query) {
-        return queryPager(Mono.just(query));
+        return getService().queryPager(query);
     }
 
     @PostMapping("/_query")