使用Spring Boot CrudRepository过滤数据 [英] Filtering data with Spring boot CrudRepository

查看:201
本文介绍了使用Spring Boot CrudRepository过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的REST服务,可通过Spring boot CrudRepository访问数据.

I have a simple REST service which access data with 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);
}

调用:

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

return flightRepo.findAll(page);

我还要向此存储库添加过滤(例如,仅返回带有id > 13 AND id < 27的实体). CrudRepository似乎不支持此功能.有什么方法可以实现这一目标,或者我需要使用其他方法吗?

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?

感谢任何提示!

推荐答案

一种替代方案,可以通过以下两种方法之一使用规范"模式:条件API或使用QueryDSL.

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.

以下针对以下问题概述了两种方法:

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/

我发现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.

例如

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

并查询:

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"));

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

where QUser is a QueryDSL auto-generated class.

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

更新

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

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

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆