简体   繁体   English

QueryDSL动态谓词链接

[英]QueryDSL dynamic predicate chaining

How to use QueryDSL to dynamically build and chain predicates. 如何使用QueryDSL动态建立和链接谓词。

I have a UI with a datatable. 我有一个带有数据表的UI。 Each column has a box where you can enter one filter term. 每列都有一个框,您可以在其中输入一个过滤条件。 Like so: 像这样:

用户界面

In the example above eventno filter = "0288" and address filter = "drive". 在上面的示例中,eventno过滤器=“ 0288”,地址过滤器=“驱动器”。 These are then sent by the UI to the backend in a pagination object, which contains a map of column names and filter strings. 然后,UI将这些内容发送到分页对象中的后端,该对象包含列名称和过滤字符串的映射。 Like so: 像这样:

分页

In my backend with QueryDSL I now need to dynamically construct a Predicate based on the terms provided for the columns. 现在,在使用QueryDSL的后端中,我需要根据为各列提供的术语动态构建谓词。 I have tried this for just the address event and address column, but it does not seem to filter both. 我仅针对address事件和address列尝试过此操作,但似乎并未同时过滤两者。

public Page<EpisodeDashboard> getPage(int pageNumber, int pageSize, Sort sort, PaginationCriteria pagination) {
    BooleanBuilder where = new BooleanBuilder();
    if (pagination.getFilterBy().getMapOfFilters().get("eventno")!=null) {
        where.and(qEpisode.eventno.containsIgnoreCase(pagination.getFilterBy().getMapOfFilters().get("eventno")));
        }
    if (pagination.getFilterBy().getMapOfFilters().get("address")!=null) {
        where.and(qEpisode.address.formattedAddress.containsIgnoreCase(pagination.getFilterBy().getMapOfFilters().get("address")));
        }
    List<Episode> e = episodeRepository.findAll(where);

I probably want something with a for each key/value in the hashmap to dynamically construct the predicates. 我可能希望散列表中的每个键/值都带有a的东西来动态构造谓词。

The above code actually works, but : 上面的代码实际上有效,但是:

episodeRepository.findAll(where);

Returns a iterable and most of my project uses List. 返回一个可迭代的对象,并且我的大多数项目都使用List。 So I modified the Episode Repo with a override to: 因此,我将Episode Repo修改为覆盖以下内容:

    @Override
    List<Episode> findAll(Predicate predicate);

Now a list is returned and the code works. 现在,将返回一个列表,并且代码可以正常工作。 An enhancement on this answer would be if the "if" statements could be generated by iterating over the pagination filter hashmap. 如果可以通过遍历分页过滤器哈希图来生成“ if”语句,则可以增强此答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM