본문 바로가기

spring

게시판에 댓글만들기

sql>tbl_reply생성

create table tbl_reply(
	rno int not null auto_increment,
    bno int not null,
    replytext varchar(100),
    regdate datetime default now(),
    updatedate datetime default now(),
    primary key(rno),
    foreign key(bno) references tbl_board(bno)
);

ReplyVO(class)

package com.example.domain;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

public class ReplyVO {
	private int rno;
	private int bno;
	private String replytext;
	
	@JsonFormat(pattern="yyyy-MM-dd")
	private Date regdate;
	@JsonFormat(pattern="yyyy-MM-dd")
	private Date updatedate;
	public int getRno() {
		return rno;
	}
	public void setRno(int rno) {
		this.rno = rno;
	}
	public int getBno() {
		return bno;
	}
	public void setBno(int bno) {
		this.bno = bno;
	}
	public String getReplytext() {
		return replytext;
	}
	public void setReplytext(String replytext) {
		this.replytext = replytext;
	}
	public Date getRegdate() {
		return regdate;
	}
	public void setRegdate(Date regdate) {
		this.regdate = regdate;
	}
	public Date getUpdatedate() {
		return updatedate;
	}
	public void setUpdatedate(Date updatedate) {
		this.updatedate = updatedate;
	}
	@Override
	public String toString() {
		return "ReplyVO [rno=" + rno + ", bno=" + bno + ", replytext=" + replytext + ", regdate=" + regdate
				+ ", updatedate=" + updatedate + "]";
	}
		
}


ReplyDAO(interface)

package com.example.persistence;

import java.util.List;

import com.example.domain.Criteria;
import com.example.domain.ReplyVO;

public interface ReplyDAO {
	public List<ReplyVO> list(Criteria cri,int bno)throws Exception;
	
	public int total(int bno) throws Exception;
	
	public void insert(ReplyVO vo) throws Exception;
	
	public void delete(int rno) throws Exception;
	
	
}


ReplyMapper(xml)

<?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="ReplyMapper">
	<select id="list" resultType="com.example.domain.ReplyVO">
		select * 
		from tbl_reply 
		where bno=#{bno}
		order by rno desc
		limit #{cri.pageStart}, #{cri.perPageNum}
	</select>
	<select id="total" resultType="int">
		select count(*) from tbl_reply
		where bno=#{bno}
	</select>
	<insert id="insert">
	 insert into tbl_reply(bno,replytext) 
	 values(#{bno},#{replytext})
	</insert>
	
	<delete id="delete">
		delete from tbl_reply
		where rno=#{rno}
	</delete>
</mapper>


DBTest3(class)

package com.example.web;
import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.example.domain.Criteria;


import com.example.persistence.ReplyDAO;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring/**/*.xml"})

public class DBTest3 {
	@Inject
	ReplyDAO dao;
	
	@Test
	public void list() throws Exception{
		Criteria cri=new Criteria();
		
		cri.setPage(2);
		cri.setPerPageNum(10);
		
		dao.list(cri,99);
	}
}


ReplyDAOImpl(class)

package com.example.persistence;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.example.domain.Criteria;
import com.example.domain.ReplyVO;

@Repository
public class ReplyDAOImpl implements ReplyDAO{
	@Inject
	SqlSession session;
	private static final String namespace="ReplyMapper";
	
	
	@Override
	public List<ReplyVO> list(Criteria cri,int bno) throws Exception {
		// TODO Auto-generated method stub
		
		//어느게시글에 댓글인지 알기위 해  bno 를 받는다.
		
		//hasnmap은 map과  like list& arrayList관계
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("bno",bno);
		map.put("cri",cri);
		
		//하나만 매개변수로 보낼 수있어서 map을 만들어서 보냄
		
		return session.selectList(namespace +".list",map);
	}

	
	
	
	@Override
	public int total(int bno) throws Exception {
		// TODO Auto-generated method stub
		return session.selectOne(namespace+".total",bno);
	}




	@Override
	public void insert(ReplyVO vo) throws Exception {
		// TODO Auto-generated method stub
		session.insert(namespace+".insert",vo);
	}




	@Override
	public void delete(int rno) throws Exception {
		// TODO Auto-generated method stub
		session.delete(namespace+".delete",rno);
	}
}


ReplyController(class)

package com.example.web;

import java.util.HashMap;

import java.util.Map;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Criteria;
import com.example.domain.PageMaker;
import com.example.domain.ReplyVO;
import com.example.persistence.ReplyDAO;

//데이터를 만드는 컨트롤러 
//JSp는 정의 못함


@RequestMapping("reply")

@RestController
public class ReplyController {
	@Inject
	ReplyDAO dao;
	
	@RequestMapping("list")
	public Map<String,Object> list(Criteria cri,int bno) throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		
		
		
		
		cri.setPerPageNum(5);
		
		PageMaker pm=new PageMaker();
		pm.setCri(cri);
		pm.setTotalCount(dao.total(bno));
		
		map.put("list", dao.list(cri, bno));
		map.put("pm", pm);
		return map;
	}
	@RequestMapping(value="insert",method=RequestMethod.POST)
	public void insert(ReplyVO vo) throws Exception{
		dao.insert(vo);
	}
	
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public void delete(int rno) throws Exception{
		dao.delete(rno);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}


board>reply(jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert title here</title>
	<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.js"></script>
	<style>
	a{
		text-decoration:none;
		color:black;
	}
	*{
		margin:0px;
		padding:0xp;
	}
	body{
		margin:0px auto;
	}
	table{
		margin:0px auto;
	}
	</style>
</head>
<body>
	<h1 style="margin-top:10px;margin-bottom:10px;text-align:center;">[댓글 출력]</h1>
	<div id="total" style="margin:0px auto;"></div>
	<div style="border:0.5px soild; width:500px; height:200px; padding:5px; margin:0px auto;">
		<input type="text" size="50" id="txtcontent">
		<input type="button" value="입력" id="btninsert">
	</div>
	<table border=1 width=500 id="tbl"></table>
	<script id="temp" type="text/x-handlebars-template">
	{{#each list}}
	<tr>
		<td>{{rno}}</td>
		<td>{{replytext}}</td>
		<td>{{regdate}}</td>
		<td><button rno={{rno}}>삭제</button></td>
	</tr>
	{{/each}}
	</script>
	<div id="pagination"></div>
</body>
	<script>
	var bno=${vo.bno};
	var page=1;
	
	
	$("#tbl").on("click","button",function(){
		var rno=$(this).attr("rno");
		if(!confirm(rno + "을(를) 삭제하시겠습니까?")) return;
		$.ajax({
			type:"post",
			url:"../reply/delete",
			data:{"rno":rno},
			success:function(){
				alert("삭제되었습니다.");
				getlist();
			}
			
		});
	});
	
	$("#txtcontent").keydown("click",function(key){
		if(key.keyCode==13){
			var replytext=$("#txtcontent").val();
			if(replytext==""){
				alert("댓글 내용을 입력하세요");
				$("#txtcontent").focus();
			}else{
				$.ajax({
					type:"post",
					url:"../reply/insert",
					data:{"bno":bno,"replytext":replytext},
					success:function(){
						alert("저장되었습니다.");
						$("#txtcontent").val("");
						page=1;
						getlist();
					}
				});
			}
		}
	});
	
	
	getlist();
	
	$("#btninsert").on("click",function(){
		var replytext=$("#txtcontent").val();
		if(replytext==""){
			alert("댓글 내용을 입력하세요");
			$("#txtcontent").focus();
		}else{
			$.ajax({
				type:"post",
				url:"../reply/insert",
				data:{"bno":bno,"replytext":replytext},
				success:function(){
					alert("저장되었습니다.");
					$("#txtcontent").val("");
					page=1;
					getlist();
				}
			});
		}
	});
	function getlist(){
			$.ajax({
				type:"get",
				url:"../reply/list",
				data:{"page":page,"bno":bno},
				success:function(data){
					var temp=Handlebars.compile($("#temp").html());
					$("#tbl").html(temp(data));
					
					var str="";
					if(data.pm.prev){
						str += "<a href='"  +  (data.pm.startPage - 1) + "'>♧♣</a>";
					}
					for(var i=data.pm.startPage; i<=data.pm.endPage; i++){
						str += "[<a href='"  + i +  "'>" + i +   "</a>]";
						
					}
					if(data.pm.next){
						str +=  "<a href='" + (data.pm.endPage + 1) +  "'>♧♣</a>";
					}
					$("#pagination").html(str);
					$("#total").html("전체" + data.pm.totalCount + "건");
				}
			});
		}
	$("#pagination").on("click" , "a" ,function(event){
		event.preventDefault();	
		page=$(this).attr("href");
		getlist();
	});
	</script>
</html>

'spring' 카테고리의 다른 글

맛집목록 만들기  (0) 2019.11.26
Transaction 처리  (0) 2019.11.26
사용자 게시판 만들기  (0) 2019.11.22
Jstl 게시판 만들기  (0) 2019.11.22
네이버 API와 CURD작업 연결  (0) 2019.11.22