JPA (Repository - Service - Controller)
Repository - Service - Controller 순으로 JPA 작성하기
- Repository 인터페이스 생성
- 어노테이션 주입
- @Repository
- JpaRepository<엔티티명, Long> 상속받기 (extends)
- 기본 함수
- findAll
- findById
- save (update, insert)
- delete
- 사용자 생성 함수
- 사용자가 조합하여 생성 가능
- 예시
- List<Reply> findAllByReplyBoardIdx(Long replyBoradIdx);
- void deleteById(Long replyIdx);
- 어노테이션 주입
- Service 클래스 생성
- 어노테이션 주입
- @Service
- @RequiredArgsConstructor
- Repository 생성자 주입
- final private 레포지토리명 레포지토리명
- ex) final private ReplyRepository replyRepository;
- final private 레포지토리명 레포지토리명
- 메소드 생성
- 어노테이션 주입 (모든 메소드에 넣어야 한다!)
- @Transactional
- return 값이 없다면 void로, return 값이 있다면 해당 데이터 타입으로 메소드 생성
- 어노테이션 주입 (모든 메소드에 넣어야 한다!)
- 어노테이션 주입
- Controller 클래스 생성
- 어노테이션 주입
- @Controller 또는 @RestController
- @RequiredArgsConstructor
- 선택: @RequestMapping(”경로”)
- 기본 URL 지정하는 어노테이션
- Service 생성자 주입
- final private 서비스명 서비스명;
- ex) final private ReplyService replyService;
- final private 서비스명 서비스명;
- 메소드 생성
- 어노테이션 주입
- @POST/GET/DELETE/PUTMapping (”경로”)
- 선택: @ReponseBody
- REST API 구현시 추가
- 어노테이션 주입
- 어노테이션 주입
728x90
'교육 (Today I Learned) > Hanaro' 카테고리의 다른 글
[Hanaro] 65일차 / 백엔드 실습 과제 (0) | 2024.04.21 |
---|---|
[Hanaro] 64일차 / 백엔드 실습 과제 (0) | 2024.04.18 |
[Hanaro] 62일차 / REST API(CRUD), JSP (0) | 2024.04.18 |
[Hanaro] 61일차 / Spring Boot (Test, Scheduler) (0) | 2024.04.13 |
[Hanaro] 60일차 / Spring Boot (JPA, HttpSession, Logging) (0) | 2024.04.13 |