https://k-develpoper.tistory.com/5
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";
// views/freeboard/index.jsp로 이동한다.
}
//추가 : 업데이트 화면 제공
@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";
}
}
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>
</mapper>
Step3 : 화면
freeboard/index.jsp
- 글 수정 버튼 추가
<%@ 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" %>
</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>
<!--바디 -->
<div class="row">
<h2>게시판 목록</h2>
<table class="table table-hover table-bordered">
<tr>
<th>순번</th>
<th>제목</th>
<th>내용</th>
<th>날짜</th>
<th></th><!-- 추가 -->
</tr>
<c:forEach items="${list}" var="fb">
<tr>
<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>
</div>
</body>
</html>
freeboard/update.jsp
<%@ 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>글수정</title>
<meta charset="utf-8">
<%@include file="../head.jsp" %>
</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>
<div class="row">
<form action="${path}/freeboard/updateProc.do">
<input type="hidden" name="idx" value="${row.idx }">
글제목 <input class="form-control" type="text" name="title" value="${row.title }">
글내용 <textarea class="form-control" rows="10" name="content">${row.content}</textarea>
<input type="submit" class="btn btn-primary form-control" value="글수정"/>
</form>
</div>
</div>
</div>
</body>
</html>
Spring MVC 이미지 파일 불러오기 (0) | 2019.11.09 |
---|---|
[4][Srping][Mybatis] 게시판 delete 사용 예제 (0) | 2019.11.07 |
[2][Spring][MyBatis] 게시판 insert 사용 예제 (0) | 2019.11.06 |
[1] [Spring] [Mybatis] 게시판 select 사용 예제 (0) | 2019.11.05 |
[Spring MVC Project] mybatis DB 연동 2 (0) | 2019.11.04 |
댓글 영역