简体   繁体   English

QueryDSL:从实体构建查询

[英]QueryDSL: building a query from an entity

I've just started integrating QueryDSL into a Spring Boot project and I'm looking for a way to build a query out of an existing entity bean. 我刚刚开始将QueryDSL集成到Spring Boot项目中,并且正在寻找一种从现有实体Bean构建查询的方法。 Using @ModelAttribute it's nice and easy to pass in an entity via a GET request from the controller as long as the parameters align with the bean: 使用@ModelAttribute ,只要参数与Bean对齐,就可以通过控制器的GET请求传递实体,这@ModelAttribute好又容易:

public Page<Company> getLogins(@ModelAttribute Company company, Pageable pageable, @RequestParam(value = "page", required = false) String pageNumber){
    return companyService.findbyParameters(company,pageNumber);

}

And in the service class, I can use the BooleanBuilder to build up a query: 在服务类中,我可以使用BooleanBuilder建立查询:

    public Page<Company> findbyParameters(Company companySearch,String pageNumber){
        QCompany company = QCompany.company;
        BooleanBuilder builder = new BooleanBuilder();
        if (companySearch.getEmail() != null && !companySearch.getEmail().equals("")){
            builder.and(company.email.eq(companySearch.getEmail()));
        }
        if (companySearch.getCompanyName() != null && !companySearch.getCompanyName().equals("")){
            builder.and(company.companyName.eq(companySearch.getCompanyName()));
        }
       //add other clauses...
       return loginRepository.findAll(builder.getValue(),pageableService.getPageRequest(pageNumber));
    }

..and this works fine. ..这很好。 But it seems like an unnecessary amount of plumbing since I'll have to write similar, longwinded conditional code for each entity I'm working with. 但这似乎是不必要的工作,因为我必须为要使用的每个实体编写类似的,冗长的条件代码。 I reckon that reflection might be an option, but I'm not sure if QueryDSL has something built in that handles this situation. 我认为反射可能是一种选择,但是我不确定QueryDSL是否内置处理这种情况的东西。 I've looked at the QueryDSL docs and nothing jumped out at me. 我查看了QueryDSL文档,但没有发现任何问题。

So is there a nice, tidy way of handling this situation without clogging up my service classes with boilerplate? 那么,有没有一种很好而整洁的方式来处理这种情况,而又不会用样板阻塞服务类?

You can use Spring Data's QueryDSL integration. 您可以使用Spring Data的QueryDSL集成。 Basically, you extend the QueryDslPredicateExecutor in your repository interface and it add a findAll method that gets a QueryDSL Predicate and filter all the results based on that Predicate . 基本上,您可以在存储库接口中扩展QueryDslPredicateExecutor ,并添加一个findAll方法来获取QueryDSL Predicate并基于该Predicate过滤所有结果。 You see more details here . 您可以在此处查看更多详细信息。

It turns out that the exact thing I was looking for is Spring Data's query by example API. 事实证明,我要查找的确切内容是通过示例API查询Spring Data。

https://www.baeldung.com/spring-data-query-by-example https://www.baeldung.com/spring-data-query-by-example

It lets you create a query by providing a sample entity and a matcher which defines things like case sensitivity, partial 'like' matching and so on. 它允许您通过提供一个示例实体和一个匹配器(定义大小写区分,部分“喜欢”匹配等)来创建查询。

It's very useful in limited situations, and can drastically reduce boilerplate query code; 它在有限的情况下非常有用,并且可以大大减少样板查询代码; but when you want to query a more complex graph of data you'll want to use a different approach. 但是当您要查询更复杂的数据图时,您将希望使用其他方法。

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

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