如何忽略 Spring JPQL 中的空参数?

How to ignore null parameter in Spring JPQL?

我有一个这样创建的 API。当您提供两个参数时它可以工作,但我希望它在任一参数为 null 时工作。有办法吗?

@Query("Select u from User u where u.agency = :agency and " +
            "(u.firstName like %:firstName%)")
    public List<User> multiParam(Agency agency, String firstName);

试试这个

@Query("Select u from User u where (:agency is null or u.agency = :agency) and " +
        "(:firstName is null or u.firstName like %:firstName%)")
public List<User> multiParam(Agency agency, String firstName);

如需更多参考,请查看 link