Back-End/Spring

[JPA] CRUD Repositories Query Method

HMHA 2023. 2. 10. 11:01
300x250
반응형

Method Name으로 데이터베이스 쿼리 만들기

QuerySampleDescription

findById List<User> findById(ID id) id에 해당하는 user를 가져옴
findFirstBy User findFirstByOrderByLastnameAsc() Lastname column을 오름차순으로 정렬한 목록 중 첫번째 값을 찾음
findTop3By List<User> findTop3ByLastname(String lastname, Sort sort) Lastname순으로 정렬한 목록의 top 3 값을 찾음
findAll List<User> findAll()
List<User> findAllByOrderByLastname()
모든데이터를 가져옴
모든 데이터를 가져와서 Lastname순으로 정렬함 (OrderBy를 사용하려면 findAll뒤에 By를 붙여줘야함)
count Long countByLastname(String lastname) Lastname의 개수를 가져옴
delete @Transactional
void delete(User user)
해당 user를 삭제함
deleteById @Transactional
void deleteById(ID id)
해당 id를 가진 user를 삭제함
deleteAll @Transactional
void deleteAll()
모든 데이터를 삭제함
save User save(User user) 해당 entity를 업데이트하고 저장함
saveAll List<User> saveAll(List<User> users) 모든 entities를 업데이트하고 저장함
existsById boolean existsById(ID id) 해당 id를 가진 데이터가 있으면 true, 없으면 false를 return함

 

Method Name에 지원되는 keywords

KeywordSampleJPQL snippetKeywordSampleJPQL snippet
And findByLastnameAndFirstname ... where x.lastname = ?1 and x.firstname =?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstname,findByFirstnameIs,findByFirstnameEquals … where x.firstname = 1?
Between findByStartDateBetween … where x.startDate between 1? and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age <= ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection<Age> ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection<Age> age) … where x.age not in ?1
True findByActiveTrue() … where x.active = true
False findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

출처 : https://gguldh.tistory.com/18?category=990427 

 

CRUD Repositories Query Method

Method Name으로 데이터베이스 쿼리 만들기 QuerySampleDescriptionfindByIdList findById(ID id)id에 해당하는 user를 가져옴findFirstByUser findFirstByOrderByLastnameAsc()Lastname column을 오름차순으로 정렬한 목록 중 첫번째

gguldh.tistory.com

 

원글 출처 :

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html

https://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/jpa.repositories.html#jpa.query-methods

 
300x250
반응형