2019. 10. 9. 20:50ㆍProject B (SPMS)/Project B 파트5
src/main/java
com.spms.mapper
ReplyMapper.java 인터페이스
getCountByBno()
package com.spms.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.spms.domain.Criteria;
import com.spms.domain.ReplyVO;
public interface ReplyMapper {
public int insert(ReplyVO vo);
public ReplyVO read(Long bno);
public int delete(Long rno);
public int update(ReplyVO reply);
public List<ReplyVO> getListWithPaging(@Param("cri") Criteria cri, @Param("bno") Long bno);
public int getCountByBno(Long bno);
}
댓글들을 페이징 처리하기위해서는 해당 게시물의 전체 댓글의 숫자를 파악해서 화면에 보여줄 필요가 있다.
ReplyMapper 인터페이스에 getCountByBno()를 추가한다.
src/main/resources
com
spms
mapper
ReplyMapper.xml
getCountByBno()
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.spms.mapper.ReplyMapper">
<insert id="insert">
insert into TBL_REPLY (
RNO, BNO, REPLY, REPLYER
)
values (
SEQ_REPLY.NEXTVAL, #{bno}, #{reply}, #{replyer}
)
</insert>
<select id="read" resultType="com.spms.domain.ReplyVO">
select *
from TBL_REPLY
WHERE RNO = #{rno}
</select>
<delete id="delete">
delete from TBL_REPLY
where RNO = #{rno}
</delete>
<update id="update">
update TBL_REPLY
set REPLY = #{reply},
UPDATEDATE = SYSDATE
where RNO = #{rno}
</update>
<select id="getListWithPaging" resultType="com.spms.domain.ReplyVO">
<![CDATA[
select RNO, BNO, REPLY, REPLYER, REPLYDATE, UPDATEDATE
from
(
select /*+ INDEX (TBL_REPLY IDX_REPLY) */
rownum RNUM, RNO, BNO, REPLY, REPLYER, REPLYDATE, UPDATEDATE
from TBL_REPLY
where BNO = #{bno}
and RNO > 0
and rownum <= #{cri.pageNum} * #{cri.amount}
)
where RNUM > (#{cri.pageNum} -1) * #{cri.amount}
]]>
</select>
<select id="getCountByBno" resultType="int">
<![CDATA[
select count(RNO)
from TBL_REPLY
where BNO = #{BNO}
]]>
</select>
</mapper>
ReplyServiceImpl 에서 댓글과 댓글 수 처리
src/main/java
com.spms.domain
ReplyPageDTO.java
package com.spms.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;
}
단순히 댓글 전체를 보여주는 방식과 달리 댓글의 페이징 처리가 필요한 경우에는 댓글 목록과 함께 전체 댓글 수를 같이 전달해야한다.
ReplyService 인터페이스와 구현 클래스인 ReplyServiceImpl 클래스는 List<ReplyVO> 와 댓글의 수를 같이 전달할 수 있는 구조로 변경한다.
ReplyPageDTO는 객체 생성 시에 편리하도록 @AllArgsContructor 를 이용해서 replyCnt와 list를 생성자의 파라미터로 처리한다.
src/main/java
com.spms.service
ReplyService.java 인터페이스
getListPage()
package com.spms.service;
import java.util.List;
import com.spms.domain.Criteria;
import com.spms.domain.ReplyPageDTO;
import com.spms.domain.ReplyVO;
public interface ReplyService {
public int register(ReplyVO vo);
public ReplyVO get(Long rno);
public int modify(ReplyVO vo);
public int remove(Long rno);
public List<ReplyVO> getList(Criteria cri, Long bno);
public ReplyPageDTO getListPage(Criteria cri, Long bno);
}
src/main/java
com.spms.service
ReplyServiceImpl.java
getListPage()
package com.spms.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.spms.domain.Criteria;
import com.spms.domain.ReplyPageDTO;
import com.spms.domain.ReplyVO;
import com.spms.mapper.ReplyMapper;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
@Service
@Log4j
@AllArgsConstructor
public class ReplyServiceImpl implements ReplyService {
private ReplyMapper replyMapper;
@Override
public int register(ReplyVO vo) {
log.info("register......" + vo);
return replyMapper.insert(vo);
}
@Override
public ReplyVO get(Long rno) {
log.info("get......" + rno);
return replyMapper.read(rno);
}
@Override
public int modify(ReplyVO vo) {
log.info("modify......" + vo);
return replyMapper.update(vo);
}
@Override
public int remove(Long rno) {
log.info("remove...." + rno);
return replyMapper.delete(rno);
}
@Override
public List<ReplyVO> getList(Criteria cri, Long bno) {
log.info("get Reply List of a Board " + bno);
return replyMapper.getListWithPaging(cri, bno);
}
@Override
public ReplyPageDTO getListPage(Criteria cri, Long bno) {
// TODO Auto-generated method stub
return new ReplyPageDTO(replyMapper.getCountByBno(bno), replyMapper.getListWithPaging(cri, bno));
}
}
src/main/java
com.spms.controller
ReplyController.java
getList()
package com.spms.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.spms.domain.Criteria;
import com.spms.domain.ReplyPageDTO;
import com.spms.domain.ReplyVO;
import com.spms.service.ReplyService;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
@RequestMapping("/replies/")
@RestController
@Log4j
@AllArgsConstructor
public class ReplyController {
private ReplyService replyService;
@PostMapping(value = "/new", consumes = "application/json", produces = { MediaType.TEXT_PLAIN_VALUE })
public ResponseEntity<String> create(@RequestBody ReplyVO reply) {
log.info("ReplyVO: " + reply);
int insertCount = replyService.register(reply);
log.info("Reply INSERT COUNT: " + insertCount);
return insertCount == 1 ? new ResponseEntity<>("success", HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@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) {
log.info("getList.................");
Criteria cri = new Criteria(page,10);
log.info("get Reply List bno : " + bno);
log.info(cri);
return new ResponseEntity<>(replyService.getListPage(cri, bno), HttpStatus.OK);
}
@GetMapping(value = "/{rno}",
produces = { MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_UTF8_VALUE })
public ResponseEntity<ReplyVO> get(@PathVariable("rno") Long rno) {
log.info("get: " + rno);
return new ResponseEntity<>(replyService.get(rno), HttpStatus.OK);
}
@DeleteMapping(value = "/{rno}", produces = { MediaType.TEXT_PLAIN_VALUE })
public ResponseEntity<String> remove(@PathVariable("rno") Long rno) {
log.info("remove: " + rno);
return replyService.remove(rno) == 1
? new ResponseEntity<>("success", HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@RequestMapping(method = { RequestMethod.PUT,
RequestMethod.PATCH
}, value = "/{rno}",
consumes = "application/json",
produces = {
MediaType.TEXT_PLAIN_VALUE
})
public ResponseEntity<String> modify(
@RequestBody ReplyVO reply,
@PathVariable("rno") Long rno) {
reply.setRno(rno);
log.info("rno: " + rno);
log.info("modify: " + reply);
return replyService.modify(reply) == 1
? new ResponseEntity<>("success", HttpStatus.OK)
: new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
ReplyService에 새로 추가된 getListPage()를 호출하고 데이터를 전송하는 형태로 수정한다.
기존과 동일하게 JSON 데이터를 전송하지만 ReplyPageDTO 객체를 JSON 으로 전송하게 되므로, 특정 게시물의 댓글 목록을 조회하면 아래와 같이 replyCnt와 list라는 이름의 속성을 갖는 JSON 문자열이 전송된다.
http://localhost/replies/pages/{댓글들이 달린 게시물 번호}/1.json
'Project B (SPMS) > Project B 파트5' 카테고리의 다른 글
[B -2-50] Ajax 댓글 처리 23 : [댓글 페이지의 화면 처리] (0) | 2019.10.09 |
---|---|
[B -2-48] Ajax 댓글 처리 21 : [댓글의 페이징 처리] (0) | 2019.10.09 |
[B -2-47] Ajax 댓글 처리 20 : [댓글의 수정/삭제 이벤트 처리] (0) | 2019.10.09 |
[B -2-46] Ajax 댓글 처리 19 : [특정 댓글의 클릭 이벤트 처리] (0) | 2019.10.09 |
[B -2-45] Ajax 댓글 처리 18 : [댓글 작성 다이얼로그] (0) | 2019.10.09 |