使用 Spring 引导 CrudRepository 过滤数据
Filtering data with Spring boot CrudRepository
我有一个简单的 REST 服务,它通过 Spring 引导访问数据 CrudRepository
。
此存储库已实现分页和排序功能,如下所示:
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);
我还想向此存储库添加过滤(例如 return 仅具有 id > 13 AND id < 27
的实体)。 CrudRepository 似乎不支持此功能。有什么方法可以实现这个还是我需要使用不同的方法?
感谢任何提示!
在您的存储库中声明以下函数[EDITED]
Page<Flight> findByIdBetween(Long start, Long end, Pageable pageable)
然后您可以从存储库实例调用此函数。请参阅 spring-data reference 了解更多信息..
另一种方法可以解决您在上面的评论中关于必须为每个参数组合创建查询方法的担忧,即通过条件 API 或使用 QueryDSL 使用规范模式。
下面概述了两种方法,以回应以下问题:
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 更容易一些。您只需要定义一个接口方法,然后您可以将任何参数组合作为谓词传递给它。
例如
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 class。
http://www.querydsl.com/static/querydsl/2.1.0/reference/html/ch02s02.html
更新
从 Spring 数据模块的 Gosling 版本开始,现在支持从 Web 应用程序中的 HTTP 参数自动生成谓词。
https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#querydsl-web-support
我有一个简单的 REST 服务,它通过 Spring 引导访问数据 CrudRepository
。
此存储库已实现分页和排序功能,如下所示:
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);
我还想向此存储库添加过滤(例如 return 仅具有 id > 13 AND id < 27
的实体)。 CrudRepository 似乎不支持此功能。有什么方法可以实现这个还是我需要使用不同的方法?
感谢任何提示!
在您的存储库中声明以下函数[EDITED]
Page<Flight> findByIdBetween(Long start, Long end, Pageable pageable)
然后您可以从存储库实例调用此函数。请参阅 spring-data reference 了解更多信息..
另一种方法可以解决您在上面的评论中关于必须为每个参数组合创建查询方法的担忧,即通过条件 API 或使用 QueryDSL 使用规范模式。
下面概述了两种方法,以回应以下问题:
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 更容易一些。您只需要定义一个接口方法,然后您可以将任何参数组合作为谓词传递给它。
例如
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 class。
http://www.querydsl.com/static/querydsl/2.1.0/reference/html/ch02s02.html
更新
从 Spring 数据模块的 Gosling 版本开始,现在支持从 Web 应用程序中的 HTTP 参数自动生成谓词。
https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#querydsl-web-support