QueryDSL 페이징 쿼리

Kim Dong Kyun·2023년 4월 17일
0
post-custom-banner

페이징 쿼리 직접 날리기 전에 무조건 exist 쿼리 날려야겠다. FromIndex, ToIndex 설정이 꼬이니까 답이 없음

@Override
public List<MovieResponseDto> getMoviesPaging(Long pageNum) {

    int pageSize = 10;

    pageNum = Math.max(pageNum, 1L);

    long offset = (pageNum - 1) * pageSize;

    offset = Math.max(offset, 0L);

    Integer one = jpaQueryFactory.selectOne()
            .from(movie)
            .where(movie.id.between(offset, offset + 10))
            .fetchOne();

    List<MovieResponseDto> nullPointHandler = new ArrayList<>();

    if (one == null){
        return nullPointHandler;
    }

    List<Movie> movieList = jpaQueryFactory
            .selectFrom(movie)
            .leftJoin(movie.movieImages, movieImage).fetchJoin()
            .leftJoin(movie.movieVideos, movieVideo).fetchJoin()
            .where(movie.inUse.eq(true))
            .offset(offset)
            .limit(pageSize)
            .fetch();

    return MovieResponseDto.toDto(movieList);
}
post-custom-banner

0개의 댓글