[英]How to write JPARepository query where a query parameter is a list?
The documentation for Spring Data's JPARepository
provides examples such as Spring Data的
JPARepository
文档提供了以下示例
List<User> findByLastname(String lastname);
for and SpringData will do all the work generating the proper query and the correct users will be returned. for和SpringData将完成生成正确查询的所有工作,并将返回正确的用户。
But suppose I want to get 50 users and I have a list of their last names. 但是假设我想获得50个用户,并且我有他们的姓氏列表。 I don't want to call the above method 50 times.
我不想调用上述方法50次。 I would like to call the JPA equivalent of
我想将JPA称为
select *
from user
where last_name in ('N0', 'N1', 'N2'... 'N49');
Now looking at the documentation I see some possibilities such as named queries and specifications. 现在查看文档,我看到了一些可能性,例如命名查询和规范。 The named queries seem to require specifying SQL in a Java annotation.
命名查询似乎需要在Java批注中指定SQL。 The specifications used with the
findAll
methods have is
and hasMoreThan
type methods in the examples, but I don't see any that would turn into in
queries. 在示例中,与
findAll
方法使用的规范具有is
和hasMoreThan
类型方法,但是我看不到任何会in
查询中变成in
。 And the Javadocs for specifications don't have any example usage. 规范的Javadocs没有任何示例用法。
How does one create a method in a JPARepository
to query by a list of possibilities? 一个人如何在创建一个方法
JPARepository
通过的可能性列表查询?
Create an array of lastname that you want to make as query parameter. 创建一个要用作查询参数的姓氏数组。 Then try this: findAllInLastName(List lastNames);
然后尝试以下方法:findAllInLastName(List lastNames); The Spring data automatically search a suitable query method when you extend the JpaRepository class as long as you follow the right format of creating a query method name.
扩展JpaRepository类时,只要遵循创建查询方法名称的正确格式,Spring数据就会自动搜索合适的查询方法。 Hope this helps you.
希望这对您有所帮助。 And try to read the Spring Data online documentation.
并尝试阅读Spring Data在线文档。 Cheers brother!
干杯哥!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.