Spring Data动态查询 [英] Spring Data dynamic query
问题描述
我正在尝试使用spring数据设置动态查询,基本上我有一堆具有一系列特征的数组,我需要根据这些特性组装查询,几乎就像WHERE特征= A和特征 = B AND特征= C但特征量可能会有所不同。
I'm trying to set up a dynamic query using spring data, basically I have an array with a bunch of characteristics and I need to assemble the query based on those characteristics, pretty much something like "WHERE characteristic = A AND characteristic = B AND characteristic = C" but the amount of characteristics may vary.
我注意到我可以使用@Query注释但是可以得到结果@Query可分页?
I noticed that I can use the @Query annotation but is it possible to make the result of the @Query pageable ?
还有其他方法可以实现吗?
Is there any other way to accomplish this?
Page<Recipe> findDistinctByNameContainingAndOrganizationAndCharacteristicsInOrIngredientsContainingAndOrganizationAndCharacteristicsInOrDescriptionContainingAndOrganizationAndCharacteristicsInAllIgnoreCase(
String name, Organization organization1, List<Characteristic> characteristic1,
String ingredients, Organization organization2, List<Characteristic> characteristic2,
String description, Organization organization3, List<Characteristic> characteristic3,
Pageable pageable);
推荐答案
是的。 @Query
支持分页..是的还有另一种方法可以实现这一点。
Yes. @Query
supports pageable.. and yes there's another way to accomplish this.
你可以看一下建筑物使用条件查询,允许您动态地在条件中添加AND / OR。我实际上使用querydsl来促进我的开发。
You could take a look at building query with criteria, that allow you add AND / OR where conditions dynamically. I actually use querydsl to facilitate my development.
在这篇文章中你可以找到一个很好的解释:http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and -querydsl /
In this post you can find a good explanation of booth: http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
尽管开始时看起来可能更复杂,但使用此功能会使您的代码更加符合日益增加的复杂性
Altough it could be seem more complex at beginning, the use of this feature will make your code more consistent with increasing complexity
这篇关于Spring Data动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!