๐Ÿ“š TIL 40์ผ์ฐจ

temprmnยท2023๋…„ 7์›” 27์ผ
0
post-thumbnail

QueryDSL

[Feature][Refactor] 7/27 - QueryDSL์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

1. QueryDsl ์‚ฌ์šฉ์„ ์œ„ํ•œ ์˜์กด์„ฑ ์ถ”๊ฐ€

build.gradle

dependency {
    // querydsl
    implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
    annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version'] }:jakarta"
    annotationProcessor "jakarta.annotation:jakarta.annotation-api"
    annotationProcessor "jakarta.persistence:jakarta.persistence-api"
}

2. JPAQueryFactory ์‚ฌ์šฉ์„ ์œ„ํ•œ QuerydslConfig ์ƒ์„ฑ

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

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

3. ~~CustomRepsitory ์ธํ„ฐํŽ˜์ด์Šค ๋ฐ ~~RepositoryImpl ํด๋ž˜์Šค ์ƒ์„ฑ

RepositoryImpl ํด๋ž˜์Šค๋Š” CustomRepository ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์† ๋ฐ›๋Š”๋‹ค.

PostCustomRepository.java

  • QueryDsl์„ ํ™œ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•  ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•œ๋‹ค.
public interface PostCustomRepository {
    List<Post> findAllByOrderByCreatedAtDesc();
    List<Post> findAllByUsername(String username);
    Optional<Post> findByIdAndUsername(Integer id, String username);
}

PostRepositoryImpl.java

import static com.crud.blog.entity.QPost.post;

@Repository
@RequiredArgsConstructor
public class PostRepositoryImpl implements PostCustomRepository {
    private final JPAQueryFactory jpaQueryFactory;

    @Override
    public Optional<Post> findById(Integer id) {
        Optional<Post> result = jpaQueryFactory
                .selectFrom(post)
                .where(post.id.eq(id))
                .stream().findAny();
        return result;
    }

    @Override
    public List<Post> findAllByOrderByCreatedAtDesc() {
        List<Post> result = jpaQueryFactory
                .selectFrom(post)
                .orderBy(post.createdAt.desc())
                .fetch();
        return result;
    }

    @Override
    public List<Post> findAllByUsername(String username) {
        List<Post> result = jpaQueryFactory
                .selectFrom(post)
                .where(post.username.eq(username))
                .fetch();
        return result;
    }

    @Override
    public Optional<Post> findByIdAndUsername(Integer id, String username) {
        Optional<Post> result = jpaQueryFactory
                .selectFrom(post)
                .where(post.id.eq(id).and(post.username.eq(username)))
                .stream().findAny();
        return result;
    }
}
  • ์ปดํŒŒ์ผ์„ ํ•˜๋ฉด, src > main > generate > ํŒจํ‚ค์ง€ ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑ๋œ entity์— ๋Œ€ํ•œ Qํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (settings-build-gradle-IntelliJ ์„ค์ •)
    image
  • ์—ฌ๊ธฐ์„œ ์ƒ์„ฑ๋œ Qํด๋ž˜์Šค๋ฅผ import static {Qํด๋ž˜์Šค ๊ฒฝ๋กœ} ํ˜•ํƒœ๋กœ importํ•˜๊ฒŒ ๋˜๋ฉด, ๋ณ„๋„์˜ entity๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”์—†์ด ํ•ด๋‹น property์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

ํฌ์ŠคํŠธ๋งจ ํ…Œ์ŠคํŠธ

getBlogPosts - PostRepositoryImpl.findAllByOrderByCreatedAtDesc() ์‚ฌ์šฉ

getBlogPosts

profile
`ISFJ` T 49% F 51% /

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