查询方法中的 Spring Data 可选参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32728843/
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
Spring Data optional parameter in query method
提问by mohammad_1m2
I want to write some query methods in repository layer. This method must ignore null parameters. For example:
我想在存储库层编写一些查询方法。此方法必须忽略空参数。例如:
List<Foo> findByBarAndGoo(Bar barParam, @optional Goo gooParam);
This method must be return Foo by this condition:
此方法必须在此条件下返回 Foo:
bar == barParam && goo == gooParam;
if gooParam not null. if gooParam was null then condition change to:
如果 gooParam 不为空。如果 gooParam 为空,则条件更改为:
bar == barParam;
Is there any solution? Can someone help me?
有什么解决办法吗?有人能帮我吗?
采纳答案by Shaunak Patel
Too late to answer. Not sure about relationship between Barand Goo. Check if Examplecan helps you.
来不及回答。不确定Bar和Goo之间的关系。检查示例是否可以帮助您。
It worked for me. I have a similar situation, entity Userhave set of attributes and there is findAllmethod which search user based on attributes(which are optional).
它对我有用。我有类似的情况,实体User有一组属性,并且有findAll方法可以根据属性搜索用户(这是可选的)。
Example,
例子,
Class User{
String firstName;
String lastName;
String id;
}
Class UserService{
// All are optional
List<User> findBy(String firstName, String lastName, String id){
User u = new User();
u.setFirstName(firstName);
u.setLastName(lastName);
u.setId(id);
userRepository.findAll(Example.of(user));
// userRepository is a JpaRepository class
}
}
回答by chaserb
I don't believe you'll be able to do that with the method name approach to query definition. From the documentation (reference):
我不相信您将能够使用方法名称方法来进行查询定义。从文档(参考):
Although getting a query derived from the method name is quite convenient, one might face the situation in which either the method name parser does not support the keyword one wants to use or the method name would get unnecessarily ugly. So you can either use JPA named queries through a naming convention (see Using JPA NamedQueries for more information) or rather annotate your query method with @Query
虽然从方法名获取查询是很方便的,但人们可能会面临这样的情况,即方法名解析器不支持想要使用的关键字,或者方法名会变得不必要地难看。因此,您可以通过命名约定使用 JPA 命名查询(有关更多信息,请参阅使用 JPA NamedQueries),或者使用 @Query 注释您的查询方法
I think you have that situation here, so the answer below uses the @Query annotation approach, which is almost as convenient as the method name approach (reference).
我认为您在这里遇到了这种情况,因此下面的答案使用@Query 注释方法,这几乎与方法名称方法(参考)一样方便。
@Query("select foo from Foo foo where foo.bar = :bar and "
+ "(:goo is null or foo.goo = :goo)")
public List<Foo> findByBarAndOptionalGoo(
@Param("bar") Bar bar,
@Param("goo") Goo goo);
回答by Vitor Reis
Complementing the answer of @chaserb, I personally would add the parameter as a Java8 Optional type to make it explicit in the signature of the method the semantics that is an optional filter.
补充@chaserb 的答案,我个人会将参数添加为 Java8 Optional 类型,以使其在方法的签名中明确表示可选过滤器的语义。
@Query("select foo from Foo foo where foo.bar = :bar and "
+ "(:goo is null or foo.goo = :goo)")
public List<Foo> findByBarAndOptionalGoo(
@Param("bar") Bar bar,
@Param("goo") Optional<Goo> goo);
回答by user152468
You could code this yourself in just a few lines:
您可以自己编写几行代码:
List<Foo> findByBarAndOptionalGoo(Bar bar, Goo goo) {
return (goo == null) ? this.findByBar(bar) : this.findByBarAndGoo(bar, goo);
}
Otherwise, I don't know if Spring-Data supports this out of the box.
否则,我不知道 Spring-Data 是否支持开箱即用。