Spring JpaRepository中的类似查询

147

我想在JpaRepository中编写一个LIKE查询,但它没有返回任何结果:

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);
}
16个回答

262

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

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

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

如果您觉得这个查询类似于标准查询(由Spring Data代理自动生成),那么您可能希望完全摆脱@Query注释,并使用单行查询。

List<Registration> findByPlaceContaining(String place);

足够。


37
为了查询时更加方便,可以忽略大小写,使用如下方法:findByPlaceIgnoreCaseContaining(String place); - ilyailya
1
只是顺便说一下 - 你不能在同一个查询中混合使用 %:param% 和 %param%。否则应用程序甚至无法启动。 - RVP
2
谢谢。我曾经使用带有 'like '%:place%' 进行编写,但是没有任何结果,因为 Hibernate 会自动将 ' 添加到字符串中。 - Yamashiro Rion
1
@Query("Select c from Registration c where c.place like %:place%")在Spring Boot版本2.3.3中无法正常工作。您能否提供一些关于为什么我在使用2.3.3时无法使其工作的想法? - user10767069
1
@SubhaBhowmik,到底是哪个版本出了问题?你说对于2.3.3它既可以工作又不能工作。我也曾经在Spring Boot 2.4.0中苦苦挣扎,因为我发现你不能在SQL函数内部使用%:param%:例如,在LOWER('%:param%')中,由于引号的存在,:param没有被替换。希望这能帮到你。 - Sebastian S.
显示剩余3条评论

128

实际上你根本不需要使用@Query注解。

你可以直接使用以下方法:

    @Repository("registerUserRepository")
    public interface RegisterUserRepository extends JpaRepository<Registration,Long>{
    
    List<Registration> findByPlaceIgnoreCaseContaining(String place);

    }

1
对我来说,这是最好的解决方案。我不知道你可以将IgnoreCase与Containing结合使用,因为文档中没有提到。 - Wilson Campusano
4
如果我想使用多列,该怎么办?- 我认为@Query()是必须的,对吗?采用这种方法,我将不得不使用 or ,但实际上并不适合于这种情况:findByField1OrField2Containing(String phraseForField1,String phraseForField2) - user3529850

42

对于您的情况,您可以直接使用JPA方法。代码如下:

包含:select ... like %:place%

List<Registration> findByPlaceContainingIgnoreCase(String place);

在这里,IgnoreCase 将帮助您忽略大小写地搜索项目。

在JPQL中使用@Query:

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

以下是一些相关的方法:

  1. Like findByPlaceLike

    … 其中 x.place like ?1

  2. StartingWith findByPlaceStartingWith

    … 其中 x.place like ?1(参数绑定附加%)

  3. EndingWith findByPlaceEndingWith

    … 其中 x.place like ?1(参数绑定前置%)

  4. Containing findByPlaceContaining

    … 其中 x.place like ?1(参数绑定在%内)

更多信息,请查看此链接(引用自上述引文),此链接此链接


你如何针对视图执行此操作? - TheRealChx101

33

您还可以使用Spring Data JPA支持的关键字"Containing"实现类似查询。

List<Registration> findByPlaceContaining(String place);

10

试试这个。

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

使用 something=:placeanother like %:place% 在查询中会很有帮助。 - hong4rc

7
你可以选择使用占位符的一种替代方式,如下所示:
@Query("Select c from Registration c where c.place LIKE  %?1%")
List<Registration> findPlaceContainingKeywordAnywhere(String place);

6
我使用这个:
@Query("Select c from Registration c where lower(c.place) like lower(concat('%', concat(:place, '%')))")

lower()类似于String中的toLowerCase,因此结果不区分大小写。


我使用相同的方法,但当较低函数中的变量为空时,我会收到以下错误,如何添加空值检查? 函数lower(bytea)不存在。 - arslanbenzer

3
当调用函数时,我使用以下代码之一: findByPlaceContaining("%" + place); 或者: findByPlaceContaining(place + "%"); 或者: findByPlaceContaining("%" + place + "%"); 这些代码涉及到IT技术。

2
我们可以使用本地查询。
@Query(nativeQuery = true, value ="Select * from Registration as c where c.place like %:place%")

List<Registration> findByPlaceContaining(@Param("place")String place);

2

我必须使用类似于这样的内容 CONCAT('%',:setName,'%')


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接