๐Ÿ”ฅ TIL - Day 68 Querydsl ์ •๋ ฌ์กฐ๊ฑด ๋™์ ์œผ๋กœ ์ ์šฉํ•˜๊ธฐ (QuerydslRepositorySupport)

Kim Dae Hyunยท2021๋…„ 11์›” 30์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
79/93

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ์ค‘์— ์ •๋ ฌ์กฐ๊ฑด์œผ๋กœ ํ•„๋“œ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ์š”๊ตฌ์‚ฌํ•ญ์ด ์žˆ์—ˆ๋‹ค.
ex) ๋‚ ์งœ๋ณ„, ์ˆœ์œ„๋ณ„, ์ ์ˆ˜๋ณ„ ... ์ •๋ ฌ

๐Ÿ“Œ Code

@Slf4j
public class ReviewRequestDaoImpl extends QuerydslRepositorySupport implements ReviewRequestDao {
    private final EntityManager em;
    private final JPAQueryFactory query;

    public ReviewRequestDaoImpl(JPAQueryFactory jpaQueryFactory, EntityManager entityManager) {
        super(ReviewRequest.class);
        this.em = entityManager;
        this.query = jpaQueryFactory;
    }
    
    @Override
    public Page<ReviewAnswerResponseDto> findMyAnswer(Long userId, Pageable pageable) {
        JPAQuery<ReviewAnswerResponseDto> jpaQuery = query.select(
                        new QReviewAnswerResponseDto(
                                reviewRequest.reviewAnswer.id,
                                reviewRequest.id,
                                reviewRequest.reviewAnswer.title,
                                reviewRequest.reviewAnswer.code,
                                reviewRequest.reviewAnswer.comment,
                                reviewRequest.reviewAnswer.point,
                                reviewRequest.reviewAnswer.createdAt
                        )
                ).from(reviewRequest)
                .join(reviewRequest.reviewAnswer, reviewAnswer)
                .where(reviewRequest.reviewAnswer.answerUser.id.eq(userId));

        List<ReviewAnswerResponseDto> result = getQuerydsl().applyPagination(pageable, jpaQuery).fetch();

        return PageableExecutionUtils.getPage(result, pageable, jpaQuery::fetchCount);
    }    
}

QuerydslRepositorySupport๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•œ๋‹ค.
์ด ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ getQuerydsl(), applyPagination ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

QuerydslRepositorySupport๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์—๋Š” ์ง์ ‘ limit, offset, order ๋“ฑ์„ ์„ค์ •ํ–ˆ๋‹ค. ์ง์ ‘ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋„ ํฌ๊ฒŒ ๋ฒˆ๊ฑฐ๋กœ์šด ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ Order์˜ ์ ์šฉ ํ•„๋“œ๋ฅผ ๋™์ ์œผ๋กœ ์ ์šฉ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ข€ ๋ณต์žกํ•˜๋‹ค.

์ฝ”๋“œ๊ฐ€ ๊ต‰์žฅํžˆ ๊น”๋”ํ•ด์„œ ๊ธฐ์กด์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋น„๊ตํ–ˆ์„ ๋•Œ ์„ฑ๋Šฅ์ƒ ์ด์Šˆ๊ฐ€ ์—†๋‹ค๋ฉด ์ด ๋ฐฉ์‹์„ ์ž์ฃผ ์‚ฌ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค.

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€