簡體   English   中英

spring JpaRepository 中的 %Like% 查詢

[英]%Like% Query in spring JpaRepository

我想在JpaRepository中寫一個類似的查詢,但它沒有返回任何內容:

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

春季數據 JPA 查詢需要 "%" 字符以及like在您的查詢中一樣的空格字符,如

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

參照。 http://docs.spring.io/spring-data/jpa/docs/current/reference/html

您可能希望完全擺脫@Query注釋,因為它似乎類似於標准查詢(由 spring 數據代理自動實現); 即使用單行

List<Registration> findByPlaceContaining(String place);

足夠了。

您實際上根本不需要@Query注釋。

您可以使用以下內容

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

    }

對於您的情況,您可以直接使用 JPA 方法。 該代碼如下所示:

包含:選擇 ... 像 %: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. 喜歡findByPlaceLike

    … x.place 像 ?1

  2. 開始findByPlaceStartingWith

    ...其中 x.place 像 ?1 (與附加 % 綁定的參數)

  3. EndingWith findByPlaceEndingWith

    ......其中 x.place 像 ?1 (參數與前置 % 綁定)

  4. 包含findByPlaceContaining

    …其中 x.place 像 ?1 (以 % 包裹的參數綁定)

更多信息,查看這個鏈接這個鏈接這個

希望對你有幫助 :)

您還可以使用 Spring Data JPA 支持的關鍵字"Containing"來實現類似的查詢。

List<Registration> findByPlaceContaining(String place);

嘗試這個。

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

您可以使用占位符的另一種方法:

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

調用函數時,我使用: findByPlaceContaining("%" + place);

或: findByPlaceContaining(place + "%");

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

我用這個:

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

lower() 類似於 String 中的 toLowerCase,因此結果不區分大小寫。

答案將是

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

我們可以使用原生查詢

@Query(nativeQuery = true, value ="Select * from Registration as c where c.place like %:place%")

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

找到了沒有@Query解決方案(實際上我嘗試了哪個是“接受”的。但是,它沒有用)。

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

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

IgnoreCase部分對於實現這一目標至關重要!

您可以在參數后簡單地說“Like”關鍵字..

List<Employee> findAllByNameLike(String name);

我們這樣

@Query("來自 CasFhgDeviceView where deviceGroupName like concat(concat('%CSGW%', :usid), '%') ")

可以有各種方法。 正如許多人的回答中提到的,如果可能的話,您可以使用 JPA 預定義的模板查詢。

List<Registration> findByPlaceContainingIgnoreCase(String place);

此外,您可以在調用上述方法之前在 java 層中附加 '%'。

如果查詢復雜,那么你通常可以使用@Query 之一

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

為了可讀性,您可以使用以下一種

@Query("Select r from Registration r where r.place like CONCAT('%', :place, '%'")

我不得不使用這樣的東西CONCAT('%',:setName,'%')

現在可以使用Spring 數據 JPA 來實現 查看http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example

Registration registration = new Registration();
registration.setPlace("UK");

ExampleMatcher matcher = ExampleMatcher.matchingAll()
  .withIgnoreCase()
  .withStringMatcher(StringMatcher.CONTAINING);

Example<Registration> example = Example.of(registration, matcher);

List<Registration> registrationList = registerUserRepository.findAll(example);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM