-
[Spring Data JPA] 기본 사용법 정리Back-End/Spring Boot 2022. 8. 12. 03:41

전에 공부했던 Spring Data JPA의 기본 사용법을 정리하고자 한다.
✔️ Dependency
build.gradle 파일의 dependencies 부분에 다음을 추가하자.
implementation ‘org.springframework.boot:spring-boot-starter-data-jpa’✔️ 공통 인터페이스, JpaRepository 적용
Spring Data JPA는
JpaRepository라는 공통 인터페이스를 제공한다.이 인터페이스는 기본적인 CRUD와 페이징 등 유용한 기능을 제공해주며, 적용하는 방법도 간단하다.
JpaRepository<T, ID>를 상속하는 인터페이스를 구성하면 된다.T: 엔티티 타입ID: 식별자 타입
public interface EntityRepository extends JpaRepository<T, ID> {}+)
@Repository애너테이션은 생략 가능하다. 컴포넌트 스캔과 JPA 예외를 스프링 예외로 변환하는 과정을 스프링 데이터 JPA가 자동으로 처리해준다.✔️ 주요 메서드
JpaRepository가 기본적으로 제공하는 주요 메소드는 다음과 같다.
메서드 설명 <S extends T> S save(S)새로운 엔티티는 저장하고, 이미 있는 엔티티는 병합한다. delete(T)엔티티 하나를 삭제한다. Optional<T> findById(ID)ID로 엔티티 하나를 조회한다. List<T> findAll(...)모든 엔티티를 조회한다.
정렬(Sort)이나 페이징(Pageable) 조건을 파라미터로 제공할 수 있다.✔️ Query Creation
Spring Data JPA는 메서드의 이름으로 쿼리를 생성해준다.
우리는 JpaRepository를 상속한 인터페이스 안에 규칙에 맞게 메서드를 선언해주기만 하면 된다.
📌 이름 규칙
메서드 이름에 관한 규칙은 다음과 같다. (By 다음에는 필드명이 들어간다)
쿼리 종류 이름 규칙 조회 find...By,read...By,query...By,get...ByCOUNT count...By반환타입 longEXISTS exists...By반환타입 boolean삭제 delete...By,remove...ByDISTINCT findDistinct,findMemberDistinctByLIMIT findFirst3,findFirst,findTop,findTop3+ 엔티티의 필드명이 변경되면 인터페이스에 정의한 메서드 이름도 꼭 함께 변경해야 한다.
그렇지 않으면 애플리케이션을 시작하는 시점에 오류가 발생한다.📌 키워드 종류
메서드 이름에 들어가는 키워드는 다음과 같다.
Keywords Sample JPQL snippet DistinctfindDistinctByLastnameAndFirstname select 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) 쿼리 메서드의 파라미터에는 특별한 파라미터를 사용할 수도 있다.
Pageable,Slice,Sort
Page<User> findByLastname(String lastname, Pageable pageable); Slice<User> findByLastname(String lastname, Pageable pageable); List<User> findByLastname(String lastname, Sort sort); List<User> findByLastname(String lastname, Pageable pageable);자세한 내용은 공식문서를 참고하자.
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods728x90