https://k-develpoper.tistory.com/6
Step1 : 컨트롤러
FreeBoardController.java
package com.kh.test.freeboard;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.kh.test.freeboard.FreeBoardDTO;
@Controller
public class FreeBoardController {
@Autowired
SqlSession ss;
@RequestMapping(value="freeboard/index.do")
public String index(Model model) {
List<FreeBoardDTO> list = new ArrayList<>();
list = ss.selectList("freeboard.selectlist");
model.addAttribute("list",list);
return "freeboard/index";
}
@RequestMapping(value="freeboard/insert.do")
public String insert() {
return "freeboard/insert";
}
@RequestMapping(value="freeboard/insertProc.do")
public String insertProc(FreeBoardDTO fbd) {
ss.insert("freeboard.insertrow",fbd);
return "redirect:index.do";
}
@RequestMapping(value="freeboard/update.do")
public String update(Model model, FreeBoardDTO fbd) {
FreeBoardDTO returnfbd = ss.selectOne("freeboard.selectrow", fbd);
model.addAttribute("row", returnfbd);
return "freeboard/update";
}
@RequestMapping(value="freeboard/updateProc.do")
public String updateProc(FreeBoardDTO fbd) {
ss.update("freeboard.updaterow",fbd);
return "redirect:index.do";
}
// 추가 : 글 삭제 처리
@RequestMapping(value="freeboard/delete.do")
public String delete(String[] idx) {
String idxss="";
for(int i=0; i<idx.length;i++) {
if(i == idx.length-1) {
idxss = idxss + idx[i];
} else {
idxss = idxss + idx[i]+",";
}
}
ss.delete("freeboard.deleterow",idxss);
return "redirect:index.do";
}
}
Step2 : 맵핑
freeboardMapper.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="freeboard">
<select id="selectlist" resultType="com.kh.test.freeboard.FreeBoardDTO">
select * from freeboard
</select>
<insert id="insertrow" parameterType="com.kh.test.freeboard.FreeBoardDTO">
insert into freeboard
(title,content,rgdate)
values
(#{title},#{content},now())
</insert>
<select id="selectrow" resultType="com.kh.test.freeboard.FreeBoardDTO"
parameterType="com.kh.test.freeboard.FreeBoardDTO">
select * from freeboard where idx = #{idx}
</select>
<update id="updaterow" parameterType="com.kh.test.freeboard.FreeBoardDTO">
update freeboard
set title=#{title}, content=#{content}, rgdate=now()
where idx=#{idx}
</update>
<!-- 추가 : 글 삭제 처리 -->
<delete id="deleterow" parameterType="java.lang.String">
delete from freeboard
<where>
find_in_set(idx,#{idxss})
</where>
</delete>
</mapper>
Step3 : 화면
freeboard/index.jsp
- form 태그 추가
- 글삭제 버튼 추가
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Freeboard</title>
<meta charset="utf-8">
<%@include file="../head.jsp" %>
<script type="text/javascript">
function dodelete(){
alert("삭제");
$("#delefrm").submit();
}
</script>
</head>
<body>
<div class="container">
<div class="row">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">신입개발자</a>
</div>
<ul class="nav navbar-nav">
<li ><a href="${path}">Home</a></li>
<li class="active"><a href="${path}/freeboard/index.do">게시판</a></li>
</ul>
</div>
</nav>
</div>
<!-- 광고 -->
<div class="row">
<div class="jumbotron">
<h1>게시판</h1>
</div>
<p>mysql과 mybatis를 이용하여 게시판 구현</p>
</div>
<!--바디 -->
<form action="${path}/freeboard/delete.do" id="delefrm">
<div class="row">
<h2>게시판 목록</h2>
<table class="table table-hover table-bordered">
<tr>
<th></th>
<th>순번</th>
<th>제목</th>
<th>내용</th>
<th>날짜</th>
<th></th>
</tr>
<c:forEach items="${list}" var="fb">
<tr>
<td><input type="checkbox" name="idx" value="${fb.idx}"></td>
<td>${fb.idx }</td>
<td>${fb.title }</td>
<td>${fb.content }</td>
<td>${fb.rgdate }</td>
<td>
<a class="btn btn-primary"
href="${path}/freeboard/update.do?idx=${fb.idx}">수정</a>
</td>
</tr>
</c:forEach>
</table>
</div>
<div class="row">
<div class="col-sm-1">
<a class="btn btn-primary" href="${path}/freeboard/insert.do">글쓰기</a>
</div>
<div class="col-sm-1">
<input type="button" class="btn btn-primary" value="글삭제"
onclick="dodelete();">
</div>
</div>
</form>
</div>
</body>
</html>
[8]~[14] : 글 삭제 이벤트 추가
[40][76] : form 태그 추가
[45][54] : 체크 박스 추가
[70]~[74] : 글 삭제 버튼 추가
SpringMVC 자바스트립트, CSS 파일 적용하기 (0) | 2019.11.09 |
---|---|
Spring MVC 이미지 파일 불러오기 (0) | 2019.11.09 |
[3][Srping][Mybatis] 게시판 updete 사용 예제 (0) | 2019.11.06 |
[2][Spring][MyBatis] 게시판 insert 사용 예제 (0) | 2019.11.06 |
[1] [Spring] [Mybatis] 게시판 select 사용 예제 (0) | 2019.11.05 |
댓글 영역