JAVA/SPRING

[SPRING] SRPING 게시판 (7) - CRUD [게시글 삭제]

KMSEOP 2020. 3. 16. 16:43
728x90

Mapper 작성

// mapper/BoardMapper.xml

...

	<!-- 추가 -->
	<delete id="delete">
		DELETE FROM board
		WHERE bno = #{bno}
	</delete>

 

Dao 작성

// Dao/BoardDao.java

...
	
    //추가
	public void deletePosts(int bno) throws Exception{
		
		sqlSession.delete(Namespace + ".delete", bno);
	}

 

테스트 케이스 작성

// test/boardDaoTest.java

...

	//추가
	@Test
	public void testDeletePosts() throws Exception{
		
		int bno = 3;
		boardDao.deletePosts(bno);
	}

 

Service 작성

//Service/BoardService.java

...

	//추가
	public void deletePosts(int bno) throws Exception{
		
		boardDao.deletePosts(bno);
	}

 

Controller 작성

//Controller/BoardController.java

...

	//추가
	//게시글 삭제
	@RequestMapping(value="/posts/{bno}", method=RequestMethod.DELETE)
	public String delete(@PathVariable int bno) throws Exception{
		
		boardService.deletePosts(bno);
		
		return "redirect:/boardList";
	}

 

View 작성

<!-- views/detail.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="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
    <style>
      #list{
        border: 1px solid black;
      }
      th, td {
        border: 1px solid #444444;
      }
    </style>
  </head>
  <body>
    <table id="list">
      <thead>
        <tr>
          <th>NO</th>
          <th>TITLE</th>
          <th>WRITER</th>
          <th>COUNT</th>
          <th>DATE</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>${post.bno}</td>
          <td>${post.title}</td>
          <td>${post.writer}</td>
          <td>${post.count}</td>
          <td>${post.createTime}</td>
        </tr>
      </tbody>
    </table>
    <form action="/board/posts/${post.bno}" method="POST">
      <input type="hidden" name="_method" value="DELETE" />
      <input type="submit" value="DELETE">
    </form>
  </body>
</html>

<!-- Jquery -->
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
728x90