Spring Data中的查询创建 - 动态where子句 [英] Query creation in Spring Data - dynamic where clause

查看:157
本文介绍了Spring Data中的查询创建 - 动态where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想要做的是有一个方法(就像findBy / get方法一样)运行WHERE和AND使用提到的属性为NOT NULL。

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子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆