Spring JpaRepository 中的 %Like% 查询 [英] %Like% Query in spring JpaRepository

查看:20
本文介绍了Spring JpaRepository 中的 %Like% 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 JpaRepository 中编写一个类似的查询,但它没有返回任何内容:

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

LIKE '%place%' - 它不起作用.

LIKE 'place' 完美运行.

这是我的代码:

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

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

推荐答案

Spring Data JPA 查询需要在查询中的 like 后面加上%"字符和空格字符,如

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%").

参见http://docs.spring.io/spring-data/jpa/docs/current/参考/html.

您可能希望完全摆脱 @Query 注释,因为它似乎类似于标准查询(由 spring 数据代理自动实现);即使用单行

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

就足够了.

这篇关于Spring JpaRepository 中的 %Like% 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆