Springboot/블로그만들기

[Springboot] 블로그 만들기 (39)_댓글 작성하기 (네이티브 쿼리 사용)

21종 2023. 12. 8. 21:07

BoardApiController

@PostMapping("/api/board/{boardId}/reply")
public ResponseDto<Integer> replySave(@RequestBody ReplySaveRequestDto replySaveRequestDto) {

    boardService.댓글쓰기(replySaveRequestDto);
    return new ResponseDto<Integer>(HttpStatus.OK.value(), 1); 
}

 

BoardService

@Transactional
public void 댓글쓰기(ReplySaveRequestDto replySaveRequestDto) {
    replyRepository.mSave(replySaveRequestDto.getUserId(), replySaveRequestDto.getBoardId(), replySaveRequestDto.getContent());
}

 

ReplyRepository

public interface ReplyRepository extends JpaRepository<Reply, Integer>{
	@Modifying
	// ReplySaveRequestDto와 순서를 맞춰야한다.
	@Query(value="INSERT INTO reply(userId, boardId, content, createDate) VALUES(?1, ?2, ?3, now())", nativeQuery = true)
	int mSave(int userId, int boardId, String content); 
}

 


참고 유튜브 (메타코딩님 강의)

https://youtu.be/FsZLNUJBKOc?si=zg3Bdc3hkGTEMgkk