<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 2. add my batis-config.xml: my batis의 환뎡설정 담당(중요한 파일)
3. db.properties 만들기
: db연결보들을 가지고 있는 파일 -> 공백 주의할 것 -->
<properties resource="com/smhrd/database/db.properties"> </properties>
<typeAliases>
<typeAlias type="com.smhrd.model.Member" alias ="Member"/>
<typeAlias type="com.smhrd.model.Board" alias ="Board"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/smhrd/database/MemberMapper.xml"/>
<mapper resource="com/smhrd/database/BoardMapper.xml"/>
</mappers>
</configuration>
How to get information from Board
<%@ 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>
<title>Forty by HTML5 UP</title>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" href="assetsBoard/css/main.css" />
<link rel="stylesheet" href="assetsBoard/css/board.css" />
<!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]-->
<!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->
</head>
<body>
<!-- Q16. 게시글 작성 기능(작성된 게시글은 DB에 저장) - 파일업로드 cos.jar 사용 -->
<div id = "board">
<!-- enctype
1) application/x-www-form-urlencoded
-> form 태그 안에서 보내는 요청데이터를 key값과 value값 쌍으로 보내
2) multipart/form-data
-> 사진파일 같이 용량이 큰 데이터 (파일)을 보낼 때 사용 (post방식으로 보내야함)
-->
<form action = "BoardService" method = "post" enctype="multipart/form-data">
<table id="list">
<tr>
<td>제목</td>
<td><input type="text" name ="title" > </td>
</tr>
<tr>
<td>작성자</td>
<td><input type="text" name = "writer"> </td>
</tr>
<tr>
<td colspan="2">내용</td>
</tr>
<tr>
<td colspan="2">
<input type="file" name = "filename" style="float: right;">
<textarea rows="10" name = "content" style="resize: none;"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="reset" value="초기화">
<input type="submit" value="작성하기">
</td>
</tr>
</table>
</form>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.scrolly.min.js"></script>
<script src="assets/js/jquery.scrollex.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
<script src="assets/js/main.js"></script>
</body>
</html>
package com.smhrd.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
@Getter
@ToString
public class Board {
private int num;// index
@NonNull
private String title; // title
@NonNull
private String writer;
@NonNull
private String filename;
@NonNull
private String content;
private String b_date;// upload time
}
package com.smhrd.model;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.smhrd.database.SqlSessionManager;
public class BoardDAO {
// 세션을 생성해 줄 수 있는 factory 생성
SqlSessionFactory sqlSessionFactory = SqlSessionManager.getSqlSession();
// connnection.close, sql문 실행
SqlSession sqlSession = sqlSessionFactory.openSession(true);
// 게시글 작성
public int writeBoard(Board vo) {
int cnt = 0;
try {
cnt = sqlSession.insert("com.smhrd.database.BoardMapper.writeBoard", vo);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return cnt;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace ="com.smhrd.database.BoardMapper">
<insert id="writeBoard" parameterType = "Board">
insert into web_board1 values(num_board.nextval,#{title},#{writer},#{filename},#{content},sysdate)
</insert>
</mapper>
package com.smhrd.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import com.smhrd.model.Board;
@WebServlet("/BoardService")
public class BoardService extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//MultipartRequest 객체
// 퍼일 업로드를 위한 변수 설정
//1. request객체
//2. 파일을 저장할 경로(string)
String savePath = request.getServletContext().getRealPath("file");
System.out.println("savePath : "+savePath);
//3. 파일 최대크기(int)
int maxSize = 1024*1024*10;
//4. 인코딩 방식 (String)
String encoding = "UTF-8";
//5. 파일이름 중북제거
DefaultFileRenamePolicy rename = new DefaultFileRenamePolicy();
//파일 업로드 해주는 객체 = >multipartrequest
MultipartRequest multi = new MultipartRequest(request, savePath, maxSize, encoding, rename);
//요청 데이터 받아오기
String title = multi.getParameter("title");
String writer = multi.getParameter("writer");
String filename = multi.getFilesystemName("filename");
String content = multi.getParameter("content");
Board vo = new Board(title,writer,filename,content);
System.out.println(vo.toString());
//DB에 넣은
}
}
<%
List<Board> board =new BoardDAO().allBoard();
System.out.print(board.size());
pageContext.setAttribute("board" , board);
%>
<% for(int i = 0;i<board.size();i++){ %>
<tr>
<td><%= board.get(i).getNum() %></td>
<td><a href = "BoardDetail?title=<%= board.get(i).getTitle() %>"><%=board.get(i).getTitle() %></a></td>
<td><%=board.get(i).getWriter() %></td>
<td><%=board.get(i).getB_date() %></td>
<!-- query: url ?name=value?name = value -->
</tr>
<%} %>
<c:forEach var="b" items="${board}" varStatus="s">
<tr>
<td>${s.count}</td>
<td>${b.title}</td>
<td>${b.writer}</td>
<td>${b.b_date}</td>
</tr>
</c:forEach>