πŸ’»

μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬ - [Spring] MVC ν”„λ‘œμ νŠΈ(5) : νŒŒμΌμ—…λ‘œλ“œ λ³Έλ¬Έ

KITRI/SPRING

μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬ - [Spring] MVC ν”„λ‘œμ νŠΈ(5) : νŒŒμΌμ—…λ‘œλ“œ

λ˜νš¨λ‹ˆ 2020. 7. 30. 10:32

κ²Œμ‹œνŒ νŒŒμΌμ—…λ‘œλ“œ

 

 

WEB-INF > lib 에 νŒŒμΌμ—…λ‘œλ“œλ₯Ό μœ„ν•œ .jar파일 μΆ”κ°€

 

μž‘μ—…μˆœμ„œ : Setting -> Controller -> Service -> ServiceImp -> Dao -> mapper.xml -> DaoImp 

 

SpringController-servlet.xml 

fileBoardController μΆ”κ°€

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
   
   <!-- AOP -->
   <bean id="hAspect" class="com.java.aop.HAspect"/>
   <aop:config>
      <aop:aspect id="exAspect" ref="hAspect">
         <aop:pointcut expression="within(com.java.*)" id="pMethod"/>
         <aop:around method="advice" pointcut-ref="pMethod"/>
      </aop:aspect>
   </aop:config>
   
   <!-- HandlerMapping / UIμš”μ²­μ΄ λ“€μ–΄μ˜€λ©΄ ν•΄λ‹Ή 클래슀λ₯Ό 찾아라 -->
   <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="mappings">
         <props>
            <prop key="/member/*.do">memberController</prop>
            <prop key="/fileBoard/*.do">fileBoardController</prop>
         </props>
      </property>
   </bean>

   
   <!-- ViewResolver : MVC RequestDispatcher / forward -->
   <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/"/>
      <property name="suffix" value=".jsp"></property>
   </bean>
</beans>

 


View

 

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>Insert title here</title>
</head>
<body>
	<c:set var="root" value="${pageContext.request.contextPath}"/>
	<c:set var= "id" value="${id}" scope="session"/>
	<a href="${root}/member/test.do"> Spring MVC Start!!! </a>	

	<c:if test="${memberLevel==null}"> 
	<h3>νšŒμ›κ΄€λ¦¬</h3>
	<a href="${root}/member/register.do">νšŒμ›κ°€μž…</a>
	<h3>둜그인</h3>
	<a href="${root}/member/login.do">둜그인</a>
	</c:if>
	
	<c:if test="${memberLevel!=null}"> 
		<h3>νšŒμ›μˆ˜μ •</h3>
		<a href="${root}/member/update.do">νšŒμ›μˆ˜μ •</a>
		<h3>λ‘œκ·Έμ•„μ›ƒ</h3>
		<a href="${root}/member/logout.do">λ‘œκ·Έμ•„μ›ƒ</a>
	</c:if>
	
	<c:if test="${memberLevel=='MA'}"> 
		<h3>κ΄€λ¦¬μžνŽ˜μ΄μ§€</h3>
	</c:if>
	
	<h3>νŒŒμΌκ²Œμ‹œνŒ</h3>
	<a href="${root}/fileBoard/write.do">νŒŒμΌκ²Œμ‹œνŒ μ“°κΈ°</a>
	<br/><br/>
	<a href="${root}/fileBoard/list.do">νŒŒμΌκ²Œμ‹œνŒ 보기</a>
</body>
</html>

 

write.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>
<c:set var="root" value="${pageContext.request.contextPath }" />
<html>
<head>
<meta charset="UTF-8">
<title>νŒŒμΌλ³΄λ“œ κΈ€μž‘μ„± νŽ˜μ΄μ§€</title>
<link rel="stylesheet" href="${root}/css/board/boardStyle.css" />
<script type="text/javascript" >
	function boardCheck(obj){
		//alert("OK");	
	}
	function replyFunc(root, boardNumber, groupNumber, sequenceNumber, sequenceLevel){
		var url = root + "/fileboard/write.do?boardNumber=" + boardNumber +
				"&groupNumber=" + groupNumber + "&sequenceNumber=" + sequenceNumber +
				"&sequenceLevel=" + sequenceLevel;
		
		location.href = url;
	}
	function delFunc(root, boardNumber, pageNumber){
		var url = root+ "/fileboard/delete.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
		
		location.href = url;
	
		/*var value = confirm("μ‚­μ œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?");
		
		if(value == true){
			var url = root+ "/board/deleteOk.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
			alert(url);
		}else{
			alert("μ·¨μ†Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
		}*/
	}
	function updFunc(root, boardNumber, pageNumber){
		var url = root + "/fileboard/update.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;		
		location.href = url;
	}
</script>
</head>
<body>
${boardNumber}, ${groupNumber}, ${sequenceNumber}, ${sequenceLevel}
	<form id="createform" action="${root}/fileBoard/writeOk.do" method="post"
		onsubmit="return boardCheck(this)" enctype="multipart/form-data">
		<div id="title"><a href="${root}/fileboard/list.do">κΈ€λͺ©λ‘</a></div>
			
		<div class="menu" style="border-bottom-width: 0px;">
			<input type="hidden" name="boardNumber" value="${boardNumber}" /> 
			<input type="hidden" name="groupNumber" value="${groupNumber}" /> 
			<input type="hidden" name="sequenceNumber" value="${sequenceNumber}" /> 
			<input type="hidden" name="sequenceLevel" value="${sequenceLevel}" />
			
			<div id="id">μž‘μ„±μž</div>
			<span> 
				<input type="text" name="writer" size="12" />
			</span>
		</div>

		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">제λͺ©</div>
			<span> 
				<input type="text" name="subject" size="50" />
			</span>
		</div>

		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">이메일</div>
			<span> 
				<input type="email" name="email" size="50" />
			</span>
		</div>

		<div class="content" style="border-bottom-width: 0px;">
			<div id="text">λ‚΄μš©</div>
			<span> 
				<textarea name="content" rows="12" cols="65"></textarea>
			</span>
		</div>

		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">λΉ„λ°€λ²ˆν˜Έ</div>
			<span> 
				<input type="password" name="password" size="30" />
			</span>
		</div>

		<!-- 파일 μ—…λ‘œλ“œ -->
		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">파일λͺ…</div>
			<span> 
				<input type="file" name="file" size="50" />
			</span>
		</div>
		
		<div class="menu" style="border-bottom-width: 3px; text-align: center;">
			<span> 
				<input type="submit" value="κΈ€μ“°κΈ°" /> 
				<input type="reset" value="λ‹€μ‹œμž‘μ„±" /> 
				<input type="button" value="λͺ©λ‘λ³΄κΈ°" onclick="location.href='${root}/fileboard/list.do'"/>
			</span>
		</div>
	</form>
</body>
</html>

 

writeOk.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>
<c:set var = "root" value = "${pageContext.request.contextPath}"/>
<html>
<head>
<meta charset="UTF-8">
<title>νŒŒμΌλ³΄λ“œ κΈ€μ“°κΈ° μ™„λ£Œ</title>
</head>
<body>
   <c:if test="${check>0}">
      <script type="text/javascript">
         alert("글쓰기에 μ„±κ³΅ν•˜μ…¨μŠ΅λ‹ˆλ‹€.")
         location.href = "${root}/fileBoard/list.do";
      </script>
   </c:if>
   
   <c:if test="${check==0}">
      <script type="text/javascript">
         alert("κΈ€μ“°κΈ°λ₯Ό μ‹€νŒ¨.")
         location.href = "${root}/fileBoard/write.do";
      </script>
   </c:if>
</body>
</html>

 

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<c:set var = "root" value = "${pageContext.request.contextPath}"/>
<html>
<head>
<meta charset="UTF-8">
<title>νŒŒμΌλ³΄λ“œ λͺ©λ‘ νŽ˜μ΄μ§€</title>
</head>
<body>
	<div align="right">
		<table>
			<tr>
				<td>
					<a href="${root}/fileBoard/write.do">κΈ€μ“°κΈ°</a>
				</td>
			</tr>
		</table>
	</div>
	
	<c:if test="${count==0||boardList.size()==0}">
		<div align="center">
			κ²Œμ‹œνŒμ— μ €μž₯된 글이 μ—†μŠ΅λ‹ˆλ‹€.
		</div>
	</c:if>
	
	<c:if test="${count>0}">
		<div align="center">
			<table border="1">
				<tr>
					<td align="center" width="50"><strong>번호</strong></td>
					<td align="center" width="250"><strong>제λͺ©</strong></td>
					<td align="center" width="70"><strong>μž‘μ„±μž</strong></td>
					<td align="center" width="150"><strong>μž‘μ„±μΌ</strong></td>
					<td align="center" width="50"><strong>쑰회수</strong></td>
				</tr>
				
				 <c:forEach var="boardDto" items="${boardList}">
					 <tr>
					 	<!-- κΈ€λ²ˆν˜Έ -->
					 	<td align="center" width="50">${boardDto.boardNumber}</td>
						
						<td align="center" width="250">
							<!-- λ‹΅κΈ€ -->
							<c:if test="${boardDto.sequenceLevel > 0}">
								<c:forEach begin="0" end="${boardDto.sequenceLevel}">
									&nbsp;&nbsp;
								</c:forEach>
								[λ‹΅κΈ€]
							</c:if>
							
							<!-- 제λͺ© -->
							<a href="${root}/fileBoard/read.do?boardNumber=${boardDto.boardNumber}&pageNumber=${currentPage}">${boardDto.subject}</a>
						</td>
						<!-- μž‘μ„±μž -->
						<td align="center" width="70">${boardDto.writer}</td>
						
						<!-- μž‘μ„±μΌ -->
						<td align="center" width="150">
							<fmt:formatDate value="${boardDto.writeDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
						</td>
						
						<!-- 쑰회수 -->
						<td align="center" width="50">${boardDto.readCount}</td>
					 </tr>
				 </c:forEach>
			</table>
		</div>
	</c:if>
	
	<div align="center">
		<%-- 
			1. ν•œ νŽ˜μ΄μ§€λ‹Ή κ²Œμ‹œλ¬Ό 수 : 10 
			2. 총 νŽ˜μ΄μ§€ 수: 10page = μ „μ²΄λ ˆμ½”λ“œ 수 100/ ν•œ νŽ˜μ΄μ§€λ‹Ή κ²Œμ‹œλ¬Ό 수 10 
						 11page = μ „μ²΄λ ˆμ½”λ“œ 수 101/ ν•œ νŽ˜μ΄μ§€λ‹Ή κ²Œμ‹œλ¬Ό 수 10
						 
			3. νŽ˜μ΄μ§€λ²ˆν˜Έ λΈ”λŸ­ : 10 
							[1][2][3][4][5]...[10]
							μš”μ²­νŽ˜μ΄μ§€λ²ˆν˜Έ 5이면 μ‹œμž‘λ²ˆν˜Έ 1, κΈ€λ²ˆν˜Έ 10
							
							pageBlock, currentPage : μ‹œμž‘λ²ˆν˜Έ, 끝번호
							
							int startPage=(int)((currentPage-1)/pageBlock)*pageBlock + 1
												(3-1)/10 = 0*10 + 1 = 1 
												(33-1)/10= 3*10 + 1 = 31
							int endPage=startPage+pageBlock-1
											1+10-1 = 10
											31+10-1 = 40
											
			4. boardSize, currentPage, count: Command Data
		--%>
		<fmt:parseNumber var="pageCount" value="${count/boardSize+(count%boardSize==0? 0:1)}" integerOnly="true"/>
		<c:set var="pageBlock" value="${2}"/>
		
		<fmt:parseNumber var="result" value="${(currentPage-1)/pageBlock}" integerOnly="true"/>
		<c:set var="startPage" value="${result*pageBlock+1}"/>
		<c:set var="endPage" value="${startPage+pageBlock-1}"/>
		
		<c:if test="${endPage > pageCount}">
			<c:set var="endPage" value="${pageCount}"/>
		</c:if>
		<%-- ${startPage}, ${endPage}--%>
		
		<c:if test="${startPage > pageBlock}">
			<a href="${root}/fileBoard/list.do?pageNumber=${startPage-pageBlock}">[이전]</a>
		</c:if>
		
		<c:forEach var="i" begin="${startPage}" end="${endPage}">
			<a href="${root}/fileBoard/list.do?pageNumber=${i}">[${i}]</a>
		</c:forEach>
		
		<c:if test="${endPage < pageCount }">
			<a href="${root}/fileBoard/list.do?pageNumber=${startPage+pageBlock}">[λ‹€μŒ]</a>
		</c:if>
	</div>
</body>
</html>

 

read.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<c:set var="root" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta charset="UTF-8">
<title>νŒŒμΌλ³΄λ“œ 읽기 νŽ˜μ΄μ§€</title>
<link rel="stylesheet" href="${root}/css/board/boardStyle.css" />
<script type="text/javascript">
	function boardCheck(obj){
		//alert("OK");	
	}
	function replyFunc(root, boardNumber, groupNumber, sequenceNumber, sequenceLevel){
		var url = root + "/fileBoard/write.do?boardNumber=" + boardNumber +
				"&groupNumber=" + groupNumber + "&sequenceNumber=" + sequenceNumber +
				"&sequenceLevel=" + sequenceLevel;
		
		location.href = url;
	}
	function delFunc(root, boardNumber, pageNumber){
		var url = root+ "/fileBoard/delete.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
		
		location.href = url;

		/*var value = confirm("μ‚­μ œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?");
		
		if(value == true){
			var url = root+ "/board/deleteOk.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;
			alert(url);
		}else{
			alert("μ·¨μ†Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
		}*/
	}
	function updFunc(root, boardNumber, pageNumber){
		var url = root + "/fileBoard/update.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;		
		location.href = url;
	}
</script>
</head>
<body>
	<div id="createform">
		<div class="menu" style="border-bottom-width: 0px;"> 
			<div id="id">κΈ€λ²ˆν˜Έ</div>
			${boardDto.boardNumber}
		</div>
		
		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">μž‘μ„±μž</div>
			${boardDto.writer}
		</div>
		
		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">쑰회수</div>
			${boardDto.readCount}
		</div>
		
		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">μž‘μ„±μΌ</div>
			<fmt:formatDate value="${boardDto.writeDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
		</div>

		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">제λͺ©</div>
			${boardDto.subject}
		</div>

		<div class="content" style="border-bottom-width: 0px;">
			<div id="text">λ‚΄μš©</div>
			${boardDto.content}
		</div>
		
		<c:if test="${boardDto.fileSize!=0}">
			<div class="menu" style="border-bottom-width: 0px;">
				<div id="id">파일λͺ…</div>
				<a href="${root}/fileBoard/download.do?boardNumber=${boardDto.boardNumber}">${boardDto.fileName}</a>
			</div>
		</c:if>

		<div class="menu" style="border-bottom-width: 3px; text-align: center;">
			<input type="button" value="κΈ€μˆ˜μ •" onclick="updFunc('${root}','${boardDto.boardNumber}','${pageNumber}')"/>	
			<input type="button" value="κΈ€μ‚­μ œ" onclick="delFunc('${root}','${boardDto.boardNumber}','${pageNumber}')"/>	
			<input type="button" value="λ‹΅κΈ€" onclick="replyFunc('${root}','${boardDto.boardNumber}','${boardDto.groupNumber}','${boardDto.sequenceNumber}','${boardDto.sequenceLevel}')"/>	
			<input type="button" value="κΈ€λͺ©λ‘" onclick="location.href='${root}/fileBoard/list.do?pageNumber=${pageNumber}'"/>	
		</div>
	</div>
</body>
</html>

 

update.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>
<c:set var="root" value="${pageContext.request.contextPath }" />
<html>
<head>
<meta charset="UTF-8">
<title>νŒŒμΌλ³΄λ“œ μˆ˜μ • νŽ˜μ΄μ§€</title>
<link rel="stylesheet" href="${root}/css/board/boardStyle.css" />
<script type="text/javascript">
	function boardCheck(obj){
		//alert("OK");	
	}
	function updFunc(root, boardNumber, pageNumber){
		var url = root + "/fileBoard/update.do?boardNumber=" + boardNumber + "&pageNumber=" + pageNumber;		
		location.href = url;
	}
</script>
</head>
<body>
	<form id="createform" action="${root}/fileBoard/updateOk.do" method="post" enctype="multipart/form-data">
		<div id="title"><a href="${root}/fileBoard/list.do">κΈ€λͺ©λ‘</a></div>

		${boardNumber}
		<div class="menu" style="border-bottom-width: 0px;">
			<input type="hidden" name="boardNumber" value="${boardDto.boardNumber}"/>
			<input type="hidden" name="groupNumber" value="${boardDto.groupNumber}" /> 
			<input type="hidden" name="sequenceNumber" value="${boardDto.sequenceNumber}" /> 
			<input type="hidden" name="sequenceLevel" value="${boardDto.sequenceLevel}" />
			<input type="hidden" name="pageNumber" value="${pageNumber}"/>
			
			<div id="id">μž‘μ„±μž</div>
			<span>
				${boardDto.writer}
				<input type="hidden" name="writer" value="${boardDto.writer}"/>
			</span>
		</div>

		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">제λͺ©</div>
			<span> 
				<input type="text" name="subject" size="50" value="${boardDto.subject}"/>
			</span>
		</div>

		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">이메일</div>
			<span> 
				<input type="email" name="email" size="50" value="${boardDto.email}"/>
			</span>
		</div>

		<div class="content" style="border-bottom-width: 0px;">
			<div id="text">λ‚΄μš©</div>
			<span> 
				<textarea name="content" rows="12" cols="65" >${boardDto.content}</textarea>
			</span>
		</div>

		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">λΉ„λ°€λ²ˆν˜Έ</div>
			<span> 
				<input type="password" name="password" size="30" value="${boardDto.password}"/>
			</span>
		</div>
		
		<!-- 파일λͺ… -->
		<div class="menu" style="border-bottom-width: 0px;">
			<div id="id">파일λͺ…</div>
			<span> 
				${boardDto.fileName}
				&nbsp;&nbsp;&nbsp;
				<input type="file" name="file" size="50"/>
			</span>
		</div>

		<div class="menu" style="border-bottom-width: 3px; text-align: center;">
			<span> 
				<input type="submit" value="μˆ˜μ •μ™„λ£Œ"/> 
				<input type="reset" value="λ‹€μ‹œμž‘μ„±" /> 
				<input type="button" value="λͺ©λ‘λ³΄κΈ°" onclick="location.href='${root}/fileBoard/list.do'"/>
			</span>
		</div>
	</form>
</body>
</html>

 

updateOk.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>
<c:set var = "root" value = "${pageContext.request.contextPath}"/>
<html>
<head>
<meta charset="UTF-8">
<title>νŒŒμΌλ³΄λ“œ μˆ˜μ • μ™„λ£ŒνŽ˜μ΄μ§€</title>
</head>
<body>	
	<c:if test="${check > 0}">
		<script type="text/javascript">
			alert("μˆ˜μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
			location.href="${root}/fileBoard/list.do?";
		</script>
	</c:if>
	<c:if test="${check == 0}">
		<script type="text/javascript">
			alert("μˆ˜μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
			location.href="${root}/fileBoard/update.do?";
		</script>
	</c:if>
</body>
</html>

 

delete.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>
<c:set var = "root" value = "${pageContext.request.contextPath}"/>
<html>
<head>
<meta charset="UTF-8">
<title>νŒŒμΌλ³΄λ“œ μ‚­μ œ νŽ˜μ΄μ§€</title>
</head>
<body>
	<div align="center">
		<form action="${root}/fileBoard/deleteOk.do" method="post">
			<input type="hidden" name="boardNumber" value="${boardNumber}"/>
			<input type="hidden" name="pageNumber" value="${pageNumber}"/>
			
			<div>λΉ„λ°€λ²ˆν˜Έλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”.</div>
			<div>
				<input type="password" name="password"/>
			</div>
			<div>
				<input type="submit" value="κΈ€μ‚­μ œ"/>
				<input type="button" value="λͺ©λ‘λ³΄κΈ°" onclick="location.href='${root}/fileBoard/list.do?pageNumber=${pageNumber}'"/>
			</div>
		</form>
	</div>
</body>
</html>

 

deleteOk.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>
<c:set var = "root" value = "${pageContext.request.contextPath}"/>
<html>
<head>
<meta charset="UTF-8">
<title>νŒŒμΌλ³΄λ“œ μ‚­μ œ μ™„λ£ŒνŽ˜μ΄μ§€</title>
</head>
<body>
	<c:if test="${check > 0}">
		<script type="text/javascript">
			alert("μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
			location.href="${root}/fileBoard/list.do?pageNumber=${pageNumber}";
		</script>
	</c:if>
	<c:if test="${check == 0}">
		<script type="text/javascript">
			alert("μ‚­μ œλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
			location.href="${root}/fileBoard/list.do?pageNumber=${pageNumber}";
		</script>
	</c:if>
</body>
</html>

 


 

fileBoard.xml

맀핑 μ„€μ •

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- URL/Function -->
	<bean id="propertiesMethodNameResolver"
		class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
		<property name="mappings">
			<props>
				<prop key="/fileBoard/write.do">fileBoardWrite</prop>
				<prop key="/fileBoard/writeOk.do">fileBoardWriteOk</prop>
				<prop key="/fileBoard/list.do">fileBoardList</prop>
				<prop key="/fileBoard/read.do">fileBoardRead</prop>
				<prop key="/fileBoard/download.do">fileDownLoad</prop>
				<prop key="/fileBoard/update.do">fileBoardUpdate</prop>
				<prop key="/fileBoard/updateOk.do">fileBoardUpdateOk</prop>
				<prop key="/fileBoard/delete.do">fileBoardDelete</prop>
				<prop key="/fileBoard/deleteOk.do">fileBoardDeleteOk</prop>
			</props>
		</property>
	</bean>

	<!-- FileBoardController -->
	<bean id="fileBoardController" class="com.java.fileboard.controller.FileBoardController">
		<property name="fileBoardService" ref="fileBoardServiceImp"></property>
		<property name="methodNameResolver" ref="propertiesMethodNameResolver"></property>
	</bean>

	<!-- FileBoardService -->
	<bean id="fileBoardServiceImp" class="com.java.fileboard.service.FileBoardServiceImp">
		<property name="fileBoardDao" ref="fileBoardDaoImp"></property>
	</bean>

	<!-- FileBoardDAO -->
	<bean id="fileBoardDaoImp" class="com.java.fileboard.dao.FileBoardDaoImp">
		<constructor-arg ref="sqlSessionTemplate"></constructor-arg>
	</bean>
	
	<!-- FileUpload -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="502400"/> <!-- 102400 -->
		<property name="defaultEncoding" value="utf-8"/>
	</bean>
</beans>

 

FileBoardController.java

package com.java.fileboard.controller;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.java.fileboard.dto.FileBoardDto;
import com.java.fileboard.service.FileBoardService;

public class FileBoardController extends MultiActionController{
	private FileBoardService fileBoardService;

	public FileBoardController() {}

	public FileBoardController(FileBoardService fileBoardService) {
		this.fileBoardService = fileBoardService;
	}

	public void setFileBoardService(FileBoardService fileBoardService) {
		this.fileBoardService = fileBoardService;
	}
	
	public ModelAndView fileBoardWrite(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", req);
		
		fileBoardService.fileBoardWrite(mav);
		return mav;
	}
	
	public ModelAndView fileBoardWriteOk(HttpServletRequest req, HttpServletResponse res, FileBoardDto fileBoardDto) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", req);
		mav.addObject("fileBoardDto", fileBoardDto);
		
		fileBoardService.fileBoardWriteOk(mav);
		return mav;
	}
	
	public ModelAndView fileBoardList(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", request);
		
		fileBoardService.fileBoardList(mav);
		return mav;
		
	}
	
	public ModelAndView fileBoardRead(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", req);
		
		fileBoardService.fileBoardRead(mav);
		
		return mav;
	}
	
	public void fileDownLoad(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", req);
		mav.addObject("response", res);
		
		fileBoardService.fileDownLoad(mav);
	}
	
	public ModelAndView fileBoardUpdate(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", req);
		
		fileBoardService.fileBoardUpdate(mav);
		
		return mav;
	}
	
	public ModelAndView fileBoardUpdateOk(HttpServletRequest req, HttpServletResponse res, FileBoardDto fileBoardDto) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", req);
		mav.addObject("fileBoardDto", fileBoardDto);
		
		fileBoardService.fileBoardUpdateOk(mav);		
		
		return mav;
	}
	
	public ModelAndView fileBoardDelete(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", req);

		fileBoardService.fileBoardDelete(mav);		
		
		return mav;
		
	}
	
	public ModelAndView fileBoardDeleteOk(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("request", req);

		fileBoardService.fileBoardDeleteOk(mav);		
		
		return mav;
	}
}

 

FileBoardService.java

package com.java.fileboard.service;

import org.springframework.web.servlet.ModelAndView;

public interface FileBoardService {
	public void fileBoardWrite(ModelAndView mav);
	
	public void fileBoardWriteOk(ModelAndView mav);
	
	public void fileBoardList(ModelAndView mav);
	
	public void fileBoardRead(ModelAndView mav);
	
	public void fileDownLoad(ModelAndView mav);

	public void fileBoardUpdate(ModelAndView mav);

	public void fileBoardUpdateOk(ModelAndView mav);

	public void fileBoardDelete(ModelAndView mav);

	public void fileBoardDeleteOk(ModelAndView mav);
}

 

FileBoardServiceImp.java

fileBoardWrite, fileBoardWriteOk, writeNumber, fileBoardList, fileBoardRead, fileDownLoad, fileBoardUpdate, fileBoardUpdateOk, fileBoardDelete, fileBoardDeleteOk ν•¨μˆ˜ μž‘μ„±

package com.java.fileboard.service;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.java.aop.HAspect;
import com.java.fileboard.dao.FileBoardDao;
import com.java.fileboard.dto.FileBoardDto;

public class FileBoardServiceImp implements FileBoardService{
	private FileBoardDao fileBoardDao;

	public FileBoardServiceImp() { }

	public FileBoardServiceImp(FileBoardDao fileBoardDao) {
		this.fileBoardDao = fileBoardDao;
	}

	public void setFileBoardDao(FileBoardDao fileBoardDao) {
		this.fileBoardDao = fileBoardDao;
	}
	
	@Override
	public void fileBoardWrite(ModelAndView mav) {
		//λΆ€λͺ¨κΈ€(ROOT)
		int boardNumber = 0;	//ROOT글이면 0
		int groupNumber = 1;	//그룹번호
		int sequenceNumber = 0;	//κΈ€μˆœμ„œ
		int sequenceLevel = 0;	//κΈ€λ ˆλ²¨
		
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		//λ‹΅κΈ€μΈκ²½μš° λΆ€λͺ¨κΈ€μ˜  DB κΈ€λ²ˆν˜Έ, 그룹번호, κΈ€μˆœμ„œ, κΈ€λ ˆλ²¨
		if(request.getParameter("boardNumber")!=null) {  
		    boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		    groupNumber = Integer.parseInt(request.getParameter("groupNumber"));
		    sequenceNumber = Integer.parseInt(request.getParameter("sequenceNumber"));
		    sequenceLevel = Integer.parseInt(request.getParameter("sequenceLevel"));
		}
		
		mav.addObject("boardNumber", boardNumber);
		mav.addObject("groupNumber", groupNumber);
		mav.addObject("sequenceNumber", sequenceNumber);
		mav.addObject("sequenceLevel", sequenceLevel);
		
		mav.setViewName("fileBoard/write");
	}
	
	@Override
	public void fileBoardWriteOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		FileBoardDto fileBoardDto = (FileBoardDto) map.get("fileBoardDto");
		MultipartHttpServletRequest request = (MultipartHttpServletRequest)map.get("request");
		
		
		writeNumber(fileBoardDto);
		fileBoardDto.setWriteDate(new Date());
		fileBoardDto.setReadCount(0);
		
		MultipartFile upFile = request.getFile("file");
		HAspect.logger.info(HAspect.logMsg + fileBoardDto);
		
		
		if(upFile!=null) {
			//μ €μž₯경둜, 파일λͺ…, μ‚¬μ΄μ¦ˆ
			String fileName = Long.toString(System.currentTimeMillis())+ "_" + upFile.getOriginalFilename();
			long fileSize = upFile.getSize();
			
			File path = new File("C:\\pds\\");
			path.mkdir();
			
			if(path.exists() && path.isDirectory()) {
				File file = new File(path, fileName);
				
				try {
					upFile.transferTo(file);
					
					fileBoardDto.setPath(file.getAbsolutePath());
					fileBoardDto.setFileName(fileName);
					fileBoardDto.setFileSize(fileSize);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
		HAspect.logger.info(HAspect.logMsg +fileBoardDto);
		
		int check = fileBoardDao.fileBoardWrite(fileBoardDto);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("check", check);
		mav.setViewName("fileBoard/writeOk");
	}
	
	public void writeNumber(FileBoardDto boardDto) {

		// 그룹번호(ROOT), κΈ€μˆœμ„œ(μžμ‹), κΈ€λ ˆλ²¨(μžμ‹)
		int boardNumber = boardDto.getBoardNumber(); // 0
		int groupNumber = boardDto.getGroupNumber(); // 1
		int sequenceNumber = boardDto.getSequenceNumber(); // 0
		int sequenceLevel = boardDto.getSequenceLevel(); // 0

		if (boardNumber == 0) { // ROOT : 그룹번호
			int max = fileBoardDao.fileBoardGroupNumberMax();

			if (max != 0)
				boardDto.setGroupNumber(max + 1);

		} else { // λ‹΅κΈ€ : κΈ€ μˆœμ„œ, κΈ€ 레벨
			HashMap<String, Integer> hMap = new HashMap<String, Integer>();
			hMap.put("groupNumber", groupNumber);
			hMap.put("sequenceNumber", sequenceNumber);

			int check = fileBoardDao.fileBoardWriteNumber(hMap);

			sequenceNumber = sequenceNumber + 1;
			sequenceLevel = sequenceLevel + 1;

			boardDto.setSequenceNumber(sequenceNumber);
			boardDto.setSequenceLevel(sequenceLevel);
		}
	}
	
	@Override
	public void fileBoardList(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");

		String pageNumber = request.getParameter("pageNumber");
		if (pageNumber == null)
			pageNumber = "1";

		int currentPage = Integer.parseInt(pageNumber); // μš”μ²­ 1page
		//HAspect.logger.info(HAspect.logMsg + "currentPage" + currentPage);

		int boardSize = 5; // view에 λͺ‡κ°œμ˜ 글을 보여쀄지
		int startRow = (currentPage - 1) * boardSize + 1; // μ‹œμž‘λ²ˆν˜Έ 1 11
		int endRow = currentPage * boardSize; // 끝번호 10 20
		//HAspect.logger.info(HAspect.logMsg + "startRow:" +startRow + "\t" + "endRow:" +endRow);
		
		int count = fileBoardDao.fileBoardCount();
		HAspect.logger.info(HAspect.logMsg + count);

		List<FileBoardDto> boardList = null;
		if (count > 0) {
			// startRow, endRow
			boardList = fileBoardDao.fileBoardList(startRow, endRow);
			HAspect.logger.info(HAspect.logMsg + boardList.size());
		}

		mav.addObject("boardList", boardList);
		mav.addObject("boardSize", boardSize);
		mav.addObject("currentPage", currentPage);
		mav.addObject("count", count);
		
		mav.setViewName("fileBoard/list");
	}
	
	@Override
	public void fileBoardRead(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		HAspect.logger.info(HAspect.logMsg + boardNumber + "\t" + pageNumber);
		
		FileBoardDto fileBoardDto = fileBoardDao.fileBoardRead(boardNumber);
		HAspect.logger.info(HAspect.logMsg + fileBoardDto);
		
		if(fileBoardDto.getFileName()!=null) {
			int index = fileBoardDto.getFileName().indexOf("-")+1;
			fileBoardDto.setFileName(fileBoardDto.getFileName().substring(index));
		}
		
		mav.addObject("boardDto", fileBoardDto);
		mav.addObject("pageNumber", pageNumber);
		
		mav.setViewName("fileBoard/read");
	}
	
	@Override
	public void fileDownLoad(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		HttpServletResponse response = (HttpServletResponse)map.get("response");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		HAspect.logger.info(HAspect.logMsg + boardNumber);
		
		FileBoardDto boardDto = fileBoardDao.fileBoardSelect(boardNumber);
		HAspect.logger.info(HAspect.logMsg + boardDto);
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		try {
			
			int index = boardDto.getFileName().indexOf("_") + 1;
			String fName = boardDto.getFileName().substring(index);
			String fileName = new String(fName.getBytes("utf-8"), "ISO-8859-1");
			
			long fileSize = boardDto.getFileSize();
			String path = boardDto.getPath();
			
			// μ„ΈνŒ…μ΄ ν•„μš”	
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			response.setContentType("application/octet-stream");
			response.setContentLengthLong(fileSize);
			
		    bis = new BufferedInputStream(new FileInputStream(path), 1024);
		    bos = new BufferedOutputStream(response.getOutputStream(), 1024);
		    
		    while(true) {
				int data = bis.read();
				if(data == -1) break;
				bos.write(data);
		    }
		    bos.flush();
		} catch (Exception e) {
		    e.printStackTrace();
		} finally {
			try {
				if(bis!=null) bis.close();
				if(bos!=null) bos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void fileBoardUpdate(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		HAspect.logger.info(HAspect.logMsg + boardNumber + "\t" + pageNumber);
		
		FileBoardDto fileBoardDto = fileBoardDao.fileBoardUpdate(boardNumber);
		HAspect.logger.info(HAspect.logMsg + fileBoardDto);
		
		if(fileBoardDto.getFileName()!=null) {
			int index = fileBoardDto.getFileName().indexOf("-")+1;
			fileBoardDto.setFileName(fileBoardDto.getFileName().substring(index));
		}
		
		mav.addObject("boardDto", fileBoardDto);
		mav.setViewName("fileBoard/update");
	}
	
	@Override
	public void fileBoardUpdateOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		MultipartHttpServletRequest request = (MultipartHttpServletRequest)map.get("request");
		
		FileBoardDto fileBoardDto = (FileBoardDto) map.get("fileBoardDto");
		
		writeNumber(fileBoardDto);
		fileBoardDto.setWriteDate(new Date());
		fileBoardDto.setReadCount(0);
		
		MultipartFile upFile = request.getFile("file");
		HAspect.logger.info(HAspect.logMsg + upFile.getOriginalFilename());
		
		
		if(upFile.getSize()!=0) {
			//μ €μž₯경둜, 파일λͺ…, μ‚¬μ΄μ¦ˆ
			String fileName = Long.toString(System.currentTimeMillis())+ "_" + upFile.getOriginalFilename();
			long fileSize = upFile.getSize();
			
			File path = new File("C:\\pds\\");
			path.mkdir();
			
			if(path.exists() && path.isDirectory()) {
				File file = new File(path, fileName);
				
				try {
					upFile.transferTo(file);
					
					fileBoardDto.setPath(file.getAbsolutePath());
					fileBoardDto.setFileName(fileName);
					fileBoardDto.setFileSize(fileSize);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
		HAspect.logger.info(HAspect.logMsg +fileBoardDto);
		
		int check = fileBoardDao.fileBoardUpdateOk(fileBoardDto);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("check", check);
		mav.setViewName("fileBoard/updateOk");
	}
	
	@Override
	public void fileBoardDelete(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber"));
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		HAspect.logger.info(HAspect.logMsg + boardNumber + pageNumber);
		
		mav.addObject("boardNumber", boardNumber);
		mav.addObject("pageNumber", pageNumber);
		mav.setViewName("fileBoard/delete");
	}
	
	@Override
	public void fileBoardDeleteOk(ModelAndView mav) {
		Map<String, Object> map = mav.getModelMap();
		HttpServletRequest request = (HttpServletRequest)map.get("request");
		
		int boardNumber = Integer.parseInt(request.getParameter("boardNumber")); 
		int pageNumber = Integer.parseInt(request.getParameter("pageNumber")); 
		String password = request.getParameter("password"); 
		HAspect.logger.info(HAspect.logMsg + boardNumber +"t"+ pageNumber +"t"+ password);
		
		int check = fileBoardDao.fileBoardDelete(boardNumber, password);
		HAspect.logger.info(HAspect.logMsg + check);
		
		mav.addObject("check", check);
		mav.addObject("pageNumber", pageNumber);
		mav.setViewName("fileBoard/deleteOk");
	}
}

 

[interface] FileBoardDao.java

fileBoardGroupNumberMax, fileBoardWriteNumber, fileBoardWrite, fileBoardCount, fileBoardList, fileBoardRead, fileBoardSelect, fileBoardUpdate, fileBoardUpdateOk, fileBoardDelete ν•¨μˆ˜ μΆ”κ°€

package com.java.fileboard.dao;

import java.util.HashMap;
import java.util.List;

import com.java.fileboard.dto.FileBoardDto;

public interface FileBoardDao {

	public int fileBoardGroupNumberMax();

	public int fileBoardWriteNumber(HashMap<String, Integer> hMap);

	public int fileBoardWrite(FileBoardDto fileBoardDto);
	
	public int fileBoardCount();

	public List<FileBoardDto> fileBoardList(int startRow, int endRow);
	
	public FileBoardDto fileBoardRead(int boardNumber);

	public FileBoardDto fileBoardSelect(int boardNumber);

	public FileBoardDto fileBoardUpdate(int boardNumber);

	public int fileBoardUpdateOk(FileBoardDto fileBoardDto);

	public int fileBoardDelete(int boardNumber, String password);

}

 

FileBoardMapper.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="dao.mapper.FileBoardMapper">

   <resultMap type="com.java.fileboard.dto.FileBoardDto" id="fileBoardDto">
      <!-- column: λ°μ΄ν„°λ² μ΄μŠ€ property : data -->
      <result column="board_number" property="boardNumber" />
      <result column="writer" property="writer" />
      <result column="subject" property="subject" />
      <result column="email" property="email" />
      <result column="content" property="content" />
      <result column="password" property="password" />
      
      <result column="write_date" property="writeDate" />
      <result column="read_count" property="readCount" />
      <result column="group_number" property="groupNumber" />
      <result column="sequence_number" property="sequenceNumber" />
      <result column="sequence_level" property="sequenceLevel" />
      
      <result column="file_name" property="fileName" />
      <result column="path" property="path" />
      <result column="file_size" property="fileSize" />
   </resultMap>

 	<select id="board_group_number_max" resultType="int">
		select nvl(max(group_number), 0) from board
	</select>
	
	 <update id="board_writer_number" parameterType="java.util.HashMap">
         update board set sequence_number=sequence_number+1
         where group_number=#{groupNumber} and sequence_number>#{sequenceNumber}
   	</update>

	<insert id="board_insert" parameterType="com.java.fileboard.dto.FileBoardDto">
		insert into board(
			board_number,
			writer,
			subject,
			email,
			content,
			password,
			write_date,
			read_count,
			group_number,
			sequence_number,
			sequence_level,
			file_name,
			path,
			file_size)
		values (board_number_seq.nextval,
			#{writer},
			#{subject},
			#{email},
			#{content},
			#{password},
			#{writeDate},
			#{readCount},
			#{groupNumber},
			#{sequenceNumber},
			#{sequenceLevel},
			#{fileName},
			#{path},
			#{fileSize})
	</insert>

    <select id="board_count" resultType="int">
      select count(*) from board
   </select>

  	<select id="board_list" parameterType="java.util.Map" resultMap="fileBoardDto">
         <![CDATA[
         select * from 
         (select rownum as rnum, a.* from
         (select * from board order by group_number desc, sequence_number asc) a) b
         where b.rnum>=#{startRow} and b.rnum<=#{endRow}
         ]]>
   </select>
   
   <update id="board_read_count" parameterType="int">
      <![CDATA[
         update board set read_count=read_count+1 where board_number =#{boardNumber}
      ]]>
   </update>
   
  <select id="board_read" parameterType="int" resultMap="fileBoardDto">
      select * from board where board_number=#{boardNumber}
   </select>
   
   <delete id="board_delete" parameterType="java.util.HashMap" >
      delete from board where board_number=#{boardNumber} and password=#{password}
   </delete>
   
   <update id="board_update" parameterType="java.util.HashMap">
      update board set subject=#{boardDto.subject}, email=#{boardDto.email}, content=#{boardDto.content}, password=#{boardDto.password}, write_date=#{boardDto.writeDate} where board_number=#{boardNumber}
   </update>
</mapper>

 

FileBoardDaoImp.java

package com.java.fileboard.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;

import com.java.fileboard.dto.FileBoardDto;

public class FileBoardDaoImp implements FileBoardDao{
	private SqlSessionTemplate sqlSessionTemplate;

	public FileBoardDaoImp() { }

	public FileBoardDaoImp(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
	
	@Override
	public int fileBoardGroupNumberMax() {
		return sqlSessionTemplate.selectOne("board_group_number_max");
	}
	
	@Override
	public int fileBoardWriteNumber(HashMap<String, Integer> hMap) {
		return sqlSessionTemplate.update("board_writer_number", hMap);
	}
	
	@Override
	public int fileBoardWrite(FileBoardDto fileBoardDto) {
		return sqlSessionTemplate.insert("board_insert", fileBoardDto);
	}
	
	@Override
	public int fileBoardCount() {
		return sqlSessionTemplate.selectOne("board_count");
	}
	
	@Override
	public List<FileBoardDto> fileBoardList(int startRow, int endRow) {
		Map<String, Integer> hMap = new HashMap<String, Integer>();
		hMap.put("startRow", startRow);
		hMap.put("endRow", endRow);
		
		return sqlSessionTemplate.selectList("board_list", hMap);
	}
	
	@Override
	public FileBoardDto fileBoardRead(int boardNumber) {
		sqlSessionTemplate.update("board_read_count", boardNumber);
	
		return sqlSessionTemplate.selectOne("board_read", boardNumber);
	}
	
	@Override
	public FileBoardDto fileBoardSelect(int boardNumber) {
		return sqlSessionTemplate.selectOne("board_read", boardNumber);
	}
	
	@Override
	public FileBoardDto fileBoardUpdate(int boardNumber) {
		return sqlSessionTemplate.selectOne("board_read", boardNumber);
	}
	
	@Override
	public int fileBoardUpdateOk(FileBoardDto fileBoardDto) {
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("boardNumber", fileBoardDto.getBoardNumber());
		hMap.put("boardDto", fileBoardDto);
		
		return sqlSessionTemplate.update("board_update", hMap);
	}
	
	@Override
	public int fileBoardDelete(int boardNumber, String password) {
		Map<String, Object> hMap = new HashMap<String, Object>();
		hMap.put("boardNumber", boardNumber);
		hMap.put("password", password);
		
		return sqlSessionTemplate.update("board_delete", hMap);
	}
}

 

myBatis.xml

체크해주기

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

	<!-- Spring DB -->
	<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
		<property name="username" value="mvc"/>
		<property name="password" value="1234"/>
	</bean>
	
	<!-- MyBatis : sqlSessionFactoryBean -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="driverManagerDataSource"/>
		<property name="mapperLocations">
			<list>
				<value>classpath:com/java/member/dao/mapper/MemberMapper.xml</value>
				<value>classpath:com/java/fileboard/dao/mapper/FileBoardMapper.xml</value>
			</list>
		</property>
	</bean>
	
	<!-- MyBatis : sqlSessionTemplate -->
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactoryBean"/>
	</bean>
	
	<!-- Transaction κ³΅ν†΅ν΄λž˜μŠ€ -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="driverManagerDataSource"/>
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
	<!-- propagation="REQUIRED" : 이미 ν•˜λ‚˜μ˜ νŠΈλžœμž­μ…˜μ΄ μ‘΄μž¬ν•˜λ©΄ μ§€μ›ν•˜κ³  μ—†λ‹€λ©΄ μƒˆλ‘œμ§€μ› -->
	
	<!-- Transaction AOP -->
	<aop:config>
		<aop:pointcut expression="within(com.java..*)" id="txPublic"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPublic"/>
	</aop:config>
</beans>
λ°˜μ‘ν˜•
Comments