BoardSearchImpl > SPRING_DATA_JPA

본문 바로가기
사이트 내 전체검색

SPRING_DATA_JPA

BoardSearchImpl

페이지 정보

profile_image
작성자 관리자
댓글 0건 조회 110회 작성일 24-06-29 09:46

본문

package web.repository;


import java.util.List;


import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageImpl;

import org.springframework.data.domain.Pageable;

import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport;


import com.querydsl.core.BooleanBuilder;

import com.querydsl.core.types.Projections;

import com.querydsl.jpa.JPQLQuery;


import web.domain.Board;

import web.domain.QBoard;

import web.domain.QBoardImage;

import web.domain.QReply;

import web.dto.BoardListReplyCountDTO;


public class BoardSearchImpl extends QuerydslRepositorySupport implements BoardSearch{


public BoardSearchImpl() {

super(Board.class);

}


@Override

public Page<Board> searchAll(String[] types, String keyword, Pageable pageable) {

QBoard board = QBoard.board; //Q도메인 객체 선언 및 초기화

JPQLQuery<Board> query = from(board); //select ... from board (쿼리 부분)


//검색 타입과 검색어가 있다면

if( (types != null && types.length > 0) && keyword != null ) {

BooleanBuilder booleanBuilder = new BooleanBuilder();


for(String type : types) {

switch (type){

case "t":

booleanBuilder.or(board.title.contains(keyword));

break;

case "c":

booleanBuilder.or(board.content.contains(keyword));

break;

case "w":

booleanBuilder.or(board.writer.contains(keyword));

break;

} //switch

} //for


query.where(booleanBuilder);

} //if


query.where(board.bno.gt(0L));


//paging

this.getQuerydsl().applyPagination(pageable, query);


List<Board> list = query.fetch();

long count = query.fetchCount();


return new PageImpl<>(list, pageable, count);

}


@Override

public Page<Board> search1(Pageable pageable) {

QBoard board = QBoard.board; //Q도메인 객체 선언 및 초기화


JPQLQuery<Board> query = from(board); //select ... from board (쿼리 부분)

query.where(board.title.contains("1")); //where title like '%1%' (쿼리 부분)


//paging

this.getQuerydsl().applyPagination(pageable, query);


List<Board> list = query.fetch();

long count = query.fetchCount();


return null;

}


@Override

public Page<BoardListReplyCountDTO> searchWithReplyCount(String[] types, String keyword, Pageable pageable) {

QBoard board = QBoard.board;

QReply reply = QReply.reply;


JPQLQuery<Board> query = from(board);

query.leftJoin(reply).on(reply.board.eq(board));


query.groupBy(board);


if( (types != null && types.length > 0) && keyword != null ){

BooleanBuilder booleanBuilder = new BooleanBuilder();


for(String type: types){

switch (type){

case "t":

booleanBuilder.or(board.title.contains(keyword));

break;

case "c":

booleanBuilder.or(board.content.contains(keyword));

break;

case "w":

booleanBuilder.or(board.writer.contains(keyword));

break;

}

}//end for


query.where(booleanBuilder);

}


//bno > 0

query.where(board.bno.gt(0L));


JPQLQuery<BoardListReplyCountDTO> dtoQuery = 

query.select(Projections.bean(

BoardListReplyCountDTO.class,

board.bno,

board.title,

board.writer,

board.regDate,

reply.count().as("replyCount")

));


this.getQuerydsl().applyPagination(pageable,dtoQuery);


List<BoardListReplyCountDTO> dtoList = dtoQuery.fetch();


long count = dtoQuery.fetchCount();


return new PageImpl<>(dtoList, pageable, count);

}


@Override

public Page<BoardListReplyCountDTO> searchWithAll(String[] types, String keyword, Pageable pageable) {

QBoard board = QBoard.board;

QReply reply = QReply.reply;


JPQLQuery<Board> boardJPQLQuery = from(board);

boardJPQLQuery.leftJoin(reply).on(reply.board.eq(board)); //left join


getQuerydsl().applyPagination(pageable, boardJPQLQuery); //paging


List<Board> boardList = boardJPQLQuery.fetch();


boardList.forEach(board1 ->{

System.out.println(board1.getBno());

System.out.println(board1.getImageSet());

System.out.println("-----------------------------");

});


return null;

}


}


댓글목록

등록된 댓글이 없습니다.

회원로그인

회원가입

사이트 정보

공지사항
자유게시판
질문답변
1:1문의

 

별명 : 터푸가위
주소 : 부산시 동래구 명장로20번길 90
대표 : 박규태
메일 : dancepkt@******.com

접속자집계

오늘
125
어제
127
최대
10,760
전체
269,141
Copyright © dancePKT . All rights reserved.