스프링 DB 2편(데이터 접근 활용 기술)

Ch06. SpringDataJPA - 스프링 데이터 JPA 주요 기능

webmaster 2022. 7. 1. 10:32
728x90

SpringData : https://spring.io/projects/spring-data

 

Spring Data

Spring Data is an umbrella project consisting of independent projects with, in principle, different release cadences. To manage the portfolio, a BOM (Bill of Materials - see this example) is published with a curated set of dependencies on the individual pr

spring.io

SpringDataJPA : https://spring.io/projects/spring-data-jpa

 

Spring Data JPA

Spring Data JPA, part of the larger Spring Data family, makes it easy to easily implement JPA based repositories. This module deals with enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use dat

spring.io

스프링 데이터 JPA는 JPA를 편리하게 사용할 수 있도록 도와주는 라이브러리이다. 수많은 편리한 기능을 제공하지만 가장 대표적인 기능은 다음과 같다.

  • 공통 인터페이스 기능
  • 쿼리 메서드 기능

공통 인터페이스 기능

  • JpaRepository 인터페이스를 통해서 기본적인 CRUD 기능 제공한다.
  • 공통화 가능한 기능이 거의 모두 포함되어 있다.
  • CrudRepository 에서 fineOne() -> findById()로 변경되었다

JpaRepository 사용법

public interface ItemRepository extends JpaRepository { 

}
  • JpaRepository 인터페이스를 인터페이스 상속 받고, 제네릭에 관리할 <엔티티, 엔티티 ID>를 주면 된다.
  • 그러면 JpaRepository 가 제공하는 기본 CRUD 기능을 모두 사용할 수 있다

SpringDataJpa가 동적프록시 기능을 사용해 구현체를 생성해서 스프링 빈으로 등록해준다.

  • JpaRepository 인터페이스만 상속받으면 스프링 데이터 JPA가 프록시 기술을 사용해서 구현 클래스를 만들어준다. 그리고 만든 구현 클래스의 인스턴스를 만들어서 스프링 빈으로 등록한다.
  • 따라서 개발자는 구현 클래스 없이 인터페이스만 만들면 기본 CRUD 기능을 사용할 수 있다.

쿼리 메서드 기능

스프링 데이터 JPA는 인터페이스에 메서드만 적어두면, 메서드 이름을 분석해서 쿼리를 자동으로 만들고 실행해주는 기능을 제공한다

public interface MemberRepository extends JpaRepository<Member, Long> {
	List<Member> findByUsernameAndAgeGreaterThan(String username, int age);
}
  • 스프링 데이터 JPA는 메서드 이름을 분석해서 필요한 JPQL을 만들고 실행해준다. 물론 JPQL은 JPA가 SQL로 번역해서 실행한다.
  • 물론 그냥 아무 이름이나 사용하는 것은 아니고 다음과 같은 규칙을 따라야 한다

스프링 데이터 JPA가 제공하는 쿼리 메소드 기능

  • 조회: find…By , read…By , query…By , get…By
    • 예:) findHelloBy 처럼 ...에 식별하기 위한 내용(설명)이 들어가도 된다.
  • COUNT: count…By 반환타입 long
  • EXISTS: exists…By 반환타입 boolean
  • 삭제: delete…By , remove…By 반환타입 long
  • DISTINCT: findDistinct , findMemberDistinctBy
  • LIMIT: findFirst3 , findFirst , findTop , findTop3

쿼리 메소드 필터 조건

스프링 데이터 JPA 공식 문서 참고 

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.querymethods.query-creation

 

Spring Data JPA - Reference Documentation

Example 109. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRepository { List findByLastname(String lastname); @Modifying @Transactional @Query("delete from User u where u.active = false") void del

docs.spring.io

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limitquery-result

 

Spring Data JPA - Reference Documentation

Example 109. Using @Transactional at query methods @Transactional(readOnly = true) interface UserRepository extends JpaRepository { List findByLastname(String lastname); @Modifying @Transactional @Query("delete from User u where u.active = false") void del

docs.spring.io

 

728x90