[Springboot] 조회 수 기능 구현하기

DANI·2023년 10월 24일
0
post-thumbnail

🔐 조회 수

현재 SBB는 답변 수를 표시하고 있지만 조회 수는 표시하지 않는다. 조회 수를 표시해 보자.

🔑 완료

💾 Question 엔티티에 조회수를 표시할 속성을 추가하자!

@Column(columnDefinition = "integer default 0", nullable = false)
private Integer view;
// null은 불가능 하며 디폴트 값은 0

💾 Questionservice getQuestion()메소드 변경

	public Question getQuestion(Integer id) {
		Optional<Question> question = this.questionRepository.findById(id);
		if(question.isPresent()) {
			
			Question question1 = question.get();
			question1.setView(question1.getView()+1);
			this.questionRepository.save(question1);
			return question.get();
		} else {
			throw new DataNotFoundException("question not found");
		}
	}

💾 question_list 템플릿에 태그 추가

<th>조회수</th>

<td th:href="@{|/question/detail/${questionlist.id}|}" th:text="${questionlist.view}"></td>

💻 실행화면

0개의 댓글