[JPA] JPA 쿼리 메소드 조건식이 기억이 나지 않을 때

Mineru·2022년 9월 11일
0

JPA로 하는 프로젝트가 처음이라서 쿼리 메소드라는 존재를 몰랐다.
JPA에서 기본적으로 제공하는 findById라던가 findAll만 쓰고 나머지는 전부 JPQL을 사용하여 직접 쿼리를 작성하면 프로젝트를 진행했다.

프로젝트가 마무리가 되는 시점에 쿼리 메소드를 통해서 JOIN 문에 들어가는 내용에 대해서만 조건식을 걸지 않는다면 메소드명만으로 아주 간단하게 쿼리를 짤 수 있다는 것을 알게 되었다...

Spring Boot 공식 문서에 있는 쿼리 메소드 조건식 키워드들을 가져왔다.

한번씩 까먹거나 하면 굳이 공식 문서에 안들어가고 여기서 찾아보려고 글을 적어본다.

KeywordSampleJPQL snippet
DistinctfindDistinctByLastnameAndFirstnameselect distinct … where x.lastname = ?1 and x.firstname = ?2
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
Is, EqualsfindByFirstname,findByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNull, NullfindByAge(Is)Null… where x.age is null
IsNotNull, NotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 (parameter bound with appended %)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 (parameter bound with prepended %)
ContainingfindByFirstnameContaining… where x.firstname like ?1 (parameter bound wrapped in %)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection<Age> ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection<Age> ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstname) = UPPER(?1)
profile
Daily Coding

0개의 댓글