[SpringBoot] MovieReviewProj - 3. Repository 생성

SeonJin·2023년 9월 12일
0

Springboot

목록 보기
5/5

1. 각 Entity의 Repository 생성

주의 ! interface로 생성, JpaRepository<Entity, PK Type>

// Movie
public interface MovieRepository extends JpaRepository<Movie, Long> {
}
// MovieImage
public interface MovieImageRepository extends JpaRepository<MovieImage, Long> {
}
// Member
public interface MemberRepository extends JpaRepository<Member, Long> {
}
// Review
public interface ReviewRepository extends JpaRepository<Review, Long> {
}

2. 테스트코드 실행

1. MovieRepositoryTest (insert test)

@SpringBootTest
class MovieRepositoryTest {
    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private MovieImageRepository imageRepository;

    @Commit
    @Transactional
    @Test
    public void insertMovies() {
        IntStream.rangeClosed(1, 100).forEach(i -> { // 100개 데이터 삽입
            Movie movie = Movie.builder().title("제목.." + i).build(); // movie 데이터 삽입
            System.out.println("-------------------");
            movieRepository.save(movie); // 삽입된 데이터 저장 -> save되면 생성되는 mno값을 이용하여 movieImage 추가
            int count = (int) (Math.random() * 5) + 1; // 1~5장까지 img 삽입 가능
            for (int j = 0; j < count; j++) {
                MovieImage movieImage = MovieImage.builder()
                        .uuid(String.valueOf(UUID.randomUUID()))
                        .movie(movie)
                        .imaName("test" + j + ".jpg")
                        .build();
                imageRepository.save(movieImage);
            }
        });
    }
}

-> 영화마다 등록된 사진의 개수가 다른 것을 알 수 있음

2. MemberRepositoryTest

@SpringBootTest
class MemberRepositoryTest {
    @Autowired
    private MemberRepository memberRepository;

    @Test
    public void insertMember() {
        IntStream.rangeClosed(1, 10).forEach(i -> {
            Member member = Member.builder()
                    .email("t" + i + "@test.com")
                    .pw("1111")
                    .nickname("reviewer" + i)
                    .build();
            memberRepository.save(member);
        });
    }
}

3. ReviewRepositoryTest

@SpringBootTest
class ReviewRepositoryTest {
    @Autowired
    private ReviewRepository reviewRepository;

    @Test
    public void insertMovieReviews() {
        // 리뷰 등록
        IntStream.rangeClosed(1, 200).forEach(i -> {
            // 영화번호
            Long mno = (long)(Math.random()*100) + 1;
            // 리뷰어번호
            Long mid = ((long)(Math.random()*100) + 1);
            Member member = Member.builder().mid(mid).build();

            Review movieReview = Review.builder()
                    .member(member)
                    .movie(Movie.builder().mno(mno).build())
                    .grade((int)(Math.random()*5)+1)
                    .text("소감은,,," + i)
                    .build();

            reviewRepository.save(movieReview);
        });
    }
}
profile
study notebook

0개의 댓글