목차

    불편함을 코드로 지웁니다.

    개발 공부 노트, 개발 도구와 문제를 해결한 과정, AI 프롬프트와 Obsidian 활용법을 정리합니다.

      ⏳ Time Log/TIL · 하루

      [TIL] Day 69 — Querydsl 동적 쿼리 + 쇼핑몰 상품 관리 (1/26)

      this.Serena 2026. 6. 8. 21:48

      [TIL] Day 69 — Querydsl 동적 쿼리 + 쇼핑몰 상품 관리 (1/26)

      날짜: 2026-01-26
      기술 스택: Spring Boot JPA Querydsl Thymeleaf


      한 줄 요약: 오전 팀프로젝트 1차 발표(UseCase/ERD), 오후 Querydsl로 타입 세이프한 동적 쿼리 작성 + 쇼핑몰 상품 관리 기능 구현


      팀프로젝트 1차 발표

      • UseCase 다이어그램 + ERD 포함하여 발표
      • 다음 발표 때는 DB 클래스 다이어그램 추가 예정
      • 이후 일정: 2월 생성형 AI 진도 → 3월 9일 2차 발표 → 3월 16일~ 3차 프로젝트

      Querydsl — 타입 세이프 동적 쿼리

      JPQL을 문자열이 아닌 Java 코드로 작성 → 컴파일 타임에 오류 감지 가능

      의존성 추가 (Gradle)

      implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
      annotationProcessor 'com.querydsl:querydsl-apt:5.0.0:jakarta'
      annotationProcessor 'jakarta.annotation:jakarta.annotation-api'
      annotationProcessor 'jakarta.persistence:jakarta.persistence-api'

      빌드 시 Q클래스 자동 생성 (e.g., QItem, QMember)

      ItemRepositoryCustom + 동적 쿼리

      @Repository
      public class ItemRepositoryCustomImpl implements ItemRepositoryCustom {
      
          private final JPAQueryFactory queryFactory;
      
          public ItemRepositoryCustomImpl(EntityManager em) {
              this.queryFactory = new JPAQueryFactory(em);
          }
      
          @Override
          public List<Item> getAdminItemPage(ItemSearchDto dto, Pageable pageable) {
              QItem item = QItem.item;
              return queryFactory
                  .selectFrom(item)
                  .where(
                      itemSellStatusEq(dto.getItemSellStatus()), // null이면 조건 무시
                      searchByLike(dto.getSearchBy(), dto.getSearchQuery())
                  )
                  .orderBy(item.id.desc())
                  .offset(pageable.getOffset())
                  .limit(pageable.getPageSize())
                  .fetch();
          }
      
          // null 반환 시 where 절에서 자동 제외
          private BooleanExpression itemSellStatusEq(ItemSellStatus status) {
              return status == null ? null : QItem.item.itemSellStatus.eq(status);
          }
      }

      Querydsl 핵심 장점

      • 조건절 null 반환 시 자동으로 해당 조건 제외 → 깔끔한 동적 쿼리
      • IDE 자동완성 + 컴파일 오류 감지로 JPQL 오타 방지

      더 알아볼 것

      • BooleanBuilder vs BooleanExpression — 복잡한 조건 조합 시 차이
      • Querydsl fetchResults()가 deprecated된 이유와 대안
      • UseCase 다이어그램 vs 시퀀스 다이어그램 — 언제 어떤 걸 써야 하는가
      • ERD에서 식별 관계 vs 비식별 관계 설계 기준
      • Spring Data JPA Specification vs Querydsl 비교