Spring Data - ignore parameter if it has a null value

前端 未结 9 1550
暖寄归人
暖寄归人 2020-11-29 03:59

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 findByParameter         


        
9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 04:42

    Here is the way for do so:

    @Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null"
          + " or c.email = :email)")
        List findCustomerByNameAndEmail(@Param("name") String name, @Param("email") String email);
    

提交回复
热议问题