![](https://velog.velcdn.com/images%2Fdrv98%2Fpost%2F3c1ebc7e-622f-480a-bba7-4919689fa34c%2Fimage.png)
리파지토리 커스텀 쿼리 작성
public interface CommentRepository extends JpaRepository<Comment, Long>{
List<Comment> findByPostId(long id);
}
서비스
// postId로 댓글들을 찾기
List<CommentDto> getCommentsByPostId(long postId);
서비스 구현
@Override
public List<CommentDto> getCommentsByPostId(long postId) {
List<Comment> comments = commentRepository.findByPostId(postId);
return comments.stream().map(comment -> mapToDTO(comment)).collect(Collectors.toList());
}
컨트롤러
@GetMapping("/posts/{postId}/comments")
public List<CommentDto> getCommentsByPostID(@PathVariable long postId){
return commentService. ? (postId);
}
![](https://velog.velcdn.com/images%2Fdrv98%2Fpost%2F8994f50c-85ec-4a0c-b47c-4ea2d1dad21e%2Fimage.png)