使用 Spring CrudRepository 进行不区分大小写的查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22573428/
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-09-08 06:37:19  来源:igfitidea点击:

Case insensitive Query with Spring CrudRepository

springspring-data-jpa

提问by Channa

With Spring CrudRepository Query; I want to select "DeviceType" entities with it's "name" property. But following query select the entitles on case sensitive manner. How I make it case insensitive way. Thanks.

使用 Spring CrudRepository 查询;我想选择具有“名称”属性的“DeviceType”实体。但是以下查询选择区分大小写的权利。我如何使它不区分大小写。谢谢。

public interface DeviceTypeRepository extends CrudRepository<DeviceType, Integer>, JpaSpecificationExecutor<DeviceType> {

    public Iterable<DeviceType> findByNameContaining(String name);

}  

回答by Roadrunner

Exactly as @Peter mentioned in the comment, just add IgnoreCase:

正如@Peter 在评论中提到的那样,只需添加IgnoreCase

public interface DeviceTypeRepository 
    extends CrudRepository<DeviceType, Integer>, JpaSpecificationExecutor<DeviceType> {

    public Iterable<DeviceType> findByNameContainingIgnoreCase(String name);
}  

See documentationfor a list of all supported keywords inside method names.

有关方法名称中所有受支持关键字的列表,请参阅文档

回答by Channa

The following Spring data mongo query works for me. I would prefer to use Listinstead of Iterator

以下 Spring 数据 mongo 查询对我有用。我宁愿使用List而不是Iterator

public interface DeviceTypeRepository extends CrudRepository<DeviceType,Integer>, JpaSpecificationExecutor<DeviceType> {
    List<DeviceType> findByNameIgnoreCase(String name);
} 

回答by rocksteady

In my case adding IgnoreCasedid not work at all.

在我的情况下,添加IgnoreCase根本不起作用。

I found that it is possible to provide options for the regular expression ,as well:

我发现也可以为正则表达式提供选项:

@Query(value = "{'title': {$regex : ?0, $options: 'i'}}")
Foo findByTitleRegex(String regexString);

The ioption makes the query case-insensitive.

i选项使查询不区分大小写。

回答by Dapper Dan

For those who uses custom JPA query Upperkeyword and toUpperCasehelps. The following code works for me

对于那些使用自定义 JPA 查询Upper关键字和toUpperCase 的人有帮助。以下代码对我有用

 return  entityManager.createQuery("select q from "table " q  where upper(q.applicant)=:applicant")
    .setParameter("applicant",applicant.toUpperCase().trim()).getSingleResult();