Spring Data optional parameter in query method

后端 未结 4 1025
有刺的猬
有刺的猬 2020-12-02 22:21

I want to write some query methods in repository layer. This method must ignore null parameters. For example:

List findByBarAndGoo(Bar barParam,          


        
4条回答
  •  有刺的猬
    2020-12-02 22:49

    Complementing the answer of @chaserb, I personally would add the parameter as a Java8 Optional type to make it explicit in the signature of the method the semantics that is an optional filter.

    @Query("select foo from Foo foo where foo.bar = :bar and "
       + "(:goo is null or foo.goo = :goo)")
    public List findByBarAndOptionalGoo(
         @Param("bar") Bar bar, 
         @Param("goo") Optional goo);
    

提交回复
热议问题