Joshua게시판(SSR)-10-게시글목록보기

jaegeunsong97·2023년 8월 14일
0

SSR 기반 JoshuaBlog

목록 보기
10/23

깃허브링크

노션링크

  • boardController

  • boardService

CSR은 서비스단에서 Dto 만들어서 줘야함, 하지만 SSR은 Entity를 그대로 줘도 됨

Transactional(readOnly = true) -> 변경감지 X, 고립성(REPEATABLE READ) -> 읽을 때 누가변경하면?? 이거를 무시해버리는 것(즉, 변경 전의 데이터를 읽는 것)

  • 페이징 하기

Integer : null 처리가 가능

JPA가 제공해주는 PageRequest

JPA가 알아서 서비스단에서 pageable을 받으면 페이징처리해서 리턴

boardPG인 이유 -> 왜 boardPGPS가 아님??

이유는 OSIV가 false == 이 의미는 서비스에서 컨트롤러로 넘어올 때 비영속이 된다. 따라서 DB랑 상관이 없는 상태가 된다.

하지만 만약 OSIV가 true면 boardPGPS가 된다.

서비스에서 리턴값을 받으면????????


아직까지는 영속화가 되어있어서 boardPGPS가 된다.!!!!

Page 객체 어떤상태인지 확인


더미데이터 추가

터진 이유: OSIV=false, 근데 board만 SELECT 하고 user를 SELECT 하지 못함(LAZY이기 때문에 컨트롤러에서 USER를 가져오면 OSIV가 false여서 터지는 것)

해결 1. Board객체의 user를 EAGER로 -> 한번에 가져옴
해결 2. OSIV true -> Lazy 하면 가능
해결 3. OSIV false, Lazy + 직접 JPQL 쿼리 작성

  • boardQueryRepository

join fetch를 해서 필요할 때만 같이 당겨서 가져오기

package coffee.pastry.joshuablog.model.board;

import java.util.List;

import javax.persistence.EntityManager;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class BoardQueryRepository {

     private final EntityManager em;

     public Page<Board> findAll(Pageable pageable) {
          List<Board> boardListPS = em.createQuery("select b from Board b join fetch b.user")
                    .setFirstResult(pageable.getPageNumber())
                    .setMaxResults(pageable.getPageSize())
                    .getResultList();
          return new PageImpl<>(boardListPS, pageable, boardListPS.size());
     }
}
  • boardService 변경

무조건 Lazy + join fetch 사용(마냑 EAGER로 하면 board만 필요할 때도 user를 가져오기 때문에)

  • boardController 변경

model에 담기

  • main

데이터 형태를 보고 EL 표현식으로 어떤 데이터를 꺼내와야하는지 알기

EL 표현식으로 가져오기

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%@ include file="../layout/header.jsp" %>

<div class="container my-3">
    <div class="my-board-box row">
        <c:forEach items="${boardPG.content}" var="board">
            <%-- 글 아이템 시작 --%>
            <div class="card col-lg-3 pt-2">
                <img class="card-img-top" style="height: 250px;" src="/images/python.png" alt="Card image">
                <hr/>
                <div class="card-body">
                    <div>작성자 : ${board.user.username}</div>
                    <h4 class="card-title my-text-ellipsis">${board.title}</h4>
                    <a href="/board/${board.id}" class="btn btn-primary">상세보기</a>
                </div>
            </div>
            <%-- 글 아이템 끝 --%>
        </c:forEach>
    </div>

    <ul class="pagination mt-3 d-flex justify-content-center">
        <li class="page-item disabled"><a class="page-link" href="#">Previous</a></li>
        <li class="page-item"><a class="page-link" href="#">Next</a></li>
    </ul>
</div>

<%@ include file="../layout/footer.jsp" %>

페이징 성공

profile
현재 블로그 : https://jasonsong97.tistory.com/

0개의 댓글