问题描述
Spring 数据中有没有办法动态形成 where 子句?
Is there a way in Spring data to dynamically form the where clause?
我想要做的是有一个方法(类似于 findBy/get 方法),它运行 WHERE 和 AND 使用提到的非空属性.
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.
例如
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)
谢谢.
推荐答案
看看 JPA 规范和谓词,以及更好的 QueryDSL,它们都由 spring 数据存储库支持.这篇文章提供了一个例子:http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
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/
这篇关于Spring Data 中的查询创建 - 动态 where 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!