KOSA Spring - 댓글처리

채정윤·2025년 4월 22일

Spring

목록 보기
18/25

📁 SQL

create table tbl_reply (
  rno number(10,0), 
  bno number(10,0) not null,
  reply varchar2(1000) not null,
  replyer varchar2(50) not null, 
  replyDate date default sysdate, 
  updateDate date default sysdate
);

create sequence seq_reply;

alter table tbl_reply add constraint pk_reply primary key (rno);

alter table tbl_reply  add constraint fk_reply_board  
foreign key (bno)  references  tbl_board (bno); 

◻️ ReplyVO.java

댓글 한개 객체

package org.zerock.domain;

import java.util.Date;

import lombok.Data;

@Data
public class ReplyVO {

  private Long rno;
  private Long bno;

  private String reply;
  private String replyer;
  private Date replyDate;
  private Date updateDate;

}

글번호를 참조하고 있음

TBL_BOARD와 TBL_REPLY는 일대다 관계이다!

🔩 ReplyMapper.xml

<mapper namespace="org.zerock.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="org.zerock.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="org.zerock.domain.ReplyVO">

		select rno, bno, reply, replyer, replyDate, updatedate
		from tbl_reply
		where bno = #{bno}
		order by rno asc

	</select>

<select id="getCountByBno" resultType="int">
<![CDATA[
select count(rno) from tbl_reply where bno = #{bno}
]]>
</select>

</mapper>

🅱️ ReplyMapper.java

package org.zerock.mapper;

import java.util.List;

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

public interface ReplyMapper {

	public int insert(ReplyVO vo);

	public ReplyVO read(Long bno);

	public int delete(Long bno);

	public int update(ReplyVO reply);

	public List<ReplyVO> getListWithPaging(Long bno);

	public int getCountByBno(Long bno);
}

🗺️ ReplyService.java

package org.zerock.service;

import java.util.List;

import org.zerock.domain.Criteria;
import org.zerock.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(Long bno);
}

🖱️ ReplyServiceImpl.java

@Service
@Log4j
@AllArgsConstructor
public class ReplyServiceImpl implements ReplyService {
	
	private ReplyMapper mapper;

	@Override
	public int register(ReplyVO vo) {
		return mapper.insert(vo);
	}

	@Override
	public ReplyVO get(Long rno) {
		return mapper.read(rno);
	}

	@Override
	public int modify(ReplyVO vo) {
		return mapper.update(vo);
	}

	@Override
	public int remove(Long rno) {
		return mapper.delete(rno);
	}

	@Override
	public List<ReplyVO> getList(Long bno) {
		return mapper.getListWithPaging(bno);
	}
}

🧑‍💼 Controller.java

create

...
@RestController // json 다루는 컨트롤러 선언
@RequestMapping("/replies/*")
@Log4j
@AllArgsConstructor
public class ReplyController {
	
	private ReplyService service;
	
	@PostMapping("/new")
	public ResponseEntity<String> create(@RequestBody ReplyVO vo){
		int insertCount = service.register(vo);
		
		return insertCount == 1 
				? new ResponseEntity<String>("Success", HttpStatus.OK) //200ok
				: new ResponseEntity<String>("Fail", HttpStatus.INTERNAL_SERVER_ERROR); //500
	}

getList

	@GetMapping("/pages/{bno}")
	public ResponseEntity<List<ReplyVO>> getList(@PathVariable("bno") Long bno){
		return new ResponseEntity<List<ReplyVO>>(service.getList(bno), HttpStatus.OK);
	}

get

	@GetMapping("{rno}")
	public ResponseEntity<ReplyVO> get(@PathVariable("rno") Long rno){
		return new ResponseEntity<ReplyVO>(service.get(rno), HttpStatus.OK);
	}

remove

	@DeleteMapping("/pages/{bno}")
	public ResponseEntity<String> remove(@PathVariable("bno") Long bno){
		int deleteCount = service.remove(bno);
		return deleteCount == 1 
				? new ResponseEntity<String>("Success", HttpStatus.OK)
				: new ResponseEntity<String>("Fail", HttpStatus.INTERNAL_SERVER_ERROR);
	}

modify

	@PutMapping("{rno}")
	public ResponseEntity<String> modify(@PathVariable("rno") Long rno,
																@RequestBody ReplyVO vo){
		vo.setRno(rno);
		return service.modify(vo) == 1
				? new ResponseEntity<String>("Sucess", HttpStatus.OK)
				: new ResponseEntity<String>("Fail", HttpStatus.INTERNAL_SERVER_ERROR);
	}

0개의 댓글