JPA QueryMethod
JpaRepository 인터페이스를 상속받는 레파지토리 인터페이스에서 쿼리메서드를 생성할 수 있다.
📑UserRepository
public interface UserRepository extends JpaRepository<User,Long> {
...
}
💡메서드 이름으로 쿼리 생성
네이밍 규칙 > 코드의 가독성이 중요하다.
검색 관련 메서드 명 | |
findByEmail(String email) | |
getByEmail(String email) | |
readByEmail(String email) | |
searchByEmail(String email) | |
findFirst2ByEmail(String email) | 처음 부터 2개 까지 검색 |
findTop2ByEmail(String email) | 처음 부터 2개 까지 검색 |
findByEmailAndName(String email, String name) | A와 B 조건을 모두 충족하는 결과 검색 |
findByEmailOrName(String email, String name) | A 또는 B 조건을 충족하는 결과 검색 |
findByCreatedAtAfter(LocalDateTime yesterday) | 시간에 대한 조건 | 특정 날짜보다 큰 것을 조회 |
findByCreatedAtBefore(LocalDateTime yesterday) | 시간에 대한 조건 | 특정 날짜보다 작은 것을 조회 |
findByIdAfter(String id) | 시간에 대한 조건 (숫자도 가능할 수 있음) |특정 숫자보다 큰 것을 조회 |
findByCreatedAtGreaterThan(LocalDateTime yesterday) | 숫자 또는 날짜 조건 | 특정 날짜보다 큰 것을 조회 |
findByCreatedAtGreaterThanEqual(LocalDateTime yesterday) | 숫자 또는 날짜 조건 | 특정 날짜보다 같거나 큰 것 조회 |
findByIdBetween(Long id1,Long id2) | 숫자 또는 날짜 조건 | 특정 범위 안에 있는 것을 조회 |
findByIdIsNotNull() | 빈값에 대한 조건 | null 인지 아닌지 확인 할 수 있다. |
findByIdIsNotEmpty() | 빈값에 대한 조건 | null 과 "" 둘 다 허용하지 않는다. 많이 사용되지 않는 구문 |
findByNameIn(List<String> names); | A 또는 B 조건을 충족하는 결과 검색 |
findByNameSrartingWith(String name) | 문자열에 대한 쿼리 Like ex) findByNameSrartingWith("검색어") > 검색어% |
findByNameEndingWith(String name) | 문자열에 대한 쿼리 Like ex) findByNameEndingWith("검색어") > %검색어 |
findByNameContains(String name) | 문자열에 대한 쿼리 Like ex) findByNameContains("검색어") > %검색어% |
findByNameLike(String name) | 문자열에 대한 쿼리 Like ex) .findByNameLike("%검색어%") |
findUserByNameIs(String name) | Is 키워드 자체가 특별한 역할을 하지는 않음. 코드의 가독성을 좋게 해주는 방안으로 제공한다. |
findUserByNameIs(String name) | |