복붙노트

[SPRING] 대문자와 소문자를 구별하지 않는 Spring CrudRepository의 쿼리

SPRING

대문자와 소문자를 구별하지 않는 Spring CrudRepository의 쿼리

봄 CrudRepository 쿼리; "name"속성을 가진 "DeviceType"엔티티를 선택하고 싶습니다. 그러나 다음 쿼리는 대소 문자를 구분하여 자격을 선택합니다. 어떻게 대소 문자를 구분하지 않습니까? 감사.

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

    public Iterable<DeviceType> findByNameContaining(String name);

}  

해결법

  1. ==============================

    1.@Peter가 주석에서 언급 한 것과 마찬가지로 IgnoreCase를 추가하면됩니다.

    @Peter가 주석에서 언급 한 것과 마찬가지로 IgnoreCase를 추가하면됩니다.

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

    메소드 이름 내에 지원되는 모든 키워드 목록은 documentation을 참조하십시오.

  2. ==============================

    2.다음 스프링 데이터 몽고 쿼리가 저에게 효과적입니다. Iterator 대신 List 사용하는 것을 선호합니다.

    다음 스프링 데이터 몽고 쿼리가 저에게 효과적입니다. Iterator 대신 List 사용하는 것을 선호합니다.

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

    3.제 경우에는 IgnoreCase를 추가해도 전혀 작동하지 않았습니다.

    제 경우에는 IgnoreCase를 추가해도 전혀 작동하지 않았습니다.

    정규 표현식에 대한 옵션을 제공하는 것이 가능하다는 것도 발견했습니다.

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

    i 옵션은 대소 문자를 구분하지 않습니다.

  4. ==============================

    4.사용자 정의 JPA 쿼리를 사용하는 경우에는 상위 키워드와 toUpperCase가 도움이됩니다. 다음 코드는 나를 위해 작동합니다.

    사용자 정의 JPA 쿼리를 사용하는 경우에는 상위 키워드와 toUpperCase가 도움이됩니다. 다음 코드는 나를 위해 작동합니다.

     return  entityManager.createQuery("select q from "table " q  where upper(q.applicant)=:applicant")
        .setParameter("applicant",applicant.toUpperCase().trim()).getSingleResult();
    
  5. from https://stackoverflow.com/questions/22573428/case-insensitive-query-with-spring-crudrepository by cc-by-sa and MIT license