3계층 구조

Untitled

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);
}