728x90
반응형
1. 요약
- 댓글이 글마다 몇개씩 있는지 list.html로 보여준다.
- view 부분 : list.html에 replycount 항목 추가
- DTO : BoardListRepltCountDTO 추가
- list.html에 보내는 Page<>컬렉션에 담아 보낼 클래스이다.
- 필드는 list에 출력할 값들을 포함한다.(replyCount추가)
- Repository : BoardSearch, BoardSearchImpl에 searchWithReplyCount 메서드 추가
- Querydsl을 사용해서 DB에 저장된 Board와 Reply 테이블을 조인하고 불러와 List<BoardListCountDTO>에 저장한 뒤, 매개변수로 받은 Pageable 객체 정보를 매개변수로 함께 받아 Page<BoardListCountDTO>를 반환한다.
- ReplyRepository에 JpaRepository를 상속받아서 JPA함수를 사용한다.
- Service : listWithReply 메서드 추가
- BoardRepository에서 상속받은 searchWithReplyCount 메서드를 사용해 replyCount정보까지 담아서 PageResponseDTO< BoardListCountDTO>를 반환한다.
- Controller
- PageResponseDTO<BoardDTO> 타입을 PageResponseDTO<BoardListReplyCountDTO>로 바꾸어 model에 addAttribute하여 값을 보낸다.
2. 코드
2.1 BoardReplyCountDTO.class
@Data
public class BoardListReplyCountDTO {
private Long bno;
private String title;
private String writer;
private LocalDateTime regDate;
private Long replyCount;
}
2.2 BoardSearch.interface
public interface BoardSearch {
Page<Board> search1(Pageable pageable);
Page<Board> searchAll(String[] types, String keyword, Pageable pageable);
Page<BoardListReplyCountDTO> searchWithReplyCount
(String[] types, String keyword, Pageable pageable);
}
2.3 BoardSearchImpl.class
public class BoardSearchImpl extends QuerydslRepositorySupport implements BoardSearch{
public BoardSearchImpl(){
super(Board.class);
}
~
@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)); //Spring에선 join으로 테이블을 연결하는 것을 지원한다.
query.groupBy(board.bno);
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;
}
}
query.where(booleanBuilder);
}
query.where(board.bno.gt(0L));
JPQLQuery<BoardListReplyCountDTO> dtoQuery = query.select(Projections
.bean(BoardListReplyCountDTO.class, //Projections.bean은 select할 때, BoardListReplyCountDTO 모양을 해주겠다는 설정이다.
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);
}
}
2.4 BoardRepository.class
public interface ReplyRepository extends JpaRepository<Reply, Long> {}
2.5 BoardService.interface
public interface BoardService {
Long register(BoardDTO boardDTO);
BoardDTO readOne(Long bno);
void modify(BoardDTO boardDTO);
void remove(Long bno);
PageResponseDTO<BoardDTO> list(PageRequestDTO pageRequestDTO);
//댓글의 갯수까지 처리
PageResponseDTO<BoardListReplyCountDTO> listWithReply(PageRequestDTO pageRequestDTO);
}
2.6 BoardSeviceImpl.class
@Service
@RequiredArgsConstructor
@Transactional
public class BoardServiceImpl implements BoardService{
private final ModelMapper modelMapper;
private final BoardRepository boardRepository;
~
@Override
public PageResponseDTO<BoardListReplyCountDTO> listWithReply(PageRequestDTO pageRequestDTO){
String[] types = pageRequestDTO.getTypes();
String keyword = pageRequestDTO.getKeyword();
Pageable pageable = pageRequestDTO.getPageable("bno");
Page<BoardListReplyCountDTO> result = boardRepository.searchWithReplyCount(types, keyword, pageable);
return PageResponseDTO.<BoardListReplyCountDTO>withAll()
.pageRequestDTO(pageRequestDTO)
.dtoList(result.getContent())
.total((int)result.getTotalElements())
.build();
}
}
2.7 list.html
<span>
[[${dto.replyCount}]]
</span>
- 테이블 타이틀이 들어있는 <td>에 추가한 코드
2.7 실행 화면 : Title 옆에 숫자가 댓글의 숫자이다.
3. IntelliJ DataBase에 접근해 자료값 수정 및 추가, 삭제하기
- db에서 테이블을 마우스 우클릭한다.
- Edit Data를 클릭한다.
- 자료를 수정, 삭제, 추가 후 Submit 버튼을 누른다.
- 새로고침 버튼을 누르면 자료값이 수정된 것을 볼 수 있다.
- Modify Table을 누르면 컬럼의 설정값도 수정 할 수 있다.
댓글