[STS Spring] 게시판 만들기 -- 05. 글 등록(INSERT), 글 수정(UPDATE), 글 삭제(DELETE), 글 상세 조회(SELECT)
my code archive
article thumbnail
반응형
1
2
3
4
5
6
7
8
9
private String search_type;
    
     public String getSearch_type() {
            return search_type;
        }
     
        public void setSearch_type(String search_type) {
            this.search_type = search_type;
        }
cs
  • 2. BoardDTO.java 에 아래 내용 추가
1
2
3
4
5
6
7
8
9
    private String result;
    
    public String getResult() {
        return result;
    }
 
    public void setResult(String result) {
        this.result = result;
    }
cs

 

  • 3. BoardDAO.java / BoardService.java / 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
package net.spring.study.board.dao;
 
import java.util.List;
 
import javax.annotation.Resource;
 
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
 
import net.spring.study.board.dto.BoardDTO;
import net.spring.study.board.form.BoardForm;
 
@Repository("boardDAO")
public class BoardDAO {
 
    @Resource(name = "sqlSession")
    private SqlSession sqlSession;
    
    private static final String NAMESPACE = "net.spring.study.boardMapper";
    
    /* 게시판 - 목록 조회 */
    public List<BoardDTO> getBoardList(BoardForm boardForm) {
        
        return sqlSession.selectList(NAMESPACE + ".getBoardList");
    }
    
    /* 게시판 - 조회수 수정 */
    public int updateBoardHits(BoardForm boardForm) {
        
        return sqlSession.update(NAMESPACE+".updateBoardHits", boardForm);
    }
    
    /* 게시판 - 상세 조회 */
    public BoardDTO getBoardDetail(BoardForm boardForm) throws Exception{
        
        return sqlSession.selectOne(NAMESPACE+".getBoardDetail", boardForm);
    }
    
    /* 게시판 - 글 등록 */
    public int insertBoard(BoardForm boardForm) throws Exception{
        
        return sqlSession.insert(NAMESPACE+".insertBoard", boardForm);
    }
    
    /* 게시판 - 글 삭제 */
    public int deleteBoard(BoardForm boardForm) throws Exception{
        
        return sqlSession.delete(NAMESPACE+".deleteBoard", boardForm);
    }
    
    /* 게시판 - 글 수정 */
    public int updateBoard(BoardForm boardForm) throws Exception{
        
        return sqlSession.update(NAMESPACE+".updateBoard", boardForm);
    }
 
}
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
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
package net.spring.study.board.service;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import net.spring.study.board.dao.BoardDAO;
import net.spring.study.board.dto.BoardDTO;
import net.spring.study.board.form.BoardForm;
 
@Service("boardService")
public class BoardService {
 
    @Autowired
    private BoardDAO boardDAO;
    
    /* 게시판 - 목록 조회 */
    public List<BoardDTO> getBoardList(BoardForm boardForm) throws Exception{
        
        return boardDAO.getBoardList(boardForm);
    }
    
    /* 게시판 - 상세 조회 */
    public BoardDTO getBoardDetail(BoardForm boardForm) throws Exception {
        
        BoardDTO boardDTO = new BoardDTO();
        String searchType = boardForm.getSearch_type();
        
        if("S".equals(searchType)) {
            int updateCnt = boardDAO.updateBoardHits(boardForm);
            
            if(updateCnt > 0) {
                boardDTO = boardDAO.getBoardDetail(boardForm);
            }
        }else {
            boardDTO = boardDAO.getBoardDetail(boardForm);
        }
        
        return boardDTO;
    }
    
    /* 게시판 - 글 등록 */
    public BoardDTO insertBoard(BoardForm boardForm) throws Exception{
        
        BoardDTO boardDTO = new BoardDTO();
        
        int insertCnt = boardDAO.insertBoard(boardForm);
        
        if(insertCnt > 0) {
            boardDTO.setResult("INSERT_SUCCESS");
        }else {
            boardDTO.setResult("INSERT_FAIL");
        }
        
        return boardDTO;
    }
    
    /* 게시판 - 글 삭제 */
    public BoardDTO deleteBoard(BoardForm boardForm) throws Exception{
        
        BoardDTO boardDTO = new BoardDTO();
        
        int deleteCnt = boardDAO.deleteBoard(boardForm);
        
        if(deleteCnt > 0) {
            boardDTO.setResult("DELETE_SUCCESS");
        }else {
            boardDTO.setResult("DELETE_FAIL");
        }
        
        return boardDTO;
    }
    
    /* 게시판 - 글 수정 */
    public BoardDTO updateBoard(BoardForm boardForm) throws Exception{
        
        BoardDTO boardDTO = new BoardDTO();
        
        int updateCnt = boardDAO.updateBoard(boardForm);
        
        if(updateCnt > 0) {
            boardDTO.setResult("UPDATE_SUCCESS");
        }else {
            boardDTO.setResult("UPDATE_FAIL");
        }
        return boardDTO;
    }
 
}
 
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
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
package net.spring.study.board.controller;
 
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import net.spring.study.board.dto.BoardDTO;
import net.spring.study.board.form.BoardForm;
import net.spring.study.board.service.BoardService;
 
@Controller
@RequestMapping(value = "/board")
public class BoardController {
    
    @Autowired
    private BoardService boardService;
    
    /*게시판 - 목록 페이지 이동*/
    @RequestMapping(value = "/boardList")
    public String getBoardList2(HttpServletRequest request, HttpServletResponse response) throws Exception{
        
        return "board/boardList";
    }
    
    /*게시판 - 목록 조회*/
    @RequestMapping(value = "/getBoardList")
    @ResponseBody
    public List<BoardDTO> getBoardList(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception{
        List<BoardDTO> boardList = boardService.getBoardList(boardForm);
        
        return boardList;
    }
    
    /* 게시판 - 상세 페이지 이동 */
    @RequestMapping(value = "/boardDetail")
    public String boardDetail(HttpServletRequest request, HttpServletResponse response) throws Exception{
        
        return "board/boardDetail";
    }
    
    
    /* 게시판 - 상세 조회 */
    @RequestMapping(value = "/getBoardDetail")
    @ResponseBody
    public BoardDTO getBoardDetail(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception{
        
        BoardDTO boardDTO = boardService.getBoardDetail(boardForm);
        
        return boardDTO;
    }
    
    /* 게시판 - 글 작성 페이지 이동 */
    @RequestMapping(value = "/boardWrite")
    public String boardWrite(HttpServletRequest request, HttpServletResponse response) throws Exception{
        
        return "board/boardWrite";
    }
    
    /* 게시판 - 글 등록 */
    @RequestMapping(value = "/insertBoard")
    @ResponseBody
    public BoardDTO insertBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception{
        
        BoardDTO boardDTO = boardService.insertBoard(boardForm);
        
        return boardDTO;
    }
    
    /* 게시판 - 글 삭제 */
    @RequestMapping(value = "/deleteBoard")
    @ResponseBody
    public BoardDTO deleteBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception{
        
        BoardDTO boardDTO = boardService.deleteBoard(boardForm);
        
        return boardDTO;
    }
    
    /* 게시판 - 글 수정 페이지 이동 */
    @RequestMapping(value = "/boardUpdate")
    public String boardUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception{
        
        return "board/boardUpdate";
    }
    
    /* 게시판 - 글 수정 */
    @RequestMapping(value = "/updateBoard")
    @ResponseBody
    public BoardDTO updateBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception{
        
        BoardDTO boardDTO = boardService.updateBoard(boardForm);
        
        return boardDTO;
    }
 
}
 
cs

 

  • 4.board.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
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
<!-- 조회수 수정 -->
    <update id="updateBoardHits" parameterType="net.spring.study.board.form.BoardForm">
        UPDATE SP_BOARD
            SET BOARD_HITS = BOARD_HITS + 1
            , UPD_USER_ID = 'NONMEMBER'
            , UPD_DATE = NOW()
        WHERE BOARD_SEQ = #{board_seq}    
    </update>
    
    <!-- 상세조회 -->
    <select id="getBoardDetail" parameterType="net.spring.study.board.form.BoardForm" resultType="net.spring.study.board.dto.BoardDTO">
        SELECT BOARD_SEQ 
             , BOARD_RE_REF 
             , BOARD_RE_LEV 
             , BOARD_RE_SEQ 
             , BOARD_WRITER 
             , BOARD_SUBJECT 
             , BOARD_CONTENT 
             , BOARD_HITS
             , DEL_YN 
             , INS_USER_ID
             , CAST( DATE_FORMAT( INS_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS INS_DATE
             , UPD_USER_ID
             , CAST( DATE_FORMAT( UPD_DATE, '%Y-%m-%d %H:%i:%s' ) AS CHAR(19) ) AS UPD_DATE  
        FROM SP_BOARD
        WHERE BOARD_SEQ = #{board_seq}
    </select>
    
    <!-- 글 등록 -->
    <insert id="insertBoard" parameterType="net.spring.study.board.form.BoardForm">
        INSERT
            INTO SP_BOARD
            (
                BOARD_RE_REF
                  , BOARD_RE_LEV
                  , BOARD_RE_SEQ
                  , BOARD_WRITER
                  , BOARD_SUBJECT
                  , BOARD_CONTENT
                  , INS_USER_ID
                  , INS_DATE
                  , UPD_USER_ID
                  , UPD_DATE
            )
        VALUES
            (
                 0
                , 0
                , 0
                , #{board_writer}
                , #{board_subject}
                , #{board_content}
                , 'NONMEMBER'
                , NOW() 
                , 'NONMEMBER'
                , NOW() 
            )    
    </insert>
    
    <!-- 글 삭제 -->
    <delete id="deleteBoard" parameterType="net.spring.study.board.form.BoardForm">
        DELETE 
            FROM SP_BOARD
        WHERE BOARD_SEQ = #{board_seq}    
    </delete>
    
    <!-- 글 수정 -->
    <update id="updateBoard" parameterType="net.spring.study.board.form.BoardForm">
        UPDATE SP_BOARD
            SET BOARD_SUBJECT = #{board_subject}
             ,    BOARD_CONTENT = #{board_content}
             ,    UPD_USER_ID = 'NONMEMBER'
             ,    UPD_DATE = NOW()
            WHERE BOARD_SEQ = #{board_seq} 
    </update>
cs

 

  • 프론트앤드 작업
  • 내가 블로그를 참고해서 게시판을 만들고 있는 가장 큰 목적은 Spring MVC 패턴 익히기 !!
  • 그래서 프론트 앤드 부분은 상세하게 살펴보진 않았다..
  • 그래서 이 부분 코드는 따로 올리지 않겠다...참고 블로그 내용을 따로 가져오기가 좀 그래서...맨 위 상단 링크 참고해주세요.
  • TMI를 말하자면 요즘 학원에서 팀프로젝트 중이라 프론트 부분 CSS, JSP를 지겹도록 만드는 중이기도 하고..^^

  • 1. boardList.jsp
  • 2. boardWrite.jsp
  • 3. boardUpdate.jsp
  • 4. boardDetail.jsp

 

요즘 수업 시간에 스프링 게시판 만들기를 하고 있는데 너무 따라하기 어려워서

블로그를 참고해서 각종 .xml 설정하는 것부터 하나하나 작성해보고,,

참고하는 와중에도 에러가 나서 해결해보고,,

수업시간에 안해봤던 MySQL도 연동해보고..

정말 좋은 공부였던 것 같다!!!

 

블로그에 좋은 공부 자료 올려주시는 수많은 (예비&현직)개발자 분들 항상 감사합니다...

이거 일요일 오후 3시?부터 시작했는데 언제 월요일 밤 12시가 되어버린건지....^^

내일 수업 시간에는 제발 삽질을 덜했으면 좋겠다..

반응형
profile

my code archive

@얼레벌레 개발자👩‍💻

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

반응형