• update_view : update form이 있는 곳
  • update : update form 에서 수정한 내용이 DB에 적용

8-1 Controller 작성

@Controller
public class BController {

	private static final Logger logger = LoggerFactory.getLogger(BController.class);
	
	@Autowired
	private SqlSession sqlSession;
	
	@RequestMapping("/list")
	public String list(Model model) {
		logger.info("list()");
		
		IDao dao = sqlSession.getMapper(IDao.class);
		model.addAttribute("list", dao.list());
		return "list";
	}

	@RequestMapping("/delete")
	public String list(HttpServletRequest request) {
		logger.info("delete()");
		
		IDao dao = sqlSession.getMapper(IDao.class);
		dao.delete(Integer.parseInt(request.getParameter("bId")));
		
		return "redirect:list";
	}

	@RequestMapping("write_view")
	public String write_view() {
		logger.info("write_view()");
		return "write_view";
	}
	
	@RequestMapping("/write")
	public String write(HttpServletRequest request) {
		logger.info("write()");
		
		IDao dao = sqlSession.getMapper(IDao.class);
		dao.write(request.getParameter("bUsername"), request.getParameter("bTitle"), request.getParameter("bContent"));
		
		return "redirect:list";
	}

	@RequestMapping("/content_view")
	public String content_view(HttpServletRequest request, Model model) {
		logger.info("content_view");
		
		IDao dao = sqlSession.getMapper(IDao.class);
		model.addAttribute("list", dao.content_view(Integer.parseInt(request.getParameter("bId"))));
		
		return "content_view";
	}

	// update_view 여기가 생각하기 조금 까다로웠다.
	// bId얻어와서 해당되는 쿼리 실행후 뷰로 전달
	@RequestMapping("/update_view")
	public String update_view(HttpServletRequest request, Model model) {
		logger.info("update_view()");
		
		IDao dao = sqlSession.getMapper(IDao.class);
		
		model.addAttribute("list", dao.update_view(Integer.parseInt(request.getParameter("bId"))));
		
		return "update_view";
	}
	
	// form 으로 받아온 정보를 얻어서 db에 update 쿼리 실행
	// model에 담을 필요없고 list로 리다이렉트
	@RequestMapping("/update")
	public String update(HttpServletRequest request) {
		logger.info("update()");
		
		IDao dao = sqlSession.getMapper(IDao.class);
		dao.update(request.getParameter("bUsername"), request.getParameter("bTitle"), request.getParameter("bContent"), Integer.parseInt(request.getParameter("bId")));
		
		return "redirect:list";
	}

}

8-2 IDao 작성

package com.javalec.bbs_prac.dao;
import java.util.ArrayList;
import com.javalec.bbs_prac.dto.BDto;

public interface IDao {

	public ArrayList<BDto> list();
	public void delete(int bId);
	public void write(String bUsername, String bTitle, String bContent);
	public BDto content_view(int bId);

	public BDto update_view(int bId);
	public void update(String bUsername, String bTitle, String bContent, int bId);
	
}

8-3 IDao.xml (Mapper) 작성

<?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="com.javalec.bbs_prac.dao.IDao">
	<select id="list" resultType="com.javalec.bbs_prac.dto.BDto">
		SELECT * FROM BOARD ORDER BY 1 DESC
	</select>
	
	<delete id="delete">
		DELETE FROM BOARD WHERE BID = #{param1}
	</delete>

	<insert id="write">
		INSERT INTO BOARD VALUES(BOARD_SEQ.NEXTVAL, #{param1}, #{param2}, #{param3}, SYSDATE, 0)
	</insert>

	<select id="content_view" resultType="com.javalec.bbs_prac.dto.BDto">
		SELECT * FROM BOARD WHERE BID = #{param1}
	</select>

	<select id="update_view" resultType="com.javalec.bbs_prac.dto.BDto">
		SELECT * FROM BOARD WHERE BID=#{param1}
	</select>
	
	<update id="update">
		UPDATE BOARD SET BUSERNAME=#{param1}, BTITLE=#{param2}, BCONTENT=#{param3} WHERE BID=#{param4} 
	</update>
	
</mapper>

8-4 update_view.jsp 작성

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

</head>
<body>

	<div class="container">
		<form action="update" method="POST">
			<div class="form-group">
				<label for="btitle">글 제목</label>
				<input type="text" class="form-control" id="bTitle" name="bTitle" value="${list.bTitle}" />
			</div>
			<div class="form-group">
				<label for="btitle">작성자</label>
				<input type="text" class="form-control" id="bUsername" name="bUsername" value="${list.bUsername}" />
			</div>
			<div class="form-group">
				<label for="bContent">글 내용</label>
				<textarea name="bContent" id="bContent" class="form-control">${list.bContent}</textarea>
			</div>
			<input type="hidden" name="bId" value="${list.bId}"/>
			
			<button type="submit" class="btn btn-default">수정하기</button>
			<a class="btn btn-default" href="list" >목록</a>
		</form>
	</div>

	<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

확인해보자.