BoardSearchImpl
페이지 정보
본문
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;
}
}
- 이전글BoardSearch 24.06.29
- 다음글▲ BoardRepositoryTest 24.06.29
댓글목록
등록된 댓글이 없습니다.