gpt4 book ai didi

java - 如何在 Spring Data Jpa 中编写接受四种参数组合的选择查询

转载 作者:行者123 更新时间:2023-11-30 06:39:05 30 4
gpt4 key购买 nike

我试图在用户搜索多个组合时显示结果,他可以搜索的选项 -

  1. country
  2. state
  3. district
  4. zipCode

他可以搜索的示例组合有(国家、州)、(国家、地区)、(州、邮政编码)等。

我也使用 Spring Data Jpa 进行查询和分页。

我是 Spring Jpa 的新手,任何帮助将不胜感激。

谢谢!

最佳答案

有一个非常简单的技巧可以执行您需要的操作;)

@Query("select e from Entity e where "
+"(:country = '' or e.country like '%:country%') and "
+"(:state = '' or e.state like '%:state%') and "
+"(:district = '' or e.district like '%:district%') and "
+"(:zipCode = '' or e.zipCode like '%:zipCode%')"
Page<Entity> advancedSearch(@Param("country") String country,
@Param("state") String state,
@Param("district") String district,
@Param("zipCode") String zipCode,
Pageable page);

因此,当您需要调用advancedSearch时,您可以仅设置您需要的参数,其他设置为"":

Page<Entity> enityPages = advancedSearch("", "California", "1st", "", new PageRequest(...)); 

关于java - 如何在 Spring Data Jpa 中编写接受四种参数组合的选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44704133/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com