스프링에서 JPA를 편리하게 사용하기 위해, JPA를 Wrapping 한것
반복적인 코드들 Spring Data JPA가 대신 작성
ex)Repository 인터페이스만 작성하면, 필요한 구현은 스프링이 대신 알아서 완성해줌.
Spring Data JPA Repository 생성 ex)
public interface ProductRepository extends JpaRepository<Product, Long>{
//JpaRepository<Entity, PK type>
User findByEntityMemberVariables(Data-type variable);
//예를들어, 위와 같이 추가로 interface를 선언하려면, findByEntity다음에 대문자 로, Entity 테이블에 있는 변수중 기준으로 삼고 싶은 변수로 이름 설정해야 함.
List<User> findAllByEntityMemberVariables(Data-type variable);
}
public interface ProductRepository extends JpaRepository<Product, Long> {
// (1) 회원 ID 로 등록된 상품들 조회
List findAllByUserId(Long userId);
// (2) 상품명이 title 인 관심상품 1개 조회
Product findByTitle(String title);
// (3) 상품명에 word 가 포함된 "모든" 상품들 조회
List<Product> findAllByTitleContaining(String word);
// (4) 최저가가 fromPrice ~ toPrice 인 모든 상품들을 조회
List<Product> findAllByLpriceBetween(int fromPrice, int toPrice);
}
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods
참고..