如何基于带引号的String值创建动态QueryDSL运算符

时间:2015-11-22 08:28:09

标签: spring-data-jpa querydsl

我有一个包含属性名称的pojo,作为String的逻辑运算符和property的值。我想要完成的是从pojo数据动态创建谓词或表达式等。以下是我的代码:

public class QueryParam {
    private String property = "acctType";   //can be any property of classname
    private String operator = "eqic"        //can be any logic operator !=, >, <, >=, <= etc
    private Object value;                   //will store the value of 

    // getters/setters here
}

public interface CustomerRepository extends JpaRepository<Customer, Long>, QueryDslPredicateExecutor<Customer>{

}

@Service("CustomerService")
class MyCustomerServiceImpl {
    @Resource
    private CustomerRepository custRpstry;

    //if classname is Customer, property is "acctType", operator is "eqic", and value is "Corporate"
    //I want my findAll below to retrieve all Customers having acctType = "Corporate"
    List<Customer> findAll(List<QueryParam> qryParam) {
        QCustomer qcust = QCustomer.customer;

        BooleanBuilder where = new BooleanBuilder();
        for(QueryParam param : qryParam) {

            //within this block, i want a BooleanBuilder to resolve to:
            where.and(qcust.acctType.equalsIgnoreCase("Corporate"));

            something like:
            where.and(param.getClassname().param.getProperty().param.getOperator().param.getValue())
        }

        return custRpstry.findAll(where.getValue()).getContent();

    }
}

我无法弄清楚我的BooleanBuilder特别是要转换的部分 getOperator()到.equalIgnoreCase()。

非常感谢任何帮助。

提前致谢, 马里奥

1 个答案:

答案 0 :(得分:1)

在这里结合几个相关问题的答案后,我能够制定出适合我的解决方案。

BooleanBuilder where = new BooleanBuilder();
for(QueryParam param: qryParam) {
    //create: Expressions.predicate(Operator<Boolean> opr, StringPath sp, filter value)

    //create an Operator<Boolean>
    Operator<Boolean> opr = OperationUtils.getOperator(param.getOperator().getValue());

    //create a StringPath to a class' property
    Path<User> entityPath = Expressions.path(Customer.class, "customer");
    Path<String> propPath = Expressions.path(String.class, entityPath, param.getProperty());

    //create Predicate expression
    Predicate predicate = Expressions.predicate(opr, propPath, Expressions.constant(param.getValue()));
    where.and(predicate);
}

list = repository.findAll(where.getValue(), pageReq).getContent();  

我的OperationUtils.java

public class OperationUtils {

    public static com.mysema.query.types.Operator<Boolean> getOperator(String key) {
        Map<String, com.mysema.query.types.Operator<Boolean>> operators = ImmutableMap.<String, com.mysema.query.types.Operator<Boolean>>builder()
                  .put(Operator.EQ.getValue()   ,Ops.EQ)
                  .put(Operator.NE.getValue()   ,Ops.NE)
                  .put(Operator.GT.getValue()   ,Ops.GT)
                  .put(Operator.GTE.getValue()  ,Ops.GOE)
                  .put(Operator.LT.getValue()   ,Ops.LT)
                  .put(Operator.LTE.getValue()  ,Ops.LOE)
                  .build();

        return operators.get(key);
    }
}