%Like% Query in spring JpaRepository

2020-02-02 04:19发布

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

LIKE '%place%'-its not working.

LIKE 'place' works perfectly.

Here is my code :

@Repository("registerUserRepository")
public interface RegisterUserRepository extends 
JpaRepository<Registration,Long>{

    @Query("Select c from Registration c where c.place like:place")
     List<Registration> findByPlaceContaining(@Param("place")String place);
}

8条回答
叼着烟拽天下
2楼-- · 2020-02-02 04:50

when call funtion, I use: findByPlaceContaining("%" + place);

or: findByPlaceContaining(place + "%");

or: findByPlaceContaining("%" + place + "%");

查看更多
疯言疯语
3楼-- · 2020-02-02 04:51

answer exactly will be

-->` @Query("select u from Category u where u.categoryName like %:input%")
     List findAllByInput(@Param("input") String input);
查看更多
霸刀☆藐视天下
4楼-- · 2020-02-02 04:55

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);
查看更多
可以哭但决不认输i
5楼-- · 2020-02-02 04:56

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

List<Registration> findByPlaceContaining(String place);
查看更多
小情绪 Triste *
6楼-- · 2020-02-02 04:56

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 :)

查看更多
手持菜刀,她持情操
7楼-- · 2020-02-02 04:59

You dont actually need the @Query annotation at all.

You can just use the following

    @Repository("registerUserRepository")
    public interface RegisterUserRepository extends JpaRepository<Registration,Long>{

    List<Registration> findByPlaceIgnoreCaseContaining(String place);

    }
查看更多
登录 后发表回答