Spring Data - 如果参数为空值,则忽略它 [英] Spring Data - ignore parameter if it has a null value

查看:53
本文介绍了Spring Data - 如果参数为空值,则忽略它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个带有两个参数的 spring 数据存储库接口.有没有办法让它具有以下行为?

I want to have a spring data repository interface that takes two parameters. Is there a way to make it have the following behaviour?

MyObject findByParameterOneAndParameterTwo( String parameterOne, String parameterTwo);

如果两个参数都有一个值,我希望它正常运行并对两个值进行与"运算.

If both parameters have a value, I would like it to behave normally and do an "AND" for both values.

例如,如果第二个参数为空,那么它将仅通过 ParameterOne 进行搜索

If for example the second parameter is null, then it would search only by ParameterOne

有什么建议吗?

推荐答案

我不确定存储库方法命名是否可行,但您可以像

I'm not sure it is possible with repository methods naming but you can use @Query like

(:parameterOne is null or parameter1 = :parameterOne) and (:parameterTwo is null or parameter2 = :parameterTwo)

这篇关于Spring Data - 如果参数为空值,则忽略它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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