我想在 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);
}
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。
您可能希望完全删除@Query
注解,因为它看起来类似于标准查询(由 spring 数据代理自动实现);即使用单行
List<Registration> findByPlaceContaining(String place);
足够了。
您实际上根本不需要 @Query
注释。
您可以使用以下内容
@Repository("registerUserRepository")
public interface RegisterUserRepository extends JpaRepository<Registration,Long>{
List<Registration> findByPlaceIgnoreCaseContaining(String place);
}
@Query()
是必须的,对吧?使用这种方法,我将不得不使 or
实际上不是一个非常适合这种情况的解决方案:findByField1OrField2Containg(String phraseForField1, String phraseForField2)
对于您的情况,您可以直接使用 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);
下面是一些相关的方法:
像 findByPlaceLike ... where x.place like ?1 StartingWith findByPlaceStartingWith ... where x.place like ?1 (参数与附加 % 绑定) EndingWith findByPlaceEndingWith ... where x.place like ?1 (parameter bound with prepended %) 包含 findByPlaceContaining ... 其中 x。像 ?1 的地方(以 % 包裹的参数绑定)
更多信息,查看 this link 、 this link 和 this
希望对你有帮助 :)
您还可以使用 Spring Data JPA 支持的关键字 "Containing" 实现类似查询。
List<Registration> findByPlaceContaining(String place);
尝试这个。
@Query("Select c from Registration c where c.place like '%'||:place||'%'")
something=:place
和 another 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,'%')
'
写like '%:place%'
并没有任何结果,因为 Hibernate 自己将'
添加到字符串中。