1. 서비스와 트랜잭션의 개념
서비스(service)란?
컨트롤러와 리파지터리 사이에 위치하는 계층으로, 서버의 핵심 기능(비즈니스 로직)을 처리하는 순서를 총괄한다.
ex) 컨트롤러 == 웨이터 / 서비스 == 주방장 / 보조 요리사 == 리파지토리
트랜잭션(transaction)이란?
모두 성공해야 하는 일련의 과정(쪼갤 수 없는 업무 처리의 최소 단위)
+ 롤백(rollback)
트랜잭션이 실패로 돌아갈 경우 진행 초기 단계로 돌리는 것
※ 1인 2역을 하는 REST 컨트롤러
복잡한 처리 과정이 아니라서 컨트롤러 혼자 2역을 수행해도 충분한데 일반적으로 웹 서비스는 컨트롤러와 리파지터리 사이에 서비스 계층을 두어 역할 분업
2. 서비스 계층 만들기
1. 컨트롤러, 서비스, 리파지터리의 역할을 분업
com.example.firstproject.api.ArticleApiController
com.example.firstproject.service.ArticleService
2. 게시글 조회 요청 개선하기
com.example.firstproject.service.ArticleService 코드 추가
com.example.firstproject.api.ArticleApiController 코드 추가
3. 게시글 생성 요청 개선하기
com.example.firstproject.service.ArticleService
com.example.firstproject.api.ArticleApiController
4. 게시글 수정 요청 개선하기
com.example.firstproject.service.ArticleService
com.example.firstproject.api.ArticleApiController
4. 게시글 삭제 요청 개선하기
com.example.firstproject.service.ArticleService
com.example.firstproject.api.ArticleApiController
3. 트랜잭션
com.example.firstproject.service.ArticleService
com.example.firstproject.api.ArticleApiController
'Java > Spring Boot' 카테고리의 다른 글
[Day 14] 댓글 엔티티와 리파지터리 만들기 (0) | 2024.08.10 |
---|---|
[Day 13] 테스트 코드 작성하기 (0) | 2024.08.10 |
[Day 11] HTTP와 REST 컨트롤러 (0) | 2024.08.07 |
[Day 10] REST API와 JSON (0) | 2024.08.07 |
[Day 9] CRUD와 SQL 쿼리 종합 (0) | 2024.08.06 |