JPA - 查询参数为空

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,'%') ")