28. Board에 쿼리 수정하기v5

송민경's avatar
Mar 18, 2024
28. Board에 쿼리 수정하기v5

1. BoardJPARepository 만들기

package shop.mtcoding.blog.board; import org.springframework.data.jpa.repository.JpaRepository; public interface BoardJPARepository extends JpaRepository<Board, Integer> { }
 

2. 단위 테스트하기

  • save_test하기
package shop.mtcoding.blog.Board; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardJPARepository; import shop.mtcoding.blog.user.User; import java.util.Optional; @DataJpaTest public class BoardJPARepositoryTest { @Autowired private BoardJPARepository boardJPARepository; // save @Test public void save_test() { // given User sessionUser = User.builder().id(1).build(); Board board = Board.builder() .title("제목5") .content("내용5") .user(sessionUser) .build(); // when boardJPARepository.save(board); // then System.out.println("save_test : "+ board.getId()); } }
notion image
 
  • findById_test하기
package shop.mtcoding.blog.Board; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardJPARepository; import shop.mtcoding.blog.user.User; import java.util.Optional; @DataJpaTest public class BoardJPARepositoryTest { @Autowired private BoardJPARepository boardJPARepository; // findById @Test public void findById_test(){ // given int id = 1; // when Optional<Board> boardOP = boardJPARepository.findById(id); if (boardOP.isPresent()){ Board board = boardOP.get(); System.out.println("findById_test : " + board.getUser()); System.out.println("findById_test : " + board.getTitle()); System.out.println("findById_test : " + board.getContent()); } // then } }
notion image
 
  • BoardJPARepository 에 findByIdJoinUser() 만들기
package shop.mtcoding.blog.board; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; public interface BoardJPARepository extends JpaRepository<Board, Integer> { @Query("select b from Board b join fetch b.user u where b.id = :id") Board findByIdJoinUser(@Param("id") int id); }
// findByIdJoinUser @Test public void findByIdJoinUser_test() { // given int id = 1; // when Board board = boardJPARepository.findByIdJoinUser(id); // then System.out.println("findByIdJoinUser_test" + board.getUser()); System.out.println("findByIdJoinUser_test" + board.getTitle()); System.out.println("findByIdJoinUser_test" + board.getContent()); }
notion image
notion image
 
  • findAll_test
//findAll @Test public void findAll_test() { // given Sort sort = Sort.by(Sort.Direction.DESC,"id"); // when List<Board> boardList = boardJPARepository.findAll(sort); // then System.out.println("findAll_test : " + boardList); }
notion image
 
  • deleteById_test
@Test public void deleteById_test() { // given int id = 1; // when boardJPARepository.deleteById(id); boardJPARepository.flush(); // 영속성 컨텍스트의 변경사항을 즉시 데이터베이스에 반영 // then }
notion image
 
Share article

vosw1