spring실습1-3(게시판)

거너거너·2021년 11월 1일
0

spring(학원)

목록 보기
6/16
  1. root-context.xml
  • myzBatis2에 있는걸 복붙 및 수정
	<!-- Data Source -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
		<property name="username" value="spring" />
		<property name="password" value="spring123" />
	</bean>
	
	<!-- 스프링 jdbc 즉 스프링으로 oracle 디비 연결 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:util/configuration.xml" />
		<property name="mapperLocations" value="classpath:sql/*.xml" />
	</bean>
	
	<bean id="session" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
  1. BoardDao.java
package myspring.dao;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class BoardDao {
	
	@Autowired
	private SqlSession session;

}
  1. views-board폴더 생성하여 boardform.jsp 저장

  2. index.jsp

<script>
//	location.href="home.do";
	location.href="boardform.do";
</script>
  1. board컨트롤러
	// 글작성 폼
	@RequestMapping("boardform.do")
	public String boardform() {
		return "board/boardform";
    }
	// 글작성
	@RequestMapping("boardwrite.do")
	public String boardwrite(Board board, Model model) {
		System.out.println("writer:"+ board.getWriter());
		
		int result = service.insert(board);
		System.out.println("result:"+result);
		
		model.addAttribute("result", result);
		
		return "board/insertresult";
	}
  1. BoardService
	public int insert(Board board) {
		return dao.insert(board);
	}
  1. BoardDao
	public int insert(Board board) {
		return session.insert("", board);
	}
  1. board.xml
	<!-- 글작성 -->
	<insert id="insert" parameterType="board">
		insert into myboard values(myboard_seq.nextval,#{writer},
		#{passwd},#{subject},#{content},0,sysdate)
	</insert>
  1. views-board -> insertresult.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:if test="${result == 1 }">
	<script>
		alert("글작성 성공");
		location.href="boardlist.do";
	</script>
</c:if>

<c:if test="${result != 1 }">
	<script>
		alert("글작성 실패");
		history.go(-1);
	</script>
</c:if>

</body>
</html>
  1. 컨트롤러
	// 글목록
	@RequestMapping("boardlist.do")
	public String boardlist(HttpServletRequest request, Model model) {
		int page = 1;		// 현재 페이지 번호
		int limit = 10;		// 한 페이지에 출력할 데이터 갯수
		
		if(request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		
// 		int startRow=(page-1)*limit+1;
//		int endRow=page*limit;
		
		// 총 데이터 갯수
		int listcount = service.getCount();
		System.out.println("listcount:"+listcount);
		
		return "board/boardlist";
	}
  1. 서비스
	public int getCount() {
		return dao.getCount();
	}
  1. DAO
	public int getCount() {
		return session.selectOne("count");
	}
  1. board.xml
	<!-- 글갯수 -->
	<select id="count" resultType="int">
		select count(*) from myboard
	</select>
  1. 컨트롤러
		// 총 데이터 갯수
		int listcount = service.getCount();
		System.out.println("listcount:"+listcount);
		
		List<Board> boardlist = service.getBoardList(page);
		System.out.println("boardlist:"+boardlist);
  1. 서비스
	public List<Board> getBoardList(int page) {
		return dao.getBoardList(page);
	}
  1. DAO
	public List<Board> getBoardList(int page) {
		return session.selectOne("list", page);
	}
  1. board.xml
	<!-- 글목록 -->	
	<!-- 방법 1 : <![CDATA[     ]]> -->
	<!-- 방법2 :  > : &gt; , < : &lt; -->
	<select id="list" parameterType="int" resultType="board">
		<![CDATA[
		select * from ( select rownum rnum, board.* from ( 
		select * from myboard order by no desc) board )
		where rnum >= ((#{page}-1) * 10+1) and rnum <= (#{page}*10)
		]]>
	</select>
  1. 컨트롤러
// 총 페이지
		int pageCount = listcount/limit+((listcount%limit==0)? 0 : 1);
		
		int startPage = ((page-1)/10 * limit + 1);	// 1, 11, 21...
		int endPage = startPage + 10 -1;			// 10, 20, 30...
		
		if(endPage > pageCount)
			endPage = pageCount;
		
		model.addAttribute("page", page);
		model.addAttribute("listcount", listcount);
		model.addAttribute("boardlist", boardlist);
		model.addAttribute("pageCount", pageCount);
		model.addAttribute("startPage", startPage);
		model.addAttribute("endPage", endPage);
return "board/boardlist";
  1. views-board -> boardlist.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>
<html>
<head>
<meta charset="UTF-8">
<title>게시판 목록</title>
</head>
<body>
	<a href="boardform.do">글작성</a>
	글갯수 : ${listcount }
	<table border=1 align="center" width=800>
		<caption>게시판 목록</caption>
		<tr>
			<th>번호</th>
			<th>제목</th>
			<th>작성자</th>
			<th>날짜</th>
			<th>조회수</th>
		</tr>
		
		<!-- 화면 출력 번호 -->
		<c:set var="num" value="${listcount-(page-1)*10 }"/>
		
		<c:forEach var="b" items="${boardlist}">
		<tr>
			<td>${num}
				<c:set var="num" value="${num-1}"/>
			</td>
			<td>
			<a href="boardcontent.do?no=${b.no}&page=${page}">
				${b.subject}
			</a>
			</td>
			<td>${b.writer}</td>
			<td>
				<fmt:formatDate value="${b.register}" 
						pattern="yyyy-MM-dd HH:mm:ss"/>
			</td>
			<td>${b.readcount}</td>
		</tr>
		</c:forEach>	
	</table>

<!-- 페이지 처리 -->
<center>
<c:if test="${listcount > 0}">

	<!-- 1page로 이동 -->
	<a href="boardlist.do?page=1" style="text-decoration:none"> << </a>
	
	<!-- 이전 블럭으로 이동 -->
	<c:if test="${startPage > 10 }">
		<a href="boardlist.do?page=${startPage-10}">[이전]</a>
	</c:if>
	
	<!-- 각 블럭에 10개의 페이지 출력 -->
	<c:forEach var="i" begin="${startPage }" end="${endPage }">
		<c:if test="${i == page }">		<!-- 현재 페이지 -->
			[${i}]
		</c:if>
		<c:if test="${i != page }">		<!-- 현재 페이지가 아닌 경우 -->
			<a href="boardlist.do?page=${i}">[${i}]</a>
		</c:if>
	</c:forEach>
	
	<!-- 다음 블럭으로 이동 -->
	<c:if test="${endPage < pageCount }">
		<a href="boardlist.do?page=${startPage+10}">[다음]</a>
	</c:if>
	
	<!-- 마지막 페이지로 이동 -->
	<a href="boardlist.do?page=${pageCount }" 
		style="text-decoration:none"> >> </a>
	
</c:if>
</center>
</body>
</html>
  1. 컨트롤러(상세페이지)
	// 상세 페이지 : 조회수 1증가 + 상세정보 구하기
	@RequestMapping("boardcontent.do")
	public String boardcontent(int no, String page, Model model) {
		
		service.updatecount(no);	// 조회수 1증가
		
		return "board/boardcontent";
	}
  1. 서비스
	public void updatecount(int no) {	
		dao.updatecount(no);
	}
  1. DAO
	public void updatecount(int no) {
		session.update("hit", no);
	}
  1. board.xml
	<!-- 조회수 증가 -->
	<update id="hit" parameterType="int">
		update myboard set readcount=readcount+1 where no=#{no}
	</update>
  1. 컨트롤러
	// 상세 페이지 : 조회수 1증가 + 상세정보 구하기
	@RequestMapping("boardcontent.do")
	public String boardcontent(int no, String page, Model model) {
		
		service.updatecount(no);				// 조회수 1증가
		Board board = service.getBoard(no);	// 상세 정보 구하기
		
		return "board/boardcontent";
	}
  1. 서비스
	public Board getBoard(int no) {
		return dao.getBoard(no);
	}
  1. DAO
	public Board getBoard(int no) {
		return session.selectOne("content", no);
	}
  1. boardcontent.jsp
코드를 입력하세요
profile
배움이 필요한 사람

0개의 댓글