[StringBoot] 조회수 만들기 [28]

2022. 8. 23. 16:22·Follow Work/SpringbootBoard
728x90

SpringBoot  ·  intelliJ  · OracleDB  ·  Thymeleaf 


조회수를 만들어보자.

 

1.질문 엔티티에 조회수 컬럼추가하기

Question.java

@Column(columnDefinition = "integer default 0", nullable = false)
private int countview; /*조회수*/

 

 

2.QuestionService 수정하기

QuestionServer.java

public Question getQuestion(Integer id){
    Optional<Question> question = this.questionRepository.findById(id);

    if(question.isPresent()){

        //조회수
        Question question1 = question.get();
        question1.setCountview(question1.getCountview() + 1);
        this.questionRepository.save(question1);
        return question1;
        //조회수끝

    }else{
        throw new DataNotFoundException("question not found");
    }
}

setCountview로 조회수컬럼에 값을 변경,

question1은 값을 저장하고 다시리턴하여 기존값에서 +1 하여 조회수를 늘려나감.

 

3.question_list.html과 question_detail.html에 적용하기

question_list.html

<!--조회수-->
<td th:text="${question.countview}"></td>

question_detail.html

<!--조회수-->
<div th:text="|조회수 : ${question.countview}|" class="d-flex justify-content-end fw-bold"></div>

 

 

 

728x90

'Follow Work > SpringbootBoard' 카테고리의 다른 글

[Springboot] 이전글 다음글 만들기 [30]  (0) 2022.08.25
[StringBoot] 카테고리 만들기 [29]  (0) 2022.08.24
[StringBoot] 게시판 검색 (27)  (0) 2022.08.18
[StringBoot] 마크다운 (26)  (0) 2022.08.18
[StringBoot] 앵커 (25)  (0) 2022.08.18
'Follow Work/SpringbootBoard' 카테고리의 다른 글
  • [Springboot] 이전글 다음글 만들기 [30]
  • [StringBoot] 카테고리 만들기 [29]
  • [StringBoot] 게시판 검색 (27)
  • [StringBoot] 마크다운 (26)
ReCode.B
ReCode.B
404 Brain Not Found
  • ReCode.B
    B의 개발 공부 기록
    ReCode.B
  • 전체
    오늘
    어제
  • 공지사항

    • 개인적 기록을 위한 블로그
    • 분류 전체보기 (679)
      • STUDY (527)
        • for Developer (25)
        • WEB (24)
        • HTML5 CSS3 (22)
        • JavaScript (102)
        • jQuery (30)
        • TypeScript (1)
        • JAVA (102)
        • SpringLegacy (46)
        • SpringBoot (41)
        • Vert.x (5)
        • DB SQL (86)
        • SERVER OS (34)
        • React (4)
        • C C++ C# (2)
        • EXCEL (3)
      • TOOL (66)
        • VCS (30)
        • Eclipse (23)
        • intelliJ (7)
        • VSCODE (6)
      • Programmers (31)
        • java (10)
        • javascript (15)
        • oracle (6)
      • Follow Work (54)
        • SpringbootBoard (54)
  • 블로그 메뉴

    • 방명록
  • hELLO· Designed By정상우.v4.10.0
ReCode.B
[StringBoot] 조회수 만들기 [28]
상단으로

티스토리툴바