https://k-develpoper.tistory.com/4
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 org.springframework.web.bind.annotation.RequestParam;
@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로 이동한다.
}
}
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>
</mapper>
Step3 : JSP
views/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>
</tr>
<c:forEach items="${list}" var="fb">
<tr>
<td>${fb.idx }</td>
<td>${fb.title }</td>
<td>${fb.content }</td>
<td>${fb.rgdate }</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>
insert.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>
<li><a href="${path}/member/index.do">회원</a></li>
</ul>
</div>
</nav>
</div>
<!-- 광고 -->
<div class="row">
<div class="jumbotron">
<h1>글쓰기</h1>
</div>
<div class="row">
<form action="${path}/freeboard/insertProc.do">
글제목
<input class="form-control" type="text" name="title">
글내용 <textarea class="form-control" rows="10" name="content"></textarea>
<input type="submit" class="btn btn-primary form-control" value="글작성"/>
</form>
</div>
</div>
</div>
</body>
</html>
[4][Srping][Mybatis] 게시판 delete 사용 예제 (0) | 2019.11.07 |
---|---|
[3][Srping][Mybatis] 게시판 updete 사용 예제 (0) | 2019.11.06 |
[1] [Spring] [Mybatis] 게시판 select 사용 예제 (0) | 2019.11.05 |
[Spring MVC Project] mybatis DB 연동 2 (0) | 2019.11.04 |
[Spring MVC project] mybatis(마이바티스) DB(데이터베이스)연동 1 (0) | 2019.11.03 |
댓글 영역