%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 05:06

Try this.

@Query("Select c from Registration c where c.place like '%'||:place||'%'")
查看更多
仙女界的扛把子
3楼-- · 2020-02-02 05:09

The spring data JPA query needs the "%" chars as well as a space char following like in your query, as in

@Query("Select c from Registration c where c.place like %:place%").

Cf. http://docs.spring.io/spring-data/jpa/docs/current/reference/html.

You may want to get rid of the @Queryannotation alltogether, as it seems to resemble the standard query (automatically implemented by the spring data proxies); i.e. using the single line

List<Registration> findByPlaceContaining(String place);

is sufficient.

查看更多
登录 后发表回答