sql>tbl_board 테이블 생성
create table tbl_board(
bno int not null auto_increment, /* 등록번호 */
title varchar(200) not null, /* 제목 */
content text ,
regdate datetime not null default now(), /* 등록일 현재 시간 자동*/
viewcnt int default 0, /* 조회수 0*/
primary key(bno)
);
BoardVO
package com.example.domain;
import java.util.Date;
public class BoardVO {
private int bno;
private String title;
private String content;
private Date regdate;
private int viewcnt;
private int replycnt;
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public int getViewcnt() {
return viewcnt;
}
public void setViewcnt(int viewcnt) {
this.viewcnt = viewcnt;
}
public int getReplycnt() {
return replycnt;
}
public void setReplycnt(int replycnt) {
this.replycnt = replycnt;
}
@Override
public String toString() {
return "BoardVO [bno=" + bno + ", title=" + title + ", content=" + content + ", regdate=" + regdate
+ ", viewcnt=" + viewcnt + ", replycnt=" + replycnt + "]";
}
}
BoardDAO
package com.example.persistence;
import java.util.List;
import com.example.domain.BoardVO;
public interface BoardDAO {
public List<BoardVO> list();
public BoardVO read(int bno);
public void viewupdate(int bno);//게시글 번호가 몇번인 글에 viewcnt를 1 업데이트
public void replyupdate(int bno,int cnt);
}
BoardMapper
<?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="BoardMapper">
<select id="list" resultType="com.example.domain.BoardVO">
select * from tbl_board
order by bno desc
limit 0,10
</select>
<select id="read" resultType="com.example.domain.BoardVO">
select * from tbl_board
where bno=#{bno}
</select>
<update id="viewupdate">
update tbl_board set
viewcnt=viewcnt+1
where bno=#{bno}
</update>
<update id="replyupdate">
update tbl_board set
replycnt=replycnt+#{cnt}
where bno=#{bno}
</update>
</mapper>
BoardDAOImpl
package com.example.persistence;
import java.util.HashMap;
import java.util.List;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.example.domain.BoardVO;
@Repository
public class BoardDAOImpl implements BoardDAO{
@Inject
SqlSession session; //root-context의 SQL을 사용
String namespace="BoardMapper"; //MAPPER에 SQL문을 사용
@Override
public List<BoardVO> list() {
// TODO Auto-generated method stub
return session.selectList(namespace+".list");//실행한 결과를 리턴
}
@Override
public BoardVO read(int bno) {
// TODO Auto-generated method stub
return session.selectOne(namespace+".read",bno);
}
@Override
public void viewupdate(int bno) {
// TODO Auto-generated method stub
session.update(namespace+".viewupdate",bno);
}
@Override
public void replyupdate(int bno, int cnt) {
// TODO Auto-generated method stub
HashMap<String,Object> map=new HashMap<String,Object>();
map.put("bno", bno);
map.put("cnt",cnt);
session.update(namespace+".replyupdate",map);
}
}
BoardController
package com.example.web;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.persistence.BoardDAO;
import com.example.service.BoardService;
@Controller
public class BoardController {
@Inject
BoardDAO dao;
@Inject
BoardService service;
@RequestMapping("list")//'list'라고 치면은 list.jsp를 실행하겠다.
public String list(Model model){
model.addAttribute("list",dao.list());//list에 담고
return "list";
}
@RequestMapping("read")
public String read(Model model,int bno){ //데이터를 가져가려면 model이 있어야됨
model.addAttribute("vo",service.read(bno));
return "read";
}
}
BoardService
package com.example.service;
import com.example.domain.BoardVO;
public interface BoardService {
public BoardVO read(int bno);
}
BoardServiceImpl
package com.example.service;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.domain.BoardVO;
import com.example.persistence.BoardDAO;
@Service
public class BoardServiceImpl implements BoardService{
@Inject
BoardDAO dao;
@Transactional
@Override
public BoardVO read(int bno) {
// TODO Auto-generated method stub
BoardVO vo=dao.read(bno);
dao.viewupdate(bno);
return vo;
}
}
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
package com.example.domain;
import java.util.Date;
public class ReplyVO {
private int rno;
private int bno;
private String replytext;
private Date regdate;
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
package com.example.persistence;
import java.util.List;
import com.example.domain.ReplyVO;
public interface ReplyDAO {
public void insert(ReplyVO vo);
public List<ReplyVO> list(int bno);
public void delete(int rno);
}
ReplyMapper
<?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">
<insert id="insert">
insert into tbl_reply(bno,replytext)
values(#{bno},#{replytext})
</insert>
<select id="list" resultType="com.example.domain.ReplyVO">
select * from tbl_reply
where bno=#{bno}
order by rno desc
</select>
<delete id="delete">
delete from tbl_reply
where rno=#{rno}
</delete>
</mapper>
ReplyDAOImpl
package com.example.persistence;
import java.util.List;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import com.example.domain.ReplyVO;
@Repository
public class ReplyDAOImpl implements ReplyDAO{
@Inject
SqlSession session;
String namespace="ReplyMapper";
@Override
public void insert(ReplyVO vo) {
// TODO Auto-generated method stub
session.insert(namespace+".insert",vo);
}
@Override
public List<ReplyVO> list(int bno) {
// TODO Auto-generated method stub
return session.selectList(namespace+".list",bno);
}
@Override
public void delete(int rno) {
// TODO Auto-generated method stub
session.delete(namespace+".delete",rno);
}
}
ReplyService
package com.example.service;
import com.example.domain.ReplyVO;
public interface ReplyService {
public void insert(ReplyVO vo);
public void delete(int rno,int bno);
}
ReplyServiceImpl
package com.example.service;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.domain.ReplyVO;
import com.example.persistence.BoardDAO;
import com.example.persistence.ReplyDAO;
@Service
public class ReplyServiceImpl implements ReplyService{
@Inject
BoardDAO bdao;
@Inject
ReplyDAO rdao;
@Transactional
@Override
public void insert(ReplyVO vo) {
// TODO Auto-generated method stub
rdao.insert(vo);
bdao.replyupdate(vo.getBno(), 1);
}
@Override
public void delete(int bno,int rno) {
// TODO Auto-generated method stub
rdao.delete(rno);
bdao.replyupdate(bno, -1);
}
}
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>
<style>
td{
border-bottom:1px dotted gray;
padding:5px;
}
.title{
color:gray;
}
table{
border-top:1px solid gray;
}
</style>
</head>
<body>
<h1 style="text-align:center;"></h1>
<table border=0 width=560 style="margin:0px auto;text-align:center;margin-top:100px;">
<tr class="title">
<td width=50>번호</td>
<td width=150>제목</td>
<td width=200>날짜</td>
<td width=80>조회수</td>
<td width=80>댓글수</td>
</tr>
<c:forEach items="${list}" var="vo">
<tr onClick="location.href='read?bno=${vo.bno}'">
<td width=50>${vo.bno}</td>
<td width=150>${vo.title}</td>
<td width=200><fmt:formatDate pattern="yyyy-MM-dd kk:mm:ss" value="${vo.regdate}"/></td>
<td width=80>${vo.viewcnt}</td>
<td width=80>${vo.replycnt}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
read.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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>
<style>
table{
border-top:1px solid gray;
}
td{
border-bottom:1px dotted gray;
padding:5px;
}
</style>
</head>
<body>
<h1 style="text-align:center;"></h1>
<table border=0 width=500 style="margin:0px auto;text-align:center;margin-top:100px;">
<tr>
<td width=80>글번호</td>
<td>${vo.bno}</td>
</tr>
<tr>
<td width=100>제목</td>
<td>${vo.title}</td>
</tr>
<tr>
<td width=100>내용</td>
<td>${vo.content}</td>
</tr>
<tr>
<td width=100>작성일</td>
<td><fmt:formatDate value="${vo.regdate}" pattern="yyyy-MM-dd kk:mm:ss"/></td>
</tr>
</table>
<div style="text-align:center;margin-top:10px;">
<button onClick="location.href='list'">목록</button>
</div>
<div>
<jsp:include page="reply.jsp"></jsp:include>
</div>
</body>
</html>
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>
<style>
ul{
list-style:none;
}
.total{
margin:0px auto;
text-align:center;
}
</style>
<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>
</head>
<body>
<h1 style="text-align:center;"></h1>
<div id="total" class="total"></div>
<div style="width:500px;padding:10px;background:gray;margin:0px auto;text-align:center;margin-top:25px;">
<input type="text" id="replytext" size=50 style="height:20px;">
<input type="button" value="입력" id="btninsert">
</div>
<ul id="list" style="width:500px; margin:0px auto; text-align:center; margin-top:20px;"></ul>
<script id="temp" type="text/x-handlebars-template">
{{#each .}}
<li>
{{replytext}}
<button rno={{rno}}>삭제</button>
</li>
{{/each}}
</script>
</body>
<script>
var bno="${vo.bno}";
//alert(bno);
getlist();
function getlist(){
$.ajax({
type:"get",
url:"rlist",
data:{"bno":bno},
success:function(data){
$("#total").html("전체 댓글:" + data.length + "건");
var temp=Handlebars.compile($("#temp").html());
$("#list").html(temp(data));
}
});
}
$("#list").on("click","li button",function(){
var rno=$(this).attr("rno");
if(!confirm("댓글을 삭제 하시겠습니까?")) return;
//alert(rno);
$.ajax({
type:"get",
url:"rdelete",
data:{"bno":bno,"rno":rno},
success:function(){
//alert("삭제됨");
getlist();
}
});
});
$("#btninsert").on("click",function(){
var replytext=$("#replytext").val();
if(replytext==""){
alert("내용을 입력하세요");
}else{
$.ajax({
type:"get",
url:"rinsert",
data:{"bno":bno,"replytext":replytext},
success:function(){
alert("입력됩");
$("#replytext").val("");
getlist();
}
});
}
});
</script>
</html>
'spring' 카테고리의 다른 글
파일업로드 (0) | 2019.11.29 |
---|---|
파일업로드 (0) | 2019.11.29 |
은행계좌 프로그램 만들기 (0) | 2019.11.28 |
게시글 댓글 조회수를 추가한다. (0) | 2019.11.27 |
맛집목록 만들기 (0) | 2019.11.26 |