Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

끄적끄적

[jpa] querydsl 사용하기 본문

개발

[jpa] querydsl 사용하기

으아아아앜 2020. 3. 9. 16:54

- pom.xml 

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
    <version>4.1.4</version>
</dependency>
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>4.1.4</version>
    <scope>provided</scope>
</dependency>
 
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>


 

QClass들을 생성하기 위해서 "mvn package" 실행(인텔리제이는 ctr 두번 입력후 커맨드창에 입력)

 

-@Configuration

@Configuration
public class QuerydslConfig {
    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory(){
        return new JPAQueryFactory(entityManager);
    }

}

 

- Querydsl Repository 생성

public interface CommentRepositoryCustom {
    List<Comment> getCommentList(Board board);
    Optional<Comment> getChildComment(Board board, Long group, Long commentId);
    List<Comment> getLikedCommentList(Board board, Account account);
}
@RequiredArgsConstructor
public class CommentRepositoryCustomImpl implements CommentRepositoryCustom {

    private final JPAQueryFactory queryFactory;

    @Override
    public List<Comment> getCommentList(Board board) {
        return queryFactory
                .selectFrom(comment)
                .where(comment.board.eq(board))
                .orderBy(comment.group.asc(),comment.regDate.asc())
                .fetch();
    }

    @Override
    public Optional<Comment> getChildComment(Board board, Long group, Long commentId) {
        return Optional.ofNullable(
                queryFactory
                .selectFrom(comment)
                .where(comment.board.eq(board),comment.group.eq(group),comment.commentId.gt(commentId))
                .orderBy(comment.regDate.asc())
                .fetchFirst());
    }

    @Override
    public List<Comment> getLikedCommentList(Board board, Account account) {
        return queryFactory
                .selectFrom(comment)
                .where(comment.board.eq(board))
                .join(likeComment).on(likeComment.comment.commentId.eq(comment.commentId))
                .where(likeComment.account.eq(account))
                .fetch();
    }
}

- Repository 통합하기

public interface CommentRepository extends JpaRepository<Comment,Long>,CommentRepositoryCustom {

}
  • Querydsl Repository를 구현하는 구현체는 반드시 xxxxImpl 이라는 명칭으로 끝나는 명명규칙을 반드시 지켜야함

 

 

 

gradle

- 참고 : https://jojoldu.tistory.com/372

Comments