Java Spring Data 中的查询创建 - 动态 where 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21482339/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:56:45  来源:igfitidea点击:

Query creation in Spring Data - dynamic where clause

javasqljpaspring-data-jpa

提问by Chinmay

Is there a way in Spring data to dynamically form the where clause?

Spring数据中有没有办法动态形成where子句?

What I want to do is have a method (which is like the findBy / get method) which runs a WHERE and AND using the mentioned properties which are NOT NULL.

我想要做的是有一个方法(类似于 findBy / get 方法),它使用提到的非空属性运行 WHERE 和 AND。

For example,

例如,

Consider the object Person [firstName, lastName, age, gender]

Our method looks something like this

我们的方法看起来像这样

findBy_IfNotNullFirstName_AndIfNotNullLastName_AndIfNotNullAge_AndIfNotNullGender(String firstName, String lastName, Integer age, String gender)

Thanks.

谢谢。

采纳答案by iouardi

Take a look at JPA Specification and Predicate, and Even better QueryDSL, there both supported by spring data repositories. This article provide an example: http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

看看 JPA 规范和谓词,以及更好的 QueryDSL,它们都由 spring 数据存储库支持。本文提供了一个例子:http: //spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/