Java & Spring/Spring

25일차 - Spring강의(3 Layer Architecture)

DJ.Kang 2024. 8. 19. 20:03

□ 3 Layer Architecture

  1. Controller
    • 클라이언트의 요청을 받음
    • 요청에 대한 로직 처리는 Service에게 전담
    • Service에서 처리 완료된 결과를 클라이언트에게 응답
  2. Service
    • 사용자의 요구사항을 처리(비즈니스 로직)하는 실세
    • DB저장 및 조회가 필요할 때는 Repository에게 요청
  3. Repository
    • DB관리(연결, 해제, 자원관리)
    • DB CRUD작업을 처리

□ Controller에서 Service분리해보기

- 분리 전(Controller)

 

- 분리 후(Controller, Service)

  1. service패키지에 Service클래스를 생성
  2. Service클래스에 비즈니스로직 구현(Controller의 비즈니스로직 분리)
  3. Controller에서 Service객체를 생성하고 메서드 호출
  4. 해당 메서드는 Controller에서 받는 입력값을 매개변수로 받는다.

□ Service에서 Repository분리해보기

- 분리 전(Service)

 

- 분리 후(Service, Repository)

  1. Service클래스의 비즈니스 로직 중 데이터베이스와 연동하는 부분을 Repository클래스로 옮긴다.
  2. entity, dto변환부분은 Repository영역이아니므로 남겨둔다.