1️⃣ Repository

 

@Repository

Spring에서 이 인터페이스를 빈으로 등록하려는 것을 나타낸다.

 

 

UserRepository Interface

  • JpaRepository<UserEntity, Long>를 확장한다.
    • UserEntity : 이 리포지토리가 관리하는 entity를 의미한다.
    • Long : 해당 entity의 기본 키 PK의 타입을 의미한다.

 

JpaRepository

Spring Data JPA의 일반적인 리포지토리 인터페이스 이다. 여러 JPA 관련 CRUD 메서드를 제공한다.

JpaRepository 인터페이스를 상속받는 인터페이스를 정의하면, 해당 인터페이스를 구현하는 클래스는 JPA에서 제공하는 메서드들을 사용할 수 있다.

즉, JpaRepository를 사용하면 복잡한 JDBC 코드를 작성하지 않아도 간단하게 DB와의 데이터 접근 작업을 처리할 수 있다.

 

 


 

2️⃣ JpaRepository 사용법

 

1. Entity 클래스 정의

JPARepository를 사용하여 액세스 할 엔티티 클래스를 정의 해야 한다.

예를 들기 위한 작성해둔 UserEntity의 모습이다.

 

 

 

2. JpaRepository 이터페이스를 상속받는 인터페이스 생성

JpaRepository 인터페이스를 상속받는다.

커스텀을 하기도 한다.

 

 

 

3. Spring Bean 등록

JpaRepository를 사용하여 데이터 액세스를 수행하는 EntityManager가 필요하므로, JpaRepository를 사용하는 클래스는 빈으로 등록되어야 한다.

 

 

 

4. JPARepository method 사용

Service 클래스에서 JpaRepository 인터페이스에 정의된 메서드를 호출하거나 구현한 메서드를 호출한다.

 

https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html

 

JpaRepository (Spring Data JPA Parent 3.2.1 API)

All Superinterfaces: CrudRepository , ListCrudRepository , ListPagingAndSortingRepository , PagingAndSortingRepository , QueryByExampleExecutor , Repository All Known Subinterfaces: EnversRevisionRepository , JpaRepositoryImplementation All Known Implement

docs.spring.io

 

 

 


 

 

 

3️⃣ Repository를 작성해보자

 

UserRepository

package com.example.demo.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.UserEntity;

@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {   // JpaRepository<Entity 클래스, PK 타입>

}

 

 

BoardRepository

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.BoardEntity;

@Repository
public interface BoardRepository extends JpaRepository<BoardEntity, Long> {

}

 

 

CommentRepository

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.CommentEntity;

@Repository
public interface CommentRepository extends JpaRepository<CommentEntity, Long> {

}

 

 

GalleryRepository

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.GalleryEntity;

@Repository
public interface GalleryRepository extends JpaRepository<GalleryEntity, Long> {

}

 

 

LikeListRepository

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.LikeListEntity;

@Repository
public interface LikeListRepository extends JpaRepository<LikeListEntity, Long> {

}

 

 

NoteRepository

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.NoteEntity;

@Repository
public interface NoteRepository extends JpaRepository<NoteEntity, Long> {

}

 

 

PuppyRepository

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.NoteEntity;

@Repository
public interface NoteRepository extends JpaRepository<NoteEntity, Long> {

}

 

 

StoreRepository

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.StoreEntity;

@Repository
public interface StoreRepository extends JpaRepository<StoreEntity, Long> {

}

 

 


 

 

4️⃣ Service를 작성해보자

 

UserService

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.repository.UserRepository;


@Service
public class UserService {
	
	@Autowired
	private UserRepository userRepository;
	
}

 

 

BoardService

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.repository.BoardRepository;


@Service
public class BoardService {
	
	@Autowired
	private BoardRepository boardRepository;
	
}

 

 

CommonService

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.repository.CommentRepository;

@Service
public class CommentService {
	
	@Autowired
	private CommentRepository commentRepository;
	
}

 

 

GalleryService

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.repository.GalleryRepository;


@Service
public class GalleryService {
	
	@Autowired
	private GalleryRepository galleryRepository;
	
}

 

 

LikeListService

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.repository.LikeListRepository;


@Service
public class LikeListService {
	
	@Autowired
	private LikeListRepository likeListRepository;
	
}

 

 

NoteService

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.repository.NoteRepository;


@Service
public class NoteService {
	
	@Autowired
	private NoteRepository noteRepository;
	
}

 

 

PuppyService

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.repository.PuppyRepository;


@Service
public class PuppyService {
	
	@Autowired
	private PuppyRepository puppyRepository;
	
}

 

 

StoreService

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.repository.StoreRepository;


@Service
public class StoreService {
	
	@Autowired
	private StoreRepository storeRepository;
	
}

'Project 댕린이집' 카테고리의 다른 글

CORS  (0) 2023.12.27
ERD 설계  (0) 2023.12.23
Entity 수정  (0) 2023.12.20
Entity 작성  (1) 2023.12.19
Controller 기본 설정  (1) 2023.12.18
xoo | 수진