簡體   English   中英

JPA - 查詢參數為null

[英]JPA - query parameter is null

@Query("select users from User users  " +
        "where users.id like concat('%',?1,'%') and " +
        "users.name like concat('%',?2,'%') and " +
        "users.telNo like concat('%',?3,'%') ")
List<User> fuzzyQueryUser(Integer id, String name, String telNo);

當第一個參數id為null時,模糊查詢沒有結果。 如果參數為null,我希望在數據庫中有類似的條目時得到結果。 我該怎么辦? 謝謝。

@Query("select users from User users  " +
    "where (users.id like concat('%',?1,'%') or ?1 is null or ?1 ='')and " +
    "(users.name like concat('%',?2,'%') or ?2 is null or ?2 ='') and " +
    "(users.telNo like concat('%',?3,'%') or ?3 is null or ?3 ='')")
List<User> fuzzyQueryUser(String id, String name, String telNo);

像這樣

@Query("select users from User users  " +
        "where (users.id like concat('%',?1,'%') or ?1 is null  )and " +
        "(users.name like concat('%',?2,'%') or ?2 is null or ?2 ='') and " +
        "(users.telNo like concat('%',?3,'%') or ?3 is null or ?3 ='')")
List<User> fuzzyQueryUser(Integer id, String name, String telNo);                        

只需添加另一個條件來忽略null和可選的空字符串:

@Query("select users from User users  " +
        "where (users.id like concat('%',?1,'%') or ?1 is null) and " 
       +"users.name like concat('%',?2,'%') and " +
        "users.telNo like concat('%',?3,'%') ")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM