일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- Render
- Maven
- Ajax
- 스프링게시판
- State
- SpringFramework
- spring framework
- rest
- spring
- react
- spring게시판
- 자바
- Java
- reactjs
- mysql
- 재정의
- Gradle
- springboot
- 스프링
- 다형성
- 스프링프레임워크
- Restful
- Stateless
- 스프링부트
- 지족동
- 리액트
- mybatis
- 둔산동
- 스프링 게시판
- 컴포넌트
- Today
- Total
Dev.GA
[spring/게시판] #3 게시판 등록, 조회, 수정, 삭제하기 본문
개발환경
Server OS : Windows10
Language : JAVA 1.6
Framework : Spring 3.1.1
WEB Server : Apache
WAS Server : Tomcat 7
build tool : maven 2.5.1
DB : MySQL 5.7.16
ORM : mybatis 3.2.7
3. 게시판 등록, 조회, 수정, 삭제하기
이번 포스팅은 본격적인 spring 게시판 프로젝트의 게시물 등록, 조회, 수정, 삭제 기능 추가를 해보기로 한다.
기능 구현에 앞서, 프로젝트의 base-package를 수정해준다.
servlet-context.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="com.ga" /> </beans:beans> | cs |
24번째 라인을 변경해주면 되는데,
기존의 com.ga.board에서 지난 포스팅에서 재구성한 프로젝트 구성에 맞게 상위 디렉토리인 com.ga로 변경한다.
자, 이제 본격적인 게시판 기능을 하나씩 구현해보도록 한다.
지난 포스팅에서 게시판(board) 테이블을 생성하였고, 이번에는 테이블에 맞는 VO객체를 생성한다.
BoardVO.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.ga.board.service; public class BoardVO { private int code; private String title = ""; private String content = ""; private String writer = ""; private String reg_datetime = ""; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getWriter() { return writer; } public void setWriter(String writer) { this.writer = writer; } public String getReg_datetime() { return reg_datetime; } public void setReg_datetime(String reg_datetime) { this.reg_datetime = reg_datetime; } } | cs |
객체를 생성했으면 다음으로, mybatis에서 질의할 쿼리문이 작성될 맵퍼 인터페이스와 쿼리문을 작성한다.
BoardMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.ga.board.service.mapper; import java.util.List; import com.ga.board.service.BoardVO; public interface BoardMapper { // 게시물 리스트 조회 public List<BoardVO> selectBoardList(BoardVO boardVO) throws Exception; // 게시물 등록 public void insertBoard(BoardVO boardVO) throws Exception; // 게시물 수정 public void updateBoard(BoardVO boardVO) throws Exception; // 게시물 삭제 public void deleteBoard(BoardVO boardVO) throws Exception; // 게시물 조회 public BoardVO selectBoardByCode(BoardVO boardVO) throws Exception; } | cs |
board_SQL.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?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.ga.board.service.mapper.BoardMapper"> <select id="selectBoardList" parameterType="com.ga.board.service.BoardVO" resultType="com.ga.board.service.BoardVO"> select * from board </select> <insert id="insertBoard"> insert into board( title , content , writer , reg_datetime) values( #{title} , #{content} , #{writer} , now()) </insert> <update id="updateBoard"> update board set title = #{title} , content = #{content} , writer = #{writer} , reg_datetime = now() where code = #{code} </update> <delete id="deleteBoard"> delete from board where code = #{code} </delete> <select id="selectBoardByCode" parameterType="com.ga.board.service.BoardVO" resultType="com.ga.board.service.BoardVO"> select * from board where code = #{code} </select> </mapper> | cs |
이제 맵퍼를 호출할 DAO 인터페이스와 서비스를 작성한다.
BoardDAO.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package com.ga.board.service.impl; import java.util.List; import com.ga.board.service.BoardVO; public interface BoardDAO { /** * 게시물 리스트 조회 * @param boardVO * @return * @throws Exception */ List<BoardVO> selectBoardList(BoardVO boardVO) throws Exception; /** * 게시물 등록 * @param boardVO * @throws Exception */ void insertBoard(BoardVO boardVO) throws Exception; /** * 게시물 수정 * @param boardVO * @throws Exception */ void updateBoard(BoardVO boardVO) throws Exception; /** * 게시물 삭제 * @param boardVO * @throws Exception */ void deleteBoard(BoardVO boardVO) throws Exception; /** * 게시물 조회 * @param boardVO * @return * @throws Exception */ BoardVO selectBoardByCode(BoardVO boardVO) throws Exception; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.ga.board.service.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ga.board.service.BoardVO; import com.ga.board.service.mapper.BoardMapper; @Service("boardDAOService") public class BoardDAOService implements BoardDAO{ @Autowired private SqlSession sqlSession; public List<BoardVO> selectBoardList(BoardVO boardVO) throws Exception { BoardMapper mapper = sqlSession.getMapper(BoardMapper.class); return mapper.selectBoardList(boardVO); } public void insertBoard(BoardVO boardVO) throws Exception { BoardMapper mapper = sqlSession.getMapper(BoardMapper.class); mapper.insertBoard(boardVO); } public void updateBoard(BoardVO boardVO) throws Exception { BoardMapper mapper = sqlSession.getMapper(BoardMapper.class); mapper.updateBoard(boardVO); } public void deleteBoard(BoardVO boardVO) throws Exception { BoardMapper mapper = sqlSession.getMapper(BoardMapper.class); mapper.deleteBoard(boardVO); } @Override public BoardVO selectBoardByCode(BoardVO boardVO) throws Exception { BoardMapper mapper = sqlSession.getMapper(BoardMapper.class); return mapper.selectBoardByCode(boardVO); } } | cs |
다음으로 게시판의 기능(등록, 조회, 수정, 삭제)을 구현할 Service 인터페이스를 작성한다.
BoardService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.ga.board.service.impl; import java.util.List; import com.ga.board.service.BoardVO; public interface BoardService { /** * 게시물 리스트 조회 * @param boardVO * @return * @throws Exception */ public List<BoardVO> selectBoardList(BoardVO boardVO) throws Exception; /** * 게시물 작성 * @param boardVO * @throws Exception */ public void insertBoard(BoardVO boardVO) throws Exception; /** * 게시물 수정 * @param boardVO * @throws Exception */ public void updateBoard(BoardVO boardVO) throws Exception; /** * 게시물 삭제 * @param boardVO * @throws Exception */ public void deleteBoard(BoardVO boardVO) throws Exception; /** * 게시글 조회 * @param boardVO * @return * @throws Exception */ public BoardVO selectBoardByCode(BoardVO boardVO) throws Exception; } | cs |
다음은 비즈니스 로직이 들어갈 Service 클래스를 구현한다.
BoardServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | package com.ga.board.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ga.board.service.BoardVO; @Service("boardServiceImpl") public class BoardServiceImpl implements BoardService{ @Autowired private BoardDAO boardDAOService; @Override public List<BoardVO> selectBoardList(BoardVO boardVO) throws Exception { List<BoardVO> list = null; list = boardDAOService.selectBoardList(boardVO); return list; } @Override public void insertBoard(BoardVO boardVO) throws Exception { boardDAOService.insertBoard(boardVO); } @Override public void updateBoard(BoardVO boardVO) throws Exception { boardDAOService.updateBoard(boardVO); } @Override public void deleteBoard(BoardVO boardVO) throws Exception { boardDAOService.deleteBoard(boardVO); } @Override public BoardVO selectBoardByCode(BoardVO boardVO) throws Exception { BoardVO resultVO = boardDAOService.selectBoardByCode(boardVO); return resultVO; } } | cs |
객체를 생성하고, 맵퍼, DAO, Service까지 생성하였다.
이제 클라이언트의 요청에 따라 게시판의 기능(등록, 조회, 수정, 삭제)을 제어할 Controller를 작성한다.
BoardController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | package com.ga.board.web; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.ga.board.service.BoardVO; import com.ga.board.service.impl.BoardService; @Controller public class BoardController { @Autowired private BoardService boardServiceImpl; /** * 게시판 조회 * @param boardVO * @param model * @return * @throws Exception */ @RequestMapping(value="/board/boardList.do") public String boardList(@ModelAttribute("boardVO") BoardVO boardVO, Model model) throws Exception{ List<BoardVO> list = boardServiceImpl.selectBoardList(boardVO); model.addAttribute("list", list); return "board/boardList"; } /** * 글쓰기 폼 * @return * @throws Exception */ @RequestMapping(value="/board/writeForm.do") public String writeBoardForm() throws Exception{ return "board/writeForm"; } /** * 게시글 등록 * @param boardVO * @param model * @return * @throws Exception */ @RequestMapping(value="/board/write.do") public String write(@ModelAttribute("boardVO") BoardVO boardVO, Model model) throws Exception{ boardServiceImpl.insertBoard(boardVO); return "redirect:/board/boardList.do"; } /** * 게시글 조회 * @param boardVO * @param model * @param request * @return * @throws Exception */ @RequestMapping(value="/board/viewContent.do") public String viewForm(@ModelAttribute("boardVO") BoardVO boardVO, Model model, HttpServletRequest request) throws Exception{ int code = Integer.parseInt(request.getParameter("code")); boardVO.setCode(code); BoardVO resultVO = boardServiceImpl.selectBoardByCode(boardVO); model.addAttribute("result", resultVO); return "board/viewForm"; } /** * 게시글 수정 * @param boardVO * @param model * @return * @throws Exception */ @RequestMapping(value="/board/updateboard.do") public String updateBoard(@ModelAttribute("boardVO") BoardVO boardVO, Model model) throws Exception{ try{ boardServiceImpl.updateBoard(boardVO); } catch (Exception e){ e.printStackTrace(); } return "redirect:/board/boardList.do"; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <div> <form id="boardForm" name="boardForm" method="post"> <table> <thead> <tr> <th>번호</th> <th>제목</th> <th>작성자</th> <th>날짜</th> <th>조회수</th> </tr> </thead> <tbody> <c:forEach var="result" items="${list }" varStatus="status"> <tr> <td><c:out value="${result.code }"/></td> <td><a href='#' onClick='fn_view(${result.code})'><c:out value="${result.title }"/></a></td> <td><c:out value="${result.writer }"/></td> <td><c:out value="${result.reg_datetime }"/></td> </tr> </c:forEach> </tbody> </table> <div> <a href='#' onClick='fn_write()'>글쓰기</a> </div> </form> <script> //글쓰기 function fn_write(){ var form = document.getElementById("boardForm"); form.action = "<c:url value='/board/writeForm.do'/>"; form.submit(); } //글조회 function fn_view(code){ var form = document.getElementById("boardForm"); var url = "<c:url value='/board/viewContent.do'/>"; url = url + "?code=" + code; form.action = url; form.submit(); } </script> </div> </body> </html> | cs |
writeForm.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <div> <form id="writeForm" name="writeForm" method="post"> <div> <h2>글쓰기</h2> <div> <table> <tr> <th>제목</th> <td><input style="width: 500px" type="text" id="title" name="title" /></td> </tr> <tr> <th>내용</th> <td><textarea style="width: 500px" rows="10" cols="10" id="content" name="content"></textarea></td> </tr> <tr> <th>작성자</th> <td><input style="width: 500px" type="text" id="writer" name="writer" /></td> </tr> </table> <div> <a href='#' onClick='fn_addtoBoard()'>글 등록</a> <a href='#' onClick='fn_cancel()'>목록</a> </div> </div> </div> </form> <script> //글쓰기 function fn_addtoBoard(){ var form = document.getElementById("writeForm"); form.action = "<c:url value='/board/write.do'/>"; form.submit(); } //목록 function fn_cancel(){ var form = document.getElementById("writeForm"); form.action = "<c:url value='/board/boardList.do'/>"; form.submit(); } </script> </div> </body> </html> | cs |
viewForm.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <div> <form id="viewForm" name="viewForm" method="post"> <div> <h2>글쓰기</h2> <div> <table> <tr> <th>제목</th> <td><input style="width: 500px" type="text" id="title" name="title" value="${result.title }"/></td> </tr> <tr> <th>내용</th> <td><textarea style="width: 500px" rows="10" cols="10" id="content" name="content"><c:out value="${result.content }"/></textarea></td> </tr> <tr> <th>작성자</th> <td><input style="width: 500px" type="text" id="writer" name="writer" value="${result.writer }"/></td> </tr> </table> <div> <a href='#' onClick='fn_update()'>수정</a> <a href='#' onClick='fn_cancel()'>목록</a> <a href='#' onClick='fn_relay()'>답변</a> </div> </div> </div> <input type='hidden' id='code' name='code' value='${result.code }' /> </form> <script> //목록 function fn_cancel(){ var form = document.getElementById("viewForm"); form.action = "<c:url value='/board/boardList.do'/>"; form.submit(); } //수정 function fn_update(){ var form = document.getElementById("viewForm"); form.action = "<c:url value='/board/updateboard.do'/>"; form.submit(); } //답변 function fn_relay(){ var form = document.getElementById("viewForm"); form.action = "<c:url value='/board/relayForm.do'/>"; form.submit(); } </script> </div> </body> </html> | cs |
자, 이제 그럼 모든 구현이 완료되었음으로 직접 기능에 대해서 테스트를 한다.
위와 같이 글을 등록하고 수정까지 잘 진행되었다.
이번 포스팅에서는 게시판의 등록, 조회, 수정, 삭제 기능을 정말 원초적인 기능으로만 구현하였다.
다음 포스팅에서는 게시판 좀 꾸미고, 코드 정리를 해야할 것 같다.
무궁무진한 게시판의 기능을 완성하기 위해선 로그인 및 회원관리부터 세션처리, 조회수 카운팅, 게시판 답변 달기(계층형), 댓글...등등 가야할 길이 멀다. 앞으로 포스팅에 속도를 좀 더 낼 수 있도록................... 그럼
'Dev.Project > project1-spring게시판' 카테고리의 다른 글
[spring/게시판] #6 게시판 페이징 처리하기 (19) | 2018.02.23 |
---|---|
[spring/게시판] #5 Interceptor를 이용한 로그인 처리 (3) | 2018.02.02 |
[spring/게시판] #4 게시판에 부트스트랩(bootstrap) 적용하기 (0) | 2017.11.27 |
[spring/게시판] #2 mybatis 연동하기 (2) | 2017.11.14 |
[spring/게시판] #1 Spring 프로젝트 만들기 (1) | 2017.11.08 |