반응형
Spring MVC를 이용한 블로그 시스템입니다.
코드는 깃허브에서 확인하실 수 있습니다.
https://github.com/Heegene/personal_projects/tree/master/spring_blog
Heegene/personal_projects
personal project repository mostly with Spring framework - Heegene/personal_projects
github.com
<개발 환경>
개발언어: Java8
WAS: Apache Tomcat 8.5
프레임워크: Spring 4.3.5
SQL 연동: Mybatis 3.4.6
Front: Bootstrap
Editor: CKeditor5
<구현 기능>
-. 게시글, 댓글 CRUD(Create, Read, Update, Delete)
-. CKEditor를 사용한 게시글 서식 지정
-. 게시글 검색 기능
package 구조
<Controller>
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
|
package com.heegene.web.board.controller;
import javax.inject.Inject;
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 org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.heegene.common.Pagination;
import com.heegene.common.Search;
import com.heegene.web.board.dto.BoardDto;
import com.heegene.web.board.dto.ReplyDto;
import com.heegene.web.board.service.BoardService;
@Controller
@RequestMapping(value="/board")
public class BoardController {
@Inject
private BoardService boardService;
@RequestMapping(value="/getBoardList", method = RequestMethod.GET)
public String getBoardList(Model model,
@RequestParam(required=false, defaultValue = "1") int page,
@RequestParam(required=false, defaultValue = "1") int range,
@RequestParam(required=false, defaultValue = "title") String searchType,
@RequestParam(required=false) String keyword,
@ModelAttribute("search") Search search
) throws Exception {
model.addAttribute("search", search);
search.setSearchType(searchType);
search.setKeyword(keyword);
// 전체 게시글 수
int listCnt = boardService.getBoardListCnt(search);
search.pageInfo(page, range, listCnt);
model.addAttribute("pagination", search);
model.addAttribute("boardList", boardService.getBoardList(search));
return "board/index";
}
// 글쓰기 클릭 시 호출할 부분
@RequestMapping(value="/boardForm")
public String boardForm(@ModelAttribute("boardDto") BoardDto boardDto) {
return "board/boardForm";
}
// 글을 쓰고 저장할 때 호출할 부분(글 목록으로 돌아가도록)
@RequestMapping(value="/saveBoard", method = RequestMethod.POST)
public String saveBoard(@ModelAttribute("boardDto") BoardDto boardDto,@RequestParam("mode") String mode, RedirectAttributes rttr) throws Exception {
if (mode.equals("edit")) {
boardService.updateBoard(boardDto);
} else {
boardService.insertBoard(boardDto);
}
return "redirect:/board/getBoardList";
}
@RequestMapping(value = "/getBoardContent", method=RequestMethod.GET)
public String getBoardContent(Model model, @RequestParam("bid") int bid) throws Exception {
model.addAttribute("boardContent", boardService.getBoardContent(bid));
model.addAttribute("replyDto", new ReplyDto());
return "board/boardContent";
}
@RequestMapping(value = "/editForm", method=RequestMethod.GET)
public String editForm(@RequestParam("bid") int bid,
@RequestParam("mode") String mode,
Model model) throws Exception {
model.addAttribute("boardContent", boardService.getBoardContent(bid));
model.addAttribute("mode", mode);
model.addAttribute("boardDto", new BoardDto());
return "board/boardForm";
}
@RequestMapping(value = "/deleteBoard", method=RequestMethod.GET)
public String deleteBoard(RedirectAttributes rttr, @RequestParam("bid") int bid) throws Exception {
boardService.deleteBoard(bid);
return "redirect:/board/getBoardList";
}
}
|
cs |
<Service>
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
|
package com.heegene.web.menu.service;
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.heegene.web.menu.dao.MenuDao;
import com.heegene.web.menu.dto.MenuDto;
@Service
public class MenuServiceImpl implements MenuService {
@Inject
private MenuDao menuDao;
@Override
public List<MenuDto> getMenuList() throws Exception {
return menuDao.getMenuList();
}
@Override
public void saveMenu(MenuDto menuDto) throws Exception {
menuDao.saveMenu(menuDto);
}
@Override
public void updateMenu(MenuDto menuDto) throws Exception {
menuDao.updateMenu(menuDto);
}
@Override
public void deleteMenu(String code) throws Exception {
menuDao.deleteMenu(code);
}
}
|
cs |
<Mapper>
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
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?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.heegene.web.board.boardMapper">
<select id="getBoardList"
resultType="com.heegene.web.board.dto.BoardDto" >
<![CDATA[
SELECT *
FROM (SELECT ORG.*, ROWNUM AS "RNUM" FROM
(SELECT * FROM tbl_board ORDER BY BID DESC) "ORG") "A"
WHERE A.RNUM >= #{startList, jdbcType=INTEGER} AND rownum <= #{listSize, jdbcType=INTEGER}
]]>
<include refid="search"></include>
</select>
<sql id="search">
<if test="searchType=='title' and keyword != null">
AND title LIKE '%'||#{keyword}||'%'
</if>
<if test="searchType=='Content' and keyword != null">
AND content LIKE '%'||#{keyword}||'%'
</if>
<if test="searchType=='reg_id' and keyword != null">
AND reg_id LIKE '%'||#{keyword}||'%'
</if>
</sql>
<select id="getBoardContent" resultType="com.heegene.web.board.dto.BoardDto">
SELECT
bid,
cate_cd,
title,
content,
tag,
view_cnt,
reg_id,
reg_dt,
edit_dt
FROM tbl_board
WHERE bid = #{bid}
</select>
<insert id="insertBoard" parameterType="com.heegene.web.board.dto.BoardDto">
INSERT INTO tbl_board
(
cate_cd,
title,
content,
tag,
view_cnt,
reg_id,
reg_dt,
edit_dt
)
VALUES
(
'해당없음'
, #{title}
, #{content}
, #{tag}
, 0
, #{reg_id}
, sysdate
, sysdate
)
</insert>
<update id="updateBoard" parameterType="com.heegene.web.board.dto.BoardDto">
UPDATE tbl_board SET
title = #{title}
, content = #{content}
, tag = #{tag}
, edit_dt = sysdate
WHERE
bid = #{bid}
</update>
<delete id="deleteBoard" parameterType="int">
DELETE FROM tbl_board
WHERE bid = #{bid}
</delete>
<update id="updateViewCnt" parameterType="com.heegene.web.board.dto.BoardDto">
UPDATE tbl_board SET
view_cnt = view_cnt + 1
WHERE bid = #{bid}
</update>
<select id="getBoardListCnt" resultType="int">
SELECT count(*) AS listCnt
FROM tbl_board
<include refid="searchCnt"></include>
</select>
<sql id="searchCnt">
<if test="searchType=='title' and keyword != null">
WHERE title LIKE '%'||#{keyword}||'%'
</if>
<if test="searchType=='Content' and keyword != null">
WHERE Content LIKE '%'||#{keyword}||'%'
</if>
<if test="searchType=='reg_id' and keyword != null">
WHERE reg_id LIKE '%'||#{keyword}||'%'
</if>
</sql>
</mapper>
|
cs |
Spring MVC를 이용한 블로그 시스템입니다.
MVC 패턴이 익숙해질까? 했는데 치고 또 치니까 숨쉬듯 쳐지는 걸 보니, 역시 답은 계속 쳐보는 것 뿐입니다.
추가 개선 예정사항은 Spring Security 구현과 회원관리/게시판 관리 등 관리메뉴 추가입니다.
이상입니다.
반응형
'Developments' 카테고리의 다른 글
[이럴땐이렇게] Spring boot application.yml 파일 mapping value 에러발생 시 (yaml syntax) (1) | 2020.08.18 |
---|---|
200814 Spring Security 구현 (0) | 2020.08.14 |
200806 TIL: Spring 예외처리(ExceptionHandler, ControllerAdvice) (0) | 2020.08.06 |
200805 Spring boot를 이용한 일기장 Webapp (0) | 2020.08.04 |
200804 Spring MVC 게시판(ver.1.0) (0) | 2020.08.04 |