본문 바로가기
Spring공부/1-REST

REST방식(8)-댓글페이징처리

by 으노으뇨 2021. 10. 11.
728x90
반응형
SMALL

정성스럽게 작성하다가 날라갔다..

다시 작성하겠다.

DB설계 와 인덱스 설계

인덱스를 생성하자

create index_reply on tbl_reply(bno desc, rno asc);

그리고 ReplyMapper.xml에 다음내용을 추가한다.

	<select id="getListWithPaging"
		resultType="org.study.domain.ReplyVO">

  <![CDATA[
 select  rno, bno, reply, replyer, replydate, updatedate
 from 
   (
    select /*+INDEX(tbl_reply idx_reply) */ 
      rownum rn,  rno, bno, reply, replyer, replyDate, updatedate
    from tbl_reply
    where bno =  #{bno}
    and rno > 0
    and rownum <= #{cri.pageNum} * #{cri.amount}
   ) where rn > (#{cri.pageNum} -1) * #{cri.amount}
]]>

	</select>

테스트할 구문이다.

	@Test
	public void testList2() {

		Criteria cri = new Criteria(1, 10);
		List<ReplyVO> replies = mapper.getListWithPaging(cri, 1L);

		replies.forEach(reply -> log.info(reply));

	}

테스트를 진행하자

이상이없다면 댓글과 댓글 수 처리 할 DTO  파일을 만든다.

파일이름은 ReplyPageDTO로 짓는다.

package org.study.domain;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

@Data
@AllArgsConstructor
@Getter
public class ReplyPageDTO {

	private int replyCnt;
	private List<ReplyVO> list;
}

 

 RepluService인터페이스와 RepluServiceImpl 클래스를 수정하자

package org.study.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.study.domain.Criteria;
import org.study.domain.ReplyVO;

public interface ReplyMapper {
...

	public List<ReplyVO> getListWithPaging(@Param("cri") Criteria cri, @Param("bno") Long bno);
}
package org.study.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.study.domain.Criteria;
import org.study.domain.ReplyPageDTO;
import org.study.domain.ReplyVO;
import org.study.mapper.ReplyMapper;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;

@Service
@Log4j
@AllArgsConstructor
public class ReplyServiceImpl implements ReplyService {

	private ReplyMapper mapper;

...

	@Override
	public ReplyPageDTO getListPage(Criteria cri, Long bno) {

		return new ReplyPageDTO(mapper.getCountByBno(bno), mapper.getListWithPaging(cri, bno));
	}
}

그리고 ReplyController를 수정한다.

//기존 getList()를 주석처리 후 새로 페이징을 할 getList()메서드로 수정하자
//	@GetMapping(value = "/pages/{bno}/{page}", produces = { MediaType.APPLICATION_XML_VALUE,
//			MediaType.APPLICATION_JSON_UTF8_VALUE })
//	public ResponseEntity<List<ReplyVO>> getList(@PathVariable("page") int page, @PathVariable("bno") Long bno) {
//
//		Criteria cri = new Criteria(page, 10);
//
//		log.info("get Reply List bno: " + bno);
//
//		log.info("cri:" + cri);
//
//		return new ResponseEntity<>(service.getList(cri, bno), HttpStatus.OK);
//	}
	@GetMapping(value = "/pages/{bno}/{page}", produces = { MediaType.APPLICATION_XML_VALUE,
			MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<ReplyPageDTO> getList(@PathVariable("page") int page, @PathVariable("bno") Long bno) {

		Criteria cri = new Criteria(page, 10);

		log.info("get Reply List bno: " + bno);

		log.info("cri:" + cri);

		return new ResponseEntity<>(service.getListPage(cri, bno), HttpStatus.OK);
	}

기존과 동일하게 Json데이터를 전송하게 되지만 ReplyPageDTO 객체를 Json으로 전송하게 되므로, 특정 게시물의 댓글 목록을 조회하면 아래와 같이 'replyCnt'와 'list'라는 이름의 속성을 가지는 Json문자열이 전송된다.

http://localhost:8080/replies/pages/1/1.json

{"replyCnt":6,"list":[{ ... }] 로 되어있음을 볼 수 있다.

다음엔 댓글 페이지 화면처리에 대해 알아보겠다... 작성도중 날라가버려서 내용이 엄청 많이 줄었지만 핵심만이라도 건져서 올리긴했다.

728x90
반응형
LIST

'Spring공부 > 1-REST' 카테고리의 다른 글

pull-request  (0) 2021.10.11
REST방식(9)-댓글페이징처리  (0) 2021.10.11
REST방식(9)-이벤트처리  (0) 2021.10.08
REST방식(8)-Ajax  (0) 2021.10.08
REST방식(7)-Ajax댓글처리  (2) 2021.10.07

댓글