mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-02 02:43:59 +08:00
优化条件
This commit is contained in:
@@ -33,6 +33,12 @@ public class QueryParamEntity extends QueryParam {
|
||||
@Getter
|
||||
private String termExpression;
|
||||
|
||||
@Getter
|
||||
private String where;
|
||||
|
||||
@Getter
|
||||
private String orderBy;
|
||||
|
||||
/**
|
||||
* 创建一个空的查询参数实体,该实体无任何参数.
|
||||
*
|
||||
@@ -101,7 +107,7 @@ public class QueryParamEntity extends QueryParam {
|
||||
if (null != before) {
|
||||
before.accept(query);
|
||||
}
|
||||
if(terms.isEmpty()){
|
||||
if (terms.isEmpty()) {
|
||||
return query;
|
||||
}
|
||||
return query
|
||||
@@ -127,6 +133,27 @@ public class QueryParamEntity extends QueryParam {
|
||||
setTerms(TermExpressionParser.parse(termExpression));
|
||||
}
|
||||
|
||||
/**
|
||||
* 表达式方式排序
|
||||
*
|
||||
* @param orderBy 表达式
|
||||
* @see 4.0.1
|
||||
*/
|
||||
public void setOrderBy(String orderBy) {
|
||||
this.orderBy = orderBy;
|
||||
setSorts(TermExpressionParser.parseOrder(orderBy));
|
||||
}
|
||||
|
||||
/**
|
||||
* 表达式查询条件
|
||||
*
|
||||
* @param where 表达式
|
||||
*/
|
||||
public void setWhere(String where) {
|
||||
this.where = where;
|
||||
setTermExpression(where);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Term> getTerms() {
|
||||
List<Term> terms = super.getTerms();
|
||||
|
||||
@@ -2,16 +2,23 @@ package org.hswebframework.web.api.crud.entity;
|
||||
|
||||
import org.hswebframework.ezorm.core.NestConditional;
|
||||
import org.hswebframework.ezorm.core.dsl.Query;
|
||||
import org.hswebframework.ezorm.core.param.Sort;
|
||||
import org.hswebframework.ezorm.core.param.Term;
|
||||
import org.hswebframework.ezorm.core.param.TermType;
|
||||
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* 动态条件表达式解析器
|
||||
* name=测试 and age=test
|
||||
*
|
||||
* @author zhouhao
|
||||
* @since 3.0.10
|
||||
*/
|
||||
public class TermExpressionParser {
|
||||
|
||||
@@ -162,6 +169,29 @@ public class TermExpressionParser {
|
||||
return conditional.getParam().getTerms();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析排序表达式
|
||||
* <pre>
|
||||
* age asc,score desc
|
||||
* </pre>
|
||||
*
|
||||
* @param expression 表达式
|
||||
* @return 排序集合
|
||||
* @since 4.0.1
|
||||
*/
|
||||
public static List<Sort> parseOrder(String expression) {
|
||||
return Stream.of(expression.split("[,]"))
|
||||
.map(str -> str.split("[ ]"))
|
||||
.map(arr -> {
|
||||
Sort sort = new Sort();
|
||||
sort.setName(arr[0]);
|
||||
if (arr.length > 1 && "desc".equalsIgnoreCase(arr[1])) {
|
||||
sort.desc();
|
||||
}
|
||||
return sort;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static String convertTermType(String termType) {
|
||||
if (termType == null) {
|
||||
return TermType.eq;
|
||||
|
||||
Reference in New Issue
Block a user