Java spring JpaRepository 中的 %Like% 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25362540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:12:35  来源:igfitidea点击:

%Like% Query in spring JpaRepository

javajpaspring-data-jpa

提问by sudeep cv

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

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

LIKE '%place%'-its not working.

LIKE '%place%'-它不工作。

LIKE 'place'works perfectly.

LIKE 'place'完美地工作。

Here is my code :

这是我的代码:

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

采纳答案by Hille

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

spring 数据 JPA 查询需要“%”字符以及like查询中的空格字符,如

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

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

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

参见 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

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

List<Registration> findByPlaceContaining(String place);

is sufficient.

足够了。

回答by Alexius DIAKOGIANNIS

You dont actually need the @Queryannotation at all.

您实际上根本不需要@Query注释。

You can just use the following

你可以只使用以下

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

    List<Registration> findByPlaceIgnoreCaseContaining(String place);

    }

回答by amanzoor

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

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

List<Registration> findByPlaceContaining(String place);

回答by Nitin Pawar

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

回答by user9290654

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

当调用函数时,我使用: findByPlaceContaining("%" + place);

or: findByPlaceContaining(place + "%");

或者: findByPlaceContaining(place + "%");

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

或者: findByPlaceContaining("%" + place + "%");

回答by Sulaymon Hursanov

answer exactly will be

答案将是

-->` @Query("select u from Category u where u.categoryName like %:input%")
     List findAllByInput(@Param("input") String input);

回答by Md. Sajedul Karim

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

对于您的情况,您可以直接使用 JPA 方法。就像下面这样:

Containing: select ... like %:place%

包含:选择 ... 像 %:place%

List<Registration> findByPlaceContainingIgnoreCase(String place);

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

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

Using @Query in JPQL:

在 JPQL 中使用 @Query

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

Here are some related methods:

下面是一些相关的方法:

  1. LikefindByPlaceLike

    … where x.place like ?1

  2. StartingWithfindByPlaceStartingWith

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

  3. EndingWithfindByPlaceEndingWith

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

  4. ContainingfindByPlaceContaining

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

  1. 喜欢findByPlaceLike

    ... 在哪里 x.place 喜欢?1

  2. 从...开始findByPlaceStartingWith

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

  3. 结尾findByPlaceEndingWith

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

  4. findByPlaceContaining

    ... where x.place like ?1(参数绑定在 % 中)

More info , view this link, this linkand this

更多信息,查看这个链接这个链接这个

Hope this will help you :)

希望能帮到你 :)

回答by Ashu

Try this.

尝试这个。

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

回答by nazar_art

Found solution without @Query(actually I tried which one which is "accepted". However, it didn't work).

没有找到解决方案@Query(实际上我尝试了哪个“接受”。但是,它没有用)。

Have to return Page<Entity>instead of List<Entity>:

必须返回Page<Entity>而不是List<Entity>

public interface EmployeeRepository 
                          extends PagingAndSortingRepository<Employee, Integer> {
    Page<Employee> findAllByNameIgnoreCaseStartsWith(String name, Pageable pageable);
}

IgnoreCasepart was critical for achieving this!

IgnoreCase部分是实现这一目标的关键!