Spring의 3 Layer Architecture

마라탕천재 ㅣ 2024. 8. 16. 05:53

1. Presentation Layer (Controller)

 

  • 요청 처리
  • 사용자와의 상호작용
  • 데이터 검증
@RestController
@RequestMapping("/api")
public class MyController {
    
    @Autowired
    private MyService myService;
    
    @GetMapping("/items")
    public List<Item> getItems() {
        return myService.getAllItems();
    }
}

 

2. Service Layer:

 

  • 비즈니스 로직 처리
  • 트랜잭션 관리
  • 서비스 간의 협력
@Service
public class MyService {
    
    @Autowired
    private MyRepository myRepository;
    
    public List<Item> getAllItems() {
        return myRepository.findAll();
    }
}

 

3. Data Access Layer (Repository)

 

  • 데이터 저장 및 조회
  • 쿼리 실행
  • 데이터베이스와의 연결 관리
@Repository
public MyRepository<Item, Long> {
}