%Like% Query in spring JpaRepository

前端 未结 10 1652
南方客
南方客 2020-12-04 11:36

I would like to write a like query in JpaRepository but it is not returning anything :

LIKE \'%place%\'-its not working.

LIKE

相关标签:
10条回答
  • 2020-12-04 11:58

    You can also implement the like queries using Spring Data JPA supported keyword "Containing".

    List<Registration> findByPlaceContaining(String place);
    
    0 讨论(0)
  • 2020-12-04 11:59

    You can have one alternative of using placeholders as:

    @Query("Select c from Registration c where c.place LIKE  %?1%")
    List<Registration> findPlaceContainingKeywordAnywhere(String place);
    
    0 讨论(0)
  • 2020-12-04 12:02

    For your case, you can directly use JPA methods. That is like bellow :

    Containing: select ... like %:place%

    List<Registration> findByPlaceContainingIgnoreCase(String place);
    

    here, IgnoreCase will help you to search item with ignoring the case.

    Using @Query in JPQL :

    @Query("Select registration from Registration registration where 
    registration.place LIKE  %?1%")
    List<Registration> findByPlaceContainingIgnoreCase(String place);
    

    Here are some related methods:

    1. Like findByPlaceLike

      … where x.place like ?1

    2. StartingWith findByPlaceStartingWith

      … where x.place like ?1 (parameter bound with appended %)

    3. EndingWith findByPlaceEndingWith

      … where x.place like ?1 (parameter bound with prepended %)

    4. Containing findByPlaceContaining

      … where x.place like ?1 (parameter bound wrapped in %)

    More info , view this link , this link and this

    Hope this will help you :)

    0 讨论(0)
  • 2020-12-04 12:02

    answer exactly will be

    -->` @Query("select u from Category u where u.categoryName like %:input%")
         List findAllByInput(@Param("input") String input);
    
    0 讨论(0)
提交回复
热议问题