게시판 Table 생성


mysql command를 키고 아래 코드를 입력합니다.

USE BBS;
 
CREATE TABLE IF NOT EXISTS bbs(
    bbs_id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    bbs_title VARCHAR(32) NOT NULL,
    bbs_content VARCHAR(1000) NOT NULL,
    author VARCHAR(32) NOT NULL,
    date DATETIME DEFAULT CURRENT_TIMESTAMP
    );


BbsDto 생성


Dto 패키지 아래에 BbsDto를 생성해줍니다.

package com.app.app.Dto;

public class BbsDto {
	private String bbs_id;
	private String bbs_title;
	private String bbs_content;
	private String author;
	private String date;
	public String getBbs_id() {
		return bbs_id;
	}
	public void setBbs_id(String bbs_id) {
		this.bbs_id = bbs_id;
	}
	public String getBbs_title() {
		return bbs_title;
	}
	public void setBbs_title(String bbs_title) {
		this.bbs_title = bbs_title;
	}
	public String getBbs_content() {
		return bbs_content;
	}
	public void setBbs_content(String bbs_content) {
		this.bbs_content = bbs_content;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
}

index.jsp 수정


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>Index</title>
</head>
<body>
Login ID:
<c:out value="${sessionScope.user_id }" default="null"/>
<table border="1" align="center" width="500" height="300">
	<tr>
		<td colspan="4" height="100" align="center">Main page</td>
	</tr>
	<tr>
		<td colspan="4" height="100" align="center"><a href="write">글 쓰기</a></td>
	</tr>
	<tr>
	<c:if test="${empty sessionScope.user_id}">
		<td colspan="2" align="center" width="250"><a href="Login">로그인</a></td>
	</c:if>
	
	<c:if test="${!empty sessionScope.user_id}">
		<td colspan="2" align="center" width="250"><a href="Logout">로그아웃</a></td>
	</c:if>
		<td colspan="2" align="center" width="250"><a href="Register">회원가입</a></td>
	</tr>
</table>
</body>
</html>


wirte.jsp 작성


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="WriteAct">
		<table>
			<tr>
				<td>
					<table width="100%" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td>글쓰기</td>
						</tr>
					</table>
					<table>
						<tr>
							<td>&nbsp;</td>
							<td align="center">제목</td>
							<td><input name="bbs_title" size="50" maxlength="100"></td>
							<td>&nbsp;</td>
						</tr>
						<tr height="1">
							<td colspan="4"></td>
						</tr>
						<tr>
							<td>&nbsp;</td>
							<td align="center">내용</td>
							<td><textarea name="bbs_content" cols="50" rows="13"></textarea></td>
							<td>&nbsp;</td>
						</tr>
						<tr height="1">
							<td colspan="4"></td>
						</tr>
						<tr height="1">
							<td colspan="4"></td>
						</tr>
						<tr align="center">
							<td>&nbsp;</td>
							<td colspan="2"><input type=button value="등록">
							<td>&nbsp;</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

BDao , BbsDao 생성


Dao 패키지 아래에 BDao를 생성합니다.

package com.app.app.Dao;

import java.util.ArrayList;
import java.util.Map;
import com.app.app.Dto.UserDto;

public interface Bdao {
	public void Write(Map map);
}

BbsDao 도 생성해줍니다

package com.app.app.Dao;

import java.util.Map;

public class BbsDao implements BDao{

	@Override
	public void Write(Map map) {
	}

}

BDao.xml 생성


mapper 패키지 아래에 BDao.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="com.app.app.Dao.BDao">

	<insert parameterType="Map" id="Write">
	INSERT INTO
		BBS (bbs_title, bbs_content, author)
		VALUES(#{bbs_title}, #{bbs_content}, #{author}) 
	</insert>
</mapper>

BbsController 수정


package com.app.app.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.app.Dao.BDao;
import com.app.app.Dao.BbsDao;
import com.app.app.Dto.BbsDto;

import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.simple.JSONObject;




@Controller
public class BbsController {
	@Autowired
	private SqlSession sqlSession;
	
	private static final Logger logger = LoggerFactory.getLogger(AppController.class);
	
	
	@RequestMapping("/write")
	public String Write(Model model, HttpSession session, HttpServletResponse response) throws IOException {
		if(session.getAttribute("user_id") == null) {
			System.out.println("로그인세션 없음");
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<script>alert('잘못된 접근'); history.back();</script>");
			out.flush();
		}
			
	return "write";	
	}
	@RequestMapping("/WriteAct")
	public String Write(HttpSession session, HttpServletRequest request) {
		Map map = new HashMap();
		map.put("bbs_title", request.getParameter("bbs_title"));
		map.put("bbs_content", request.getParameter("bbs_content"));
		map.put("author", session.getAttribute("user_id"));
		BDao dao = sqlSession.getMapper(BDao.class);
		dao.Write(map);
		return "index";
	}
}

이제 글쓰기를 해보자

mysql command에서 

use bbs;

select * from bbs; 를 입력하여 확인해보자

위와 같이 나오면 성공한것이다

+ Recent posts