본문 바로가기

공부 정리/DB

Spring Data Repository Interface

728x90
반응형

 Spring Data는 Spring 프로젝트 중 데이터 접근에 관련된 프로젝트이다. 기본적인 데이터 저장소의 특성을 유지하면서 데이터 접근을 위해 친숙하고 일관된 스프링 기반 프로그래밍 모델을 제공하는 것이 목표이다. 대표적으로 Spring Data Commons, Spring Data JPA가 있으며 해당 내용을 알아보도록 한다.

 

Repository

데이터베이스 마커 인터페이스로, 타입과 id의 타입을 설정한다. ( 마커 인터페이스 )

Repository

 

CrudRepository

CRUD 메서드를 제공한다. 생성, 읽기, 삭제 메서드가 제공된다.

CrudRepository

 

<S extends T> S save(S entity);
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
Optional<T> findById(ID id);
boolean existsById(ID id);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> ids);
long count();
void deleteById(ID id);
void delete(T entity);
void deleteAll(Iterable<? extends T> entities);
void deleteAll();

 

PagingAndSortingRepository

페이징과 정렬 메서드를 제공한다.

PagingAndSortingRepository

 

JpaRepository

JPA 관련 메서드를 제공한다 ( CrudRepository에 없는 batch 삭제 혹은 영속성 flush 관련 기능 제공 )

JpaRepository

 

List<T> findAll();
List<T> findAll(Sort sort);
List<T> findAllById(Iterable<ID> ids);
<S extends T> List<S> saveAll(Iterable<S> entities);
<S extends T> List<S> findAll(Example<S> example);
<S extends T> List<S> findAll(Example<S> example, Sort sort);
<S extends T> S saveAndFlush(S entity);
void deleteInBatch(Iterable<T> entities);
void deleteAllInBatch();
void flush();
T getOne(ID id);

 

SimpleRepository

CrudRepository의 기본 구현체이다. EntityManager 필드를 사용하여 실제로 데이터 영속성을 다룬다. 모든 저장소에 적용되는 공통 메서드를 추가하고 싶으면 해당 클래스를 상속하여 메서드를 구현할 수 있다.

( 새로운 메서드 만들기 https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-adding-custom-methods-into-all-repositories/ )

SimpleRepository

 

*CrudRepository vs JpaRepository

CrudRepository JpaRepository
페이징정렬 관련 메서드가 없다 페이징정렬 관련 메서드가 있다
CRUD 관련된 메서드만 제공한다 CRUD 이외에 flush, Batch와 관련된 메서드가 있다

 

 

Spring Data JPA를 사용한다면, 어플리케이션의 데이터베이스 구조는 아래와 같다.

Spring Data JPA : Spring Data 저장소 인터페이스를 상속하여 JPA 저장소 생성을 지원한다 (JpaRepository)

Spring Data Commons : Spring Data project 데이터베이스로 공유되는 기반구조를 제공한다

(Repository, CrudRepository, PagingAndSortingRepository)
Spring JPA Provider : JPA API 구현체(Hibernate)

 

 

출처

https://www.javatpoint.com/spring-boot-crud-operations

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

https://stackoverflow.com/questions/16148188/whats-the-difference-between-jpa-and-spring-data-jpa

 

 

728x90
반응형

'공부 정리 > DB' 카테고리의 다른 글

Oracle DML GRANT, REVOKE 사용하기  (0) 2021.09.01
Query Method  (0) 2021.08.29
SQL select 쿼리 문법 순서  (0) 2021.08.28
CDATA란?  (0) 2021.08.27
oracle ROWNUM & ROW_NUMBER  (0) 2021.08.27