Java 使用 Spring Boot CrudRepository 过滤数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33646714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:52:24  来源:igfitidea点击:

Filtering data with Spring boot CrudRepository

javarestspring-bootspring-dataspring-data-jpa

提问by Smajl

I have a simple REST service which access data with Spring boot CrudRepository.

我有一个简单的 REST 服务,它使用 Spring boot 访问数据CrudRepository

This repository already implements pagination and sorting capabilities like this:

这个存储库已经实现了这样的分页和排序功能:

public interface FlightRepository extends CrudRepository<Flight, Long> {
  List<Flight> findAll(Pageable pageable);
}

Calling it:

调用它:

Sort sort = new Sort(direction, ordering);
PageRequest page = new PageRequest(xoffset, xbase, sort);

return flightRepo.findAll(page);

I would like to add also filtering to this repository (for example return only entities with id > 13 AND id < 27). The CrudRepository does not seem to support this functionality. Is there some way how to achieve this or do I need to use different approach?

我还想向这个存储库添加过滤(例如只返回带有 的实体id > 13 AND id < 27)。CrudRepository 似乎不支持此功能。有什么方法可以实现这一点还是我需要使用不同的方法?

Thanks for any tips!

感谢您提供任何提示!

采纳答案by Alan Hay

An alternative, which would address your concern in the comments above about having to create query methods for every combination of parameters, is to use the Specification pattern via either the Criteria API or by using QueryDSL.

另一种替代方法是通过 Criteria API 或使用 QueryDSL 来使用规范模式,它可以解决您在上述评论中关于必须为每个参数组合创建查询方法的问题。

Both approaches are outlined at the below in response to the concern that:

下文概述了这两种方法,以应对以下问题:

the number of query methods might grow for larger applications because of - and that's the second point - the queries define a fixed set of criterias. To avoid these two drawbacks, wouldn't it be cool if you could come up with a set of atomic predicates that you could combine dynamically to build your query?

对于较大的应用程序,查询方法的数量可能会增加,因为 - 这是第二点 - 查询定义了一组固定的条件。为了避免这两个缺点,如果您能提出一组可以动态组合以构建查询的原子谓词,岂不是很酷?

https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

I find QueryDSL a bit easier to work with. You need only define one interface method which you can then pass any combination of parameters to as a predicate.

我发现 QueryDSL 更容易使用。您只需要定义一个接口方法,然后您就可以将任何参数组合作为谓词传递给它。

e.g.

例如

public interface UserRepository extends PagingAndSortingRepository<User, Long>, QueryDslPredicateExecutor<User> {
    public List<User> findAll(Predicate predicate);
}

and to query:

并查询:

repository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.M)));

repository.findAll(QUser.user.address.town.eq("Edinburgh"));

repository.findAll(QUser.user.foreName.eq("Jim"));

where QUser is a QueryDSL auto-generated class.

其中 QUser 是 QueryDSL 自动生成的类。

http://docs.spring.io/spring-data/jpa/docs/current/api/index.html?org/springframework/data/jpa/repository/support/QueryDslRepositorySupport.html

http://docs.spring.io/spring-data/jpa/docs/current/api/index.html?org/springframework/data/jpa/repository/support/QueryDslRepositorySupport.html

http://www.querydsl.com/static/querydsl/2.1.0/reference/html/ch02s02.html

http://www.querydsl.com/static/querydsl/2.1.0/reference/html/ch02s02.html

Update

更新

From the Gosling release of the Spring Data module there is now support for automatic predicate generation from HTTP parameters in a web application.

从 Spring Data 模块的 Gosling 版本开始,现在支持从 Web 应用程序中的 HTTP 参数自动生成谓词。

https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#querydsl-web-support

https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#querydsl-web-support

回答by Jos Angel George

Declare the below function in your repository [EDITED]

在您的存储库中声明以下函数 [EDITED]

Page<Flight> findByIdBetween(Long start, Long end, Pageable pageable)

Then you can invoke this function from the instance of repository. Please refer the spring-data referencefor further info..

然后你可以从存储库的实例调用这个函数。请参阅spring-data 参考以获取更多信息。