교육 (Today I Learned)/Hanaro
[Hanaro] 63일차 / Spring Boot (Repository - Service - Controller)
Bay Im
2024. 4. 18. 09:18
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