728x90
실행 순서
뷰 - 컨트롤러 - 커맨드(서비스) - DAO - DTO
테이블 생성
- bId를 위한 시퀀스 생성
구조 설정
command 패키지
<BCommand.java> - 인터페이스
<BListCommand.java>
<BWriteCommand.java>
<BContentCommand.java>
<BModifyCommand.java>
<BDeleteCommand.java>
controller 패키지
<BController.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
|
package com.javalec.spring_mvc_board.controller;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.javalec.spring_mvc_board.command.BCommand;
import com.javalec.spring_mvc_board.command.BContentCommand;
import com.javalec.spring_mvc_board.command.BDeleteCommand;
import com.javalec.spring_mvc_board.command.BListCommand;
import com.javalec.spring_mvc_board.command.BModifyCommand;
import com.javalec.spring_mvc_board.command.BWriteCommand;
@Controller
public class BController {
BCommand command;
@RequestMapping("/list")
public String list(Model model) {
//BListCommand 호출
//인터페이스로 받음
command = new BListCommand();
//BListCommand여기서 받은 결과 값을 다시 모델에 담아서
command.execute(model);
return "list";
}
@RequestMapping("/write_view")
public String write_view() {
//여기에 글쓰기 내용 넣어줌
//그리고 이 페이지에서 글쓰기 기능을 넣어주면 됨
return "write_view";
}
//write_view.jsp에서 입력한 값은 여기로 오게 됨
@RequestMapping("/write")
public String write(HttpServletRequest request, Model model) {
//입력받은 값을 "request"라는 이름으로 모델 객체에 담음
//모델 객체는 request를 가지고 넘어감
model.addAttribute("request", request);
command = new BWriteCommand();
command.execute(model);
//글을 작성하면 목록으로 가도록 redirect 설정
return "redirect:list";
}
@RequestMapping("/content_view")
public String content_view(HttpServletRequest request, Model model) {
model.addAttribute("request", request);
command = new BContentCommand();
//글번호가 담긴 모델 커맨드로 넘김
command.execute(model);
//커맨드에서 모델에 값을 넣어 다시 컨트롤러로 넘어옴
//커맨드에서 넘어온 모델을 가지고 뷰로 넘어감
return "content_view";
}
@RequestMapping(value = "/modify")
public String modify(HttpServletRequest request, Model model) {
//넘기는 이름, 뷰에서 넘어온 값
model.addAttribute("request", request);
command = new BModifyCommand();
command.execute(model);
return "redirect:list";
}
@RequestMapping("/delete")
public String delete(HttpServletRequest request, Model model) {
model.addAttribute("request", request);
command = new BDeleteCommand();
command.execute(model);
return "redirect:list";
}
}
|
cs |
dao 패키지
<BDao.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
package com.javalec.spring_mvc_board.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.javalec.spring_mvc_board.dto.BDto;
public class BDao {
DataSource dataSource;
public BDao() {
try {
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/oracle");
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<BDto> list() {
ArrayList<BDto> dtos = new ArrayList<BDto>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
String sql = "select bId, bName, bTitle, bContent, bDate, bHit from mvc_board order by bId desc";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
int bId = rs.getInt("bId");
String bName = rs.getString("bName");
String bTitle = rs.getString("bTitle");
String bContent = rs.getString("bContent");
Timestamp bDate = rs.getTimestamp("bDate");
int bHit = rs.getInt("bHit");
BDto dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit);
dtos.add(dto);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(rs != null)rs.close();
if(pstmt != null)pstmt.close();
if(con != null)con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return dtos;
}
//글 작성하는 메서드
public void write(String bName, String bTitle, String bContent) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = dataSource.getConnection();
//글번호는 시퀀스를 생성해서 사용(.nextval하면 자동으로 다음 값이 들어가게 됨)
String sql = "insert into mvc_board (bId, bName, bTitle, bContent, bHit) values(mvc_board_seq.nextval, ?,?,?,0) ";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, bName);
pstmt.setString(2, bTitle);
pstmt.setString(3, bContent);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null)pstmt.close();
if(con != null)con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
//글번호를 받아서 내용 출력
public BDto content_view(String strID) {
//아래에서 만든 조회수 업데이트 메서드를 호출
upHit(strID);
BDto dto = null;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
String sql = "select bId, bName, bTitle, bContent, bDate, bHit from mvc_board where bId=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(strID));
rs = pstmt.executeQuery();
if (rs.next()) {
int bId = rs.getInt("bId");
String bName = rs.getString("bName");
String bTitle = rs.getString("bTitle");
String bContent = rs.getString("bContent");
Timestamp bDate = rs.getTimestamp("bDate");
int bHit = rs.getInt("bHit");
dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null)pstmt.close();
if(con != null)con.close();
if(rs != null)rs.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return dto;
}
//조회수 업데이트하는 메서드
private void upHit(String bId) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = dataSource.getConnection();
String sql = "update mvc_board set bHit=bHit+1 where bId=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(bId));
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null)pstmt.close();
if(con != null)con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
//수정하는 메서드
public void modify(String bId, String bName, String bTitle, String bContent) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = dataSource.getConnection();
String sql = "update mvc_board set bName=?, bTitle=?, bContent=? where bId= ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, bName);
pstmt.setString(2, bTitle);
pstmt.setString(3, bContent);
pstmt.setInt(4, Integer.parseInt(bId));
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null)pstmt.close();
if(con != null)con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
//삭제하는 메서드
public void delete(String strID) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = dataSource.getConnection();
String sql = "delete mvc_board where bId= ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(strID));
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(pstmt != null)pstmt.close();
if(con != null)con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
|
cs |
dto 패키지
<BDto.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
|
package com.javalec.spring_mvc_board.dto;
import java.sql.Timestamp;
public class BDto {
private int bId;
private String bName;
private String bTitle;
private String bContent;
private Timestamp bDate;
private int bHit;
public BDto() {
}
public BDto(int bId, String bName, String bTitle, String bContent, Timestamp bDate, int bHit) {
this.bId = bId;
this.bName = bName;
this.bTitle = bTitle;
this.bContent = bContent;
this.bDate = bDate;
this.bHit = bHit;
}
public int getbId() {
return bId;
}
public void setbId(int bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
public String getbTitle() {
return bTitle;
}
public void setbTitle(String bTitle) {
this.bTitle = bTitle;
}
public String getbContent() {
return bContent;
}
public void setbContent(String bContent) {
this.bContent = bContent;
}
public Timestamp getbDate() {
return bDate;
}
public void setbDate(Timestamp bDate) {
this.bDate = bDate;
}
public int getbHit() {
return bHit;
}
public void setbHit(int bHit) {
this.bHit = bHit;
}
}
|
cs |
뷰
<list.jsp>
<write_view.jsp>
<content_view.jsp>
728x90
'학원 > 스프링-학원' 카테고리의 다른 글
MVC 로그인 문제 (JDBC 템플릿 사용 부분도 추가) (0) | 2022.05.19 |
---|---|
MVC_상품정보 관리 문제 (0) | 2022.05.18 |
MVC 기초 (0) | 2022.05.16 |
AOP(Aspect Oriented Programming) (0) | 2022.05.13 |
외부 파일을 이용한 설정 (0) | 2022.05.11 |