Java/SpringBoot 게시판 기능 구현_Querydsl을 활용한 카테고리별 조회, 필터링

2023. 10. 19. 20:00· Spring & SpringBoot
💡 개발 환경
Java 11, Spring 2.7.X, Gradle 7.5, MySQL

 

[구현할 기능]

게시판에서의 카테고리별 목록 조회 기능 구현

 

현재 진행 중인 사이드 프로젝트 "펫팻"에서 맡고 있는 분양 게시판의 카테고리별 목록 조회 기능을 구현하고자 한다.

"펫팻" 분양 게시판 특성상 대분류 > 중분류 로 분류가 가능하다.

 

[Querydsl 을 사용한 이유]

1. 자동으로 쿼리를 작성해줌으로써 오류가 날 가능성을 줄여줌

2. 상황에 따라 다른 조건의 SQL을 작성해야하는 동적 쿼리를 다룰 때 용이

 


> RehomingServiceImpl

@RequiredArgsConstructor @Service public class RehomingServiceImpl implements RehomingService { ‌private final RehomingRepository rehomingRepository; ​​​​// 분양 게시글 카테고리별 목록 조회 (회원) ​​​​@Override ​​​​public RehomingPagingDto getCategoryListForMember(User user, Long categoryId, Long typeId, Pageable pageable) { ​​​​​​​​Page<RehomingInfo> rehomingInfos = rehomingRepository.rehomingCategoryListForMember(user.getId(), categoryId, typeId, pageable); ​​​​​​​​return new RehomingPagingDto(rehomingInfos); ​​​​} ​​​​// 분양 게시글 카테고리별 목록 조회 (비회원) ​​​​@Override ​​​​public RehomingPagingDto getCategoryList(Long categoryId, Long typeId, Pageable pageable) { ​​​​​​​​return new RehomingPagingDto(rehomingRepository.rehomingCategoryList(categoryId, typeId, pageable)); ​​​​} }

 

> RehomingService

public interface RehomingService { ​​​​RehomingPagingDto getCategoryListForMember(User user, Long categoryId, Long typeId, Pageable pageable); ​​​​RehomingPagingDto getCategoryList(Long categoryId, Long typeId, Pageable pageable); }

> RehomingRepository

public interface RehomingRepository extends JpaRepository<Rehoming, Long>, RehomingRepositoryQuerydsl { }

> RehomingRepositoryQuerydsl

public interface RehomingRepositoryQuerydsl { ​​​​Page<RehomingInfo> rehomingCategoryListForMember(Long userId, Long categoryId, Long typeId, Pageable pageable); ​​​​Page<RehomingInfo> rehomingCategoryList(Long categoryId, Long typeId, Pageable pageable); }

> RehomingRepositoryImpl

public class RehomingRepositoryImpl implements RehomingRepositoryQuerydsl { ​​​​private final JPAQueryFactory queryFactory; ​​​​public RehomingRepositoryImpl(EntityManager em) { ​​​​​​​​this.queryFactory = new JPAQueryFactory(em); ​​​​} ​​​ ​​​​// 분양 글 카테고리별 목록 조회 (비회원) ​​​​@Override ​​​​public Page<RehomingInfo> rehomingCategoryList(Long categoryId, Long typeId, Pageable pageable) { ​​​​​​​​QueryResults<RehomingInfo> results; ​​​​​​​​results = queryFactory ​​​​​​​​​​​​​​​​.select( ​​​​​​​​​​​​​​​​​​​​​​​​Projections.constructor( ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​RehomingInfo.class, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.rehomingId, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​Expressions.as( ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​select(image.filePath) ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​.from(image) ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​.where(image.postId.eq(rehoming.rehomingId), ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​image.postType.eq(PostType.REHOMING), ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​image.repImgNY.eq(true)) ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​, "rehomingImg"), ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.user.id, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.user.profileImgPath, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.user.nickname, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.title, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.petName, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.category.categoryGroupName, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.type.petCategoryName, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.gender, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.status, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.postType, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.createdAt, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.updatedAt, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​rehoming.viewCnt, ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ExpressionUtils.as( ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​select(likes.count()) ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​.from(likes) ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​.where(likes.postId.eq(rehoming.rehomingId)), ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​"likeCnt"), ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ExpressionUtils.as( ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​select(bookmark.count()) ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​.from(bookmark) ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​.where(bookmark.postId.eq(rehoming.rehomingId)), ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​"bookmarkCnt") ​​​​​​​​​​​​​​​​​​​​​​​​) ​​​​​​​​​​​​​​​​) ​​​​​​​​​​​​​​​​.from(rehoming) ​​​​​​​​​​​​​​​​.leftJoin(rehoming.user, user) ​​​​​​​​​​​​​​​​.where(rehoming.category.categoryGroupId.eq(categoryId), ​​​​​​​​​​​​​​​​​​​​​​​​rehoming.type.petCategoryId.eq(typeId)) ​​​​​​​​​​​​​​​​.orderBy(rehoming.createdAt.desc()) ​​​​​​​​​​​​​​​​.offset(pageable.getOffset()) ​​​​​​​​​​​​​​​​.limit(pageable.getPageSize()) ​​​​​​​​​​​​​​​​.fetchResults(); ​​​​​​​​List<RehomingInfo> content = results.getResults(); ​​​​​​​​long total = results.getTotal(); ​​​​​​​​return new PageImpl<>(content, pageable, total); ​​​​} }

 

where 절에 반려동물의 카테고리, 타입의 조건을 걸어 해당 조건에 맞는 게시물만 반환하도록 하였다.

'Spring & SpringBoot' 카테고리의 다른 글

[Java/Springboot] OpenAPI를 활용해 수집한 데이터 저장하기_(2)  (5) 2024.08.31
[Java/Springboot] OpenAPI를 활용해 데이터 수집하기_(1)  (0) 2024.08.31
Java/SpringBoot 게시판 기능 구현_Token 관련 예외 처리  (0) 2023.07.05
SpringBoot 게시판_이미지 대표이미지 출력  (0) 2023.05.12
[Spring] SpringBoot_게시글 수정하기 (JPA, MySQL)  (0) 2023.01.18
'Spring & SpringBoot' 카테고리의 다른 글
  • [Java/Springboot] OpenAPI를 활용해 수집한 데이터 저장하기_(2)
  • [Java/Springboot] OpenAPI를 활용해 데이터 수집하기_(1)
  • Java/SpringBoot 게시판 기능 구현_Token 관련 예외 처리
  • SpringBoot 게시판_이미지 대표이미지 출력
예령 : )
예령 : )
개발 공부를 하며 기록하고 있는 일기장입니다. 꾸준히 기록할 예정이니 많은 관심 부탁드립니다! :)
예령 : )
예령's 개발기록
예령 : )
전체
오늘
어제
  • 분류 전체보기 (98)
    • Github (2)
    • Java (1)
    • Spring & SpringBoot (10)
    • Server (8)
      • docker (6)
    • 개발 (6)
      • 자료구조 (2)
    • 스파르타코딩클럽 (66)
      • 웹개발의 봄, Spring (1)
      • 프로그래머스_Java_알고리즘 기초 (18)
      • 항해99 (39)
    • TIL (4)
    • Tech Conference (1)
    • AWS (0)

인기 글

최근 글

hELLO · Designed By 정상우.v4.2.0
예령 : )
Java/SpringBoot 게시판 기능 구현_Querydsl을 활용한 카테고리별 조회, 필터링
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.