티스토리 뷰

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

Query
Sample
Description
findByIdList<User> findById(ID id)id에 해당하는 user를 가져옴
findFirstByUser findFirstByOrderByLastnameAsc()Lastname column을 오름차순으로 정렬한 목록 중 첫번째 값을 찾음
findTop3ByList<User> findTop3ByLastname(String lastname, Sort sort)Lastname순으로 정렬한 목록의 top 3 값을 찾음
findAll

List<User> findAll()

List<User> findAllByOrderByLastname()

모든데이터를 가져옴

모든 데이터를 가져와서 Lastname순으로 정렬함 (OrderBy를 사용하려면 findAll뒤에 By를 붙여줘야함)

countLong countByLastname(String lastname)Lastname의 개수를 가져옴
delete

@Transactional

void delete(User user)

해당 user를 삭제함
deleteById

@Transactional

void deleteById(ID id)

해당 id를 가진 user를 삭제함
deleteAll

@Transactional

void deleteAll()

모든 데이터를 삭제함
saveUser save(User user)해당 entity를 업데이트하고 저장함
saveAllList<User> saveAll(List<User> users)모든 entities를 업데이트하고 저장함
existsByIdboolean existsById(ID id)해당 id를 가진 데이터가 있으면 true, 없으면 false를 return함


Method Name에 지원되는 keywords

Keyword
Sample
JPQL snippet
Keyword
Sample
JPQL snippet
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
IsNullfindByAgeIsNull… 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> age)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)

출처

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



댓글
최근에 올라온 글
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28