Back-end/Spring Boot

[Spring Boot] Repository - Service - Controller

Bay Im 2024. 4. 21. 14:11

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;
    • 메소드 생성
      • 어노테이션 주입 (모든 메소드에 넣어야 한다!)
        • @Transactional
      • return 값이 없다면 void로, return 값이 있다면 해당 데이터 타입으로 메소드 생성
  • Controller 클래스 생성
    • 어노테이션 주입
      • @Controller 또는 @RestController
      • @RequiredArgsConstructor
      • 선택: @RequestMapping(”경로”)
        • 기본 URL 지정하는 어노테이션
    • Service 생성자 주입
      • final private 서비스명 서비스명;
        • ex) final private ReplyService replyService;
    • 메소드 생성
      • 어노테이션 주입
        • @POST/GET/DELETE/PUTMapping (”경로”)
        • 선택: @ReponseBody
          • REST API 구현시 추가
728x90

'Back-end > Spring Boot' 카테고리의 다른 글

[Spring Boot] REST API 생성 순서  (3) 2025.01.23
[Spring Boot] AWS S3를 사용한 이미지 업로드 방법  (0) 2024.06.27
[Spring Boot] Scheduler  (0) 2024.04.13
[Spring Boot] TDD  (0) 2024.04.13
[Spring Boot] MyBatis  (0) 2024.04.13