SpringBoot 게시판 만들기

스프링부트(SpringBoot) 게시판 만들기4 - CRUD

dev109 2017. 8. 20.
반응형

게시판 목록, 쓰기, 상세, 수정, 삭제






1. BoardMapper.java, BoardMapper.xml에 내용 추가


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
26
27
28
29
package com.example.demo.board.mapper;
 
import java.util.List;
 
import org.springframework.stereotype.Repository;
 
import com.example.demo.board.domain.BoardVO;
 
@Repository("com.example.demo.board.mapper.BoardMapper")
public interface BoardMapper {
    //게시글 개수  
    public int boardCount() throws Exception;
    
    //게시글 목록  
    public List<BoardVO> boardList() throws Exception;
    
    //게시글 상세
    public BoardVO boardDetail(int bno) throws Exception;
    
    //게시글 작성  
    public void boardInsert(BoardVO board) throws Exception;
    
    //게시글 수정  
    public void boardUpdate(BoardVO board) throws Exception;
    
    //게시글 삭제  
    public void boardDelete(int bno) throws Exception;
}
 
cs



BoardMapper.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
<?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.example.demo.board.mapper.BoardMapper">
 
    <select id="boardCount" resultType="int">
        SELECT
            COUNT(*)
        FROM BOARD
    </select>
    
    <select id="boardList" resultType="com.example.demo.board.domain.BoardVO">
        SELECT
            *
        FROM BOARD
    </select>
    
    <select id="boardDetail" parameterType="int" resultType="com.example.demo.board.domain.BoardVO">
        SELECT
            *
        FROM BOARD
        WHERE
            BNO = #{bno}
    </select>
    
    <insert id="boardInsert" parameterType="com.example.demo.board.domain.BoardVO">
        INSERT INTO
        BOARD (BNO, SUBJECT,CONTENT,WRITER,REG_DATE)
        VALUES(#{bno},#{subject},#{content},#{writer},now()) 
    </insert>
    
    <update id="boardUpdate" parameterType="com.example.demo.board.domain.BoardVO">
        UPDATE BOARD
            SET
        <if test="subject != null">
            SUBJECT = #{subject}
        </if>
        <if test="subject != null and content != null"> , </if>
        <if test="content != null">
            CONTENT = #{content}
        </if>
        WHERE BNO = #{bno}
    </update>
    
    <delete id="boardDelete" parameterType="int">
        DELETE FROM BOARD WHERE BNO = #{bno}
    </delete>
</mapper>
 
cs





2. 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
package com.example.demo.board.controller;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.example.demo.board.domain.BoardVO;
import com.example.demo.board.service.BoardService;
 
@Controller
public class BoardController {
 
    @Resource(name="com.example.demo.board.service.BoardService")
    BoardService mBoardService;
    
    @RequestMapping("/list"//게시판 리스트 화면 호출  
    private String boardList(Model model) throws Exception{
        
        model.addAttribute("list", mBoardService.boardListService());
        
        return "list"//생성할 jsp
    }
    
    @RequestMapping("/detail/{bno}"
    private String boardDetail(@PathVariable int bno, Model model) throws Exception{
        
        model.addAttribute("detail", mBoardService.boardDetailService(bno));
        
        return "detail";
    }
    
    @RequestMapping("/insert"//게시글 작성폼 호출  
    private String boardInsertForm(){
        
        return "insert";
    }
    
    @RequestMapping("/insertProc")
    private int boardInsertProc(HttpServletRequest request) throws Exception{
        
        BoardVO board = (BoardVO) request.getParameterMap();
        
        return mBoardService.boardInsertService(board);
    }
    
    @RequestMapping("/update/{bno}"//게시글 수정폼 호출  
    private String boardUpdateForm(@PathVariable int bno, Model model) throws Exception{
        
        model.addAttribute("detail", mBoardService.boardDetailService(bno));
        
        return "update";
    }
    
    @RequestMapping("/updateProc")
    private int boardUpdateProc(HttpServletRequest request) throws Exception{
        
        BoardVO board = (BoardVO) request.getParameterMap();
        
        return mBoardService.boardUpdateService(board);
    }
 
    @RequestMapping("/delete/{bno}")
    private String boardDelete(@PathVariable int bno) throws Exception{
        
        mBoardService.boardDeleteService(bno);
        
        return "redirect:/list";
    }
}
cs





3. 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
package com.example.demo.board.service;
 
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.example.demo.board.domain.BoardVO;
import com.example.demo.board.mapper.BoardMapper;
 
@Service("com.example.demo.board.service.BoardService")
public class BoardService {
 
    @Resource(name="com.example.demo.board.mapper.BoardMapper")
    BoardMapper mBoardMapper;
    
    public List<BoardVO> boardListService() throws Exception{
        
        return mBoardMapper.boardList();
    }
    
    public BoardVO boardDetailService(int bno) throws Exception{
        
        return mBoardMapper.boardDetail(bno);
    }
    
    public int boardInsertService(BoardVO board) throws Exception{
        
        return mBoardMapper.boardInsert(board);
    }
    
    public int boardUpdateService(BoardVO board) throws Exception{
        
        return mBoardMapper.boardUpdate(board);
    }
    
    public int boardDeleteService(int bno) throws Exception{
        
        return mBoardMapper.boardDelete(bno);
    }
}
 
cs





4. webapp - WEB-INF - views에 jsp 파일 생성 후 작성


list.jsp

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${list}
</body>
</html>
cs



detail.jsp

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${detail}
</body>
</html>
cs



insert.jsp

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
insert
</body>
</html>
cs



update.jsp

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${detail}
</body>
</html>
cs






localhost:8080/요청url 로 확인



반응형

댓글

💲 추천 글