개발햄비

[java] springBoot + mybatis + mysql 게시판 추가,삭제,갱신 기능 본문

개발/java

[java] springBoot + mybatis + mysql 게시판 추가,삭제,갱신 기능

개발햄 2019. 10. 27. 22:34

1. 게시판 추가 , 삭제, 갱신 기능 넣기

** 1. boardController 추가**

package com.example.demo.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.bean.BoardData;
import com.example.demo.service.BoardService;

@Controller
public class BoardController {
	@Resource(name="com.example.demo.service.BoardService")
    BoardService boardService;
	
	// get boardList
    @RequestMapping("/boardList") 
    private String boardList(Model model) throws Exception{
        
        model.addAttribute("boardList", boardService.getBoardList());
        
        return "boardList"; 
    }
    
    // get boardDetail
    @RequestMapping("/boardDetail/{boardNum}") 
    private String boardDetail(@PathVariable int boardNum, Model model) throws Exception{
        
        model.addAttribute("boardDetail", boardService.getBoardDetail(boardNum));
        
        return "boardDetail";
    }
    
    @RequestMapping("/insert") //게시글 작성폼 호출  
    private String boardInsertForm(){
        
        return "insert";
    }
    
    @RequestMapping("/insertProc")
    private String boardInsertProc(HttpServletRequest request) throws Exception{
        
    	BoardData board = new BoardData();
        
        boardService.boardInsert(board);
        return "redirect:/boardList";
    }
    
    @RequestMapping("/update/{boardNum}") //게시글 수정폼 호출  
    private String boardUpdateForm(@PathVariable int boardNum, Model model) throws Exception{
        
        model.addAttribute("boardDetail", boardService.getBoardDetail(boardNum));
        
        return "update";
    }
    
    @RequestMapping("/updateProc")
    private void boardUpdateProc(HttpServletRequest request) throws Exception{
        
       	BoardData board = new BoardData();
        
        boardService.boardUpdate(board);
    }
 
    @RequestMapping("/delete/{boardNum}")
    private String boardDelete(@PathVariable int boardNum) throws Exception{
        
    	boardService.boardDelete(boardNum);
        
        return "redirect:/boardList";
    }

}

 

* retrun type이 int가 아닌 void로 수정해주세요 

*에러 클릭후 create method 'boardUpdate(BoardData)'를 누르면 자동으로 메소드가 생성됩니다.

 

 

** 2.boardService 추가**

*자동으로 생성하면 위와 같이 코드가 생성됩니다. TODO 부분을 아래와 같이 코드를 넣어줍니다

 

** 3.boardMapper.java 추가**

*위와 같이 다시 에러를 클릭하여 메소드 자동생성을 해줍니다

*자동생성 헀다면 위와 같이 코드가 생깁니다.

 

** 4. boardMapper.xml 추가**

boardMapper.java의 메소드 명과 id를 통해서 매핑을 해줍시다.

 

** 5. insert.jsp, update.jsp 추가**

insert.jsp
update.jsp

** 6. 데이터 통신 확인**

http://localhost:8080/update/1

불러오는 중입니다...

http://localhost:8080/insert

불러오는 중입니다...

2. 게시글 작성 페이지 디자인 하기

  1. bootStrap.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">
<!-- jQuery -->
<script src="//code.jquery.com/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<!-- BootStrap theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">

  1. insert.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 Form</title>
</head>
<body>

<h2> Create Board Info </h2>

<div class="container">
    <form action="/insertProc" method="post">
      <div class="form-group">
        <label for="subject">title</label>
        <input type="text" class="form-control" id="subject" name="subject" placeholder="제목을 입력하세요.">
      </div>
      <div class="form-group">
        <label for="writer">writer</label>
        <input type="text" class="form-control" id="writer" name="writer" placeholder="내용을 입력하세요.">
      </div>
      <div class="form-group">
        <label for=content">content</label>
        <textarea class="form-control" id="content" name="content" rows="3"></textarea>
      </div>
      <button type="submit" class="btn btn-primary">OK</button>
    </form>
</div>

<%@ include file="bootstrap.jsp" %>
</body>
</html>
  1. boardController.java 편집

  
    @RequestMapping("/insertProc")
    private String boardInsertProc(HttpServletRequest request) throws Exception{
        
    	BoardData board = new BoardData();
        
        board.setSubject(request.getParameter("subject"));
        board.setWriter(request.getParameter("writer"));
        board.setContent(request.getParameter("content"));
        
        boardService.boardInsert(board);
        return "redirect:/boardList";
    }

 

 

3. 게시글 작성 데이터 확인

 

 

 

게시글 작성 후 자동으로 게시글 목록으로 이동하도록 설정하였습니다.