3계층 구조

API 계층
@RestController
@RequestMapping ("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping ("/(id}")
public User getUser(@PathVariable Long id) {
return userService.findById(id);
}
}
- 역할: 사용자 인터페이스 처리
- 구성 요소: 컨트롤러, 뷰
- 기능:
- 사용자 요청 수신
- 비즈니스 로직 호출
- 결과를 사용자에게 표시
비즈니스 계층
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User findById(Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new UserNotFoundException("User not found"));
}
}
- 역할: 애플리케이션의 비즈니스 로직 처리
- 구성 요소: 서비스
- 기능:
데이터 액세스 계층
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByName(String name);
}
- 역할: 데이터 처리 및 저장
- 구성 요소: 리포지토리, 데이터베이스
- 기능:
- CRUD 작업 수행
- 데이터에 대한 직접적인 접근