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

Cha*_*nna 90 spring spring-data-jpa

使用Spring CrudRepository Query; 我想用"name"属性选择"DeviceType"实体.但是以下查询以区分大小写的方式选择权限.我如何使它不区分大小写.谢谢.

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

    public Iterable<DeviceType> findByNameContaining(String name);

}  
Run Code Online (Sandbox Code Playgroud)

Roa*_*ner 175

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

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

    public Iterable<DeviceType> findByNameContainingIgnoreCase(String name);
}  
Run Code Online (Sandbox Code Playgroud)

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

  • 它对我有用。我正在使用 JPA 查询。所以根据文档, UPPER(x.firstame) = UPPER(?1) 在哪里工作。 (3认同)
  • 对于多个属性,像这样重复 `IgnoreCase`:`List&lt;Account&gt; findByUsernameIgnoreCaseAndDomainIgnoreCase(String username, String domain);` (3认同)

小智 9

以下Spring数据mongo查询适用于我.我宁愿用List而不是Iterator

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