inblog logo
|
p4rksk
    JavaSpringFrameWork

    49. PC를 이용한 데이터 조회

    박선규's avatar
    박선규
    Mar 13, 2024
    49. PC를 이용한 데이터 조회
    Contents
    영속성 컨테스트를 사용한 객체 조회컨트롤러View
     

    영속성 컨테스트를 사용한 객체 조회

     
    네이티브쿼리 사용
    public Board findById(int id) { Query query = em.createNativeQuery("select * from board_tb where id =?",Board.class); query.setParameter(1,id); Board board = (Board) query.getSingleResult(); }
    Persistence Context 사용
    public Board findById(int id){ Board board = em.find(Board.class,id); return board; }
    notion image
    📌
    그림 처럼 PC에 Board 객체가 없다면 DB에서 Board 객체를 조회 한후 PC에 저장한다.
     
    notion image
    📌
    내가 원하는 객체를 조회 하려고 할 때 PC에 존재한다면 DB에서 조회하지 않고 캐싱 된다.
     
    @Test public void fintById_test(){ int id = 1; boardReposiroty.findById(id); boardReposiroty.findById(id); }
     
    notion image
     

    컨트롤러

    @GetMapping("/board/{id}") public String detail(@PathVariable Integer id,HttpServletRequest request) { // int 를 쓰면 값이 없으면 0, Integer 를 넣으면 값이 없을 때 null 값이 들어옴. Board board = boardPersistRepository.findById(id); request.setAttribute("board",board); return "board/detail";
     

    View

    <div class="container p-5"> <!-- 수정삭제버튼 --> <div class="d-flex justify-content-end"> <a href="/board/{{board.id}}/update-form" class="btn btn-warning me-1">수정</a> <form action="/board/{{board.id}}/delete" method="post"> <button class="btn btn-danger">삭제</button> </form> </div> <div class="d-flex justify-content-end"> <b>작성자</b> : {{board.username}} </div> <!-- 게시글내용 --> <div> <h2><b>{{board.title}}</b></h2> <hr /> <div class="m-4 p-2"> {{board.content}} </div> </div>
     
    notion image
    Share article

    p4rksk

    RSS·Powered by Inblog