Skip to Content
Suffering builds character
아카이브19.spring Data특징3.Repository 인터페이스

3.Repository 인터페이스

1.Repository 인터페이스

Spring Data는 다음과 같은 Repository 인터페이스를 정의하여 제공하고 있음

Repository.java
package org.springframework.data.repository; import org.springframework.stereotype.Indexed; @Indexed public interface Repository<T, ID> { // 마커용 인터페이스, 비어있음 }
Note

제네릭 타입 파라미터

Repository 인터페이스는 Generic 문법을 기반으로 두 개의 타입 파라미터를 정의하고 있음

  1. T - 해당 엔티티의 타입 그 자체, ex. Person
  2. ID - 해당 엔티티가 가진 id 필드의 타입, Person 엔티티의 @Id 필드의 타입

2.Repository 인터페이스의 목적

Central repository marker interface. Captures the domain type to manage as well as the domain type’s id type. General purpose is to hold type information as well as being able to discover interfaces that extend this one during classpath scanning for easy Spring bean creation.

“최상위 타입을 동일하게 맞춰서 해당 타입을 확장하는 인터페이스들을 빠르게 발견하여 스프링 빈을 쉽게 생성하기 위한 마커용도의 인터페이스”

→ 별도의 추상 메서드나 상수를 포함하지 않고, 실행 시점에 객체의 타입 정보만을 명시하는 Marker 역할의 인터페이스

spring-data-commons 모듈에 포함되어 있음

개발자가 실제로 활용하는 맥락에서는,
데이터 접근 계층 구현을 위해 필요한 반복되는 코드 작성(ex. 기본적인 CRUD 로직)을 최대한 간소화 시켜서 실질적으로 최소한으로 필요한 코드만 작성하면 나머지는 알아서 동작 가능하도록 지원

3.개발자가 해야할 일

개발자가 해야 할 일은 다음과 같음

  1. 자신의 기능 구현에 활용되는 Model 클래스에 적합한 별도의 Repository 인터페이스를 정의

  2. 기능 구현에 필요한 메서드들을 정의(ex. 쿼리 메서드)
    → 인터페이스만 규칙에 맞게 잘 작성하면 실제 구현체는 스프링에서 자동으로 제공

Last updated on