오늘의 코딩순서
(폴더: oBootBoard)
메인페이지 http://localhost:8383/list
- JdbcDao.class
- SpringConfig.class + BController.class+
- 글 수정=>
- Dto아닌 버전: BExecuteCommand.class (현장 HW 1-1) + BDao.interface + JdbcDao.class(현장 HW 1-2)
- Dto 버전: BDto.class + BExecuteCommand.class + BDao.interface+ JdbcDao.class
- 글 삭제 => BController.class + BExecuteCommand.class (현장 HW 2-1) + JdbcDao.class(현장 HW 2-2)
- 신규 글 작성 => write_view.jsp + BController.class + BExecuteCommand.class (현장 HW 3-1) + JdbcDao.class(현장 HW 3-2)
- 답변 작성창 => reply_view.jsp + BController.class + BExecuteCommand.class (현장 HW 4-1) + JdbcDao.class(현장 HW 4-2)
+++ 답변 입력 => BController.class + BExecuteCommand.class (현장 HW 5-1) + JdbcDao.class(현장 HW 5-2)
오늘의 코딩 포인트
- JdbcDao.class ==> @Repository 주석처리후 아래 SpringConfig.class 작성
package com.oracle.oBootBoard.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Repository;
import com.oracle.oBootBoard.dto.BDto;
//@Repository
public class JdbcDao implements BDao {
//JDBC 사용
private final DataSource dataSource;
@Autowired
public JdbcDao(DataSource dataSource) {
this.dataSource = dataSource;
}
private Connection getConnection() {
return DataSourceUtils.getConnection(dataSource);
}
@Override
public ArrayList<BDto> boardList() {
ArrayList<BDto> bList = new ArrayList<BDto>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
System.out.println("BDao boardList start...");
// 현장 HW 1 => list.jsp와 연결
try {
connection = getConnection();
String query = "SELECT bId, bName, bTitle, bContent, bDate, bHit,"
+ " bGroup, bStep, bIndent"
+ " From mvc_board order by bGroup desc, bStep asc";
preparedStatement = connection.prepareStatement(query);
System.out.println("BDao query->"+query);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
BDto dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit,
bGroup, bStep, bIndent);
bList.add(dto);
}
} catch (SQLException e) {
System.out.println("list dataSource->" + e.getMessage());
e.printStackTrace();
} finally {
try {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (Exception e2) {
System.out.println("e2.getMessage->"+e2.getMessage());
e2.printStackTrace();
}
}
return bList;
}
// 현장 HW 2 => content_view.jsp와 연결
@Override
public BDto contentView(String strId) {
BDto bDto = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
upHit(strId);
System.out.println("contentView start...");
try {
connection = getConnection();
String query = "select * from mvc_board where bId = ?";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(strId));
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
bDto = new BDto(bId, bName, bTitle, bContent, bDate, bHit,
bGroup, bStep, bIndent);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bDto;
}
// 현장 HW 3. 히트수 올리기
private void upHit(String bId) {
Connection connection = null;
PreparedStatement preparedStatement = null;
System.out.println("upHit start...");
try {
connection = getConnection();
String query = "update mvc_board set bHit = bHit + 1 where bId = ?";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, bId);
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
- SpringConfig.class
Tip)- dependency⭐
- 약결합(Loosely-coupled)⭐
- 결합도를 약하게 하면 아래의 소스를 필요에 따라 유연하게 변경이 가능함
package com.oracle.oBootBoard;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.oracle.oBootBoard.dao.BDao;
import com.oracle.oBootBoard.dao.JdbcDao;
@Configuration
public class SpringConfig {
private final DataSource dataSource;
public SpringConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public BDao jdbcDao() {
// BDao를 기재하면 상속과 dependency를 이용하여 결합도를 약하게 하고,
// 아래의 소스를 필요에 따라 유연하게 변경이 가능함
return new JdbcDao(dataSource);
}
}
- BController.class ==> 글 수정 로직
package com.oracle.oBootBoard.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.oracle.oBootBoard.command.BExecuteCommand;
import jakarta.servlet.http.HttpServletRequest;
@Controller
public class BController {
private static final Logger logger = LoggerFactory.getLogger(BController.class);
private final BExecuteCommand bExecuteService;
@Autowired
public BController(BExecuteCommand bExecuteService) {
this.bExecuteService = bExecuteService;
}
@RequestMapping(value = "/modify" , method=RequestMethod.POST)
public String modify(HttpServletRequest request , Model model) {
logger.info("modify start...");
model.addAttribute("request",request);
bExecuteService.bModifyCmd(model);
return "redirect:list";
}
}
- BExecuteCommand.class (현장 HW 1-1)
// (0802) 현장 HW 1-1
// 1. model Map선언
public void bModifyCmd(Model model) {
System.out.println("BExecuteCommand bModifyCmd start...");
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
// 2. parameter -> bId, bName , bTitle , bContent
String bId = request.getParameter("bId");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
BDto board = jdbcDao.contentView(bId);
// 3. jdbcDao.modify(bId, bName, bTitle, bContent);
model.addAttribute("mvc_board",board);
// Tip: 이름은 커뮤니케이션비용이 발생하지 않도록 직관적으로, 일관성있게
// 여기서는 위의 contentView와 연결된 것을 암시하기 위해 이름을 똑같게 함
// 위처럼 Dto 방식으로 하거나
// jdbcDao.modify(bId, bName, bTitle, bContent);
}
- BDao.interface
- JdbcDao.class(현장 HW 1-2)
// (0802) 현장 HW 1-2
// 1. model Map선언
// 2. parameter -> bId, bName , bTitle , bContent
// 3. jdbcDao.modify(bId, bName, bTitle, bContent);
private void modify(String bId, String bName, String bTitle, String bContent) {
Connection connection = null;
PreparedStatement preparedStatement = null;
System.out.println("modify start...");
try {
connection = getConnection();
String query = "update mbc_board set bName = ?, bTitle = ?,"
+ "bContent = ? where bId = ?";
System.out.println("BDao modify query->"+query);
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, bName);
preparedStatement.setString(2, bTitle);
preparedStatement.setString(3, bContent);
preparedStatement.setInt(4, Integer.parseInt(bId));
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(preparedStatement != null) preparedStatement.close();
if(connection != null) connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
- BController.class ==> 글 삭제
@RequestMapping("/delete")
public String delete(HttpServletRequest request, Model model) {
System.out.println("delete()");
model.addAttribute("request",request);
bExecuteService.bDeleteCmd(model);
return "redirect:list";
}
- BExecuteCommand.class (현장 HW 2-1)
// (0802) 현장 HW 2-1
// 1) model이용 , map 선언
public void bDeleteCmd(Model model) {
System.out.println("BExecuteCommand bDeleteCmd start...");
// 2) request 이용 -> bId 추출
Map<String, Object> map = model.asMap();
HttpServletRequest request = (HttpServletRequest)map.get("request");
String bId = request.getParameter("bId");
jdbcDao.delete(bId);
}
- JdbcDao.class(현장 HW 2-2)
// (0802) 현장 HW 2-2
// 3) dao instance 선언
// 4) delete method 이용하여 삭제
@Override
public void delete(String bId) {
Connection connection = null;
PreparedStatement preparedStatement = null;
System.out.println("delete start...");
try {
connection = getConnection();
String query = "delete from mvc_board where bId = ?";
System.out.println("BDao delete query->"+query);
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(bId));
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
- 새 시퀸스 생성
- write_view.jsp ==> 신규 글 작성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="write" method="post">
<table width="500" border="1">
<tr>
<td> 이름 </td>
<td> <input type="text" name="bName" size="50"></td>
</tr>
<tr>
<td> 제목 </td>
<td> <input type="text" name="bTitle" size="50"></td>
</tr>
<tr>
<td> 내용 </td>
<td> <textarea name="bContent" rows="10"></textarea></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="입력">
<a href="list">목록보기</a></td>
</tr>
</table>
</form>
</body>
</html>
- BController.class
@RequestMapping("/write_view")
public String write_view(Model model) {
logger.info("write_view start...");
return "write_view";
}
@RequestMapping(value = "/write")
public String write(HttpServletRequest request, Model model) {
logger.info("write start...");
model.addAttribute("request",request);
bExecuteService.bWriteCmd(model);
return "redirect:list";
}
- BExecuteCommand.class (현장 HW 3-1)
// (0802) 현장 HW 3-1
public void bWriteCmd(Model model) {
System.out.println("BExecuteCommand bwriteCmd start...");
// 1) model이용 , map 선언
Map<String, Object> map = model.asMap();
// 2) request 이용 -> bName ,bTitle , bContent 추출
HttpServletRequest request = (HttpServletRequest)map.get("request");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
BDto bDto = new BDto(bName, bTitle, bContent);
jdbcDao.write(bName, bTitle, bContent);
}
- JdbcDao.class(현장 HW 3-2)
// (0802) 현장 HW 3-2
// 3) dao instance 선언
// 4) write method 이용하여 저장(bName, bTitle, bContent)
// bid, bGroup,,bHit, bStep, bIndent, bDate
// -> mvc_board_seq,mvc_board_seq, 0 , 0 , 0, sysdate
public void write(String bName, String bTitle, String bContent) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection();
String query =
"Insert Into mvc_board (bId, bName, bTitle, bContent, bHit, bGroup, bStep, bIndent, bDate ) "
+ "Values (mvc_board_seq.nextval,?, ?, ?, 0, mvc_board_seq.currval, 0, 0 , sysdate)";
System.out.println("BDao write query-->" + query );
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, bName);
preparedStatement.setString(2, bTitle);
preparedStatement.setString(3, bContent);
int rn = preparedStatement.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
System.out.println("write dataSource-->" + e.getMessage() );
e.printStackTrace();
} finally {
try {
if(preparedStatement != null) preparedStatement.close();
if(connection != null) connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
- reply_view.jsp ==> 답변 작성 기능
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="reply" method="post">
<input type="hidden" name="bId" value="${reply_view.bId}">
<input type="hidden" name="bGroup" value="${reply_view.bGroup}">
<input type="hidden" name="bStep" value="${reply_view.bStep}">
<input type="hidden" name="bIndent" value="${reply_view.bIndent}">
<table width="500" border="1">
<tr>
<td> 번호 </td><td>${reply_view.bId}</td>
</tr>
<tr>
<td> 히트 </td><td>${reply_view.bGroup}</td>
</tr>
<tr>
<td> 이름 </td>
<td><input type="text" name="bName" value="${reply_view.bName}"></td>
</tr>
<tr>
<td> 제목 </td><td>${reply_view.bTitle}</td>
<td><input type="text" name="bTitle" value="답변" +"${reply_view.bTitle}"></td>
</tr>
<tr>
<td> 내용 </td>
<td><textarea rows="10" name="bContent">${reply_view.bContent}</textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="답변저장">
<a href="list">목록</a></td>
</tr>
</table>
</form>
</body>
</html>
- BController.class
@RequestMapping("/reply_view")
public String reply_view(HttpServletRequest request, Model model){
System.out.println("reply_view start..");
model.addAttribute("request", request);
bExecuteService.bReplyViewCmd(model);
return "reply_view";
}
- BExecuteCommand.class (현장 HW 4-1)
/ (0802) 현장 HW 4-1
public void bReplyViewCmd(Model model) {
// 1) model이용 , map 선언
Map<String, Object> map = model.asMap();
// 2) request 이용 -> bid 추출
HttpServletRequest request = (HttpServletRequest) map.get("request");
String bId = request.getParameter("bId");
// 3) dao instance 선언
BDto dto = jdbcDao.reply_view(bId);
// 4) reply_view method 이용하여 (bid)
// - BDto dto = dao.reply_view(bId);
model.addAttribute("reply_view", dto);
}
- JdbcDao.class(현장 HW 4-2)
// (0802) 현장 HW 4-2
// - BDto dto = dao.reply_view(bId);
@Override
public BDto reply_view(String strbId) {
BDto dto = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
String query = "select * from mvc_board where bId = ?";
preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(strbId));
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
int bId = resultSet.getInt("bId");
String bName = resultSet.getString("bName");
String bTitle = resultSet.getString("bTitle");
String bContent = resultSet.getString("bContent");
Timestamp bDate = resultSet.getTimestamp("bDate");
int bHit = resultSet.getInt("bHit");
int bGroup = resultSet.getInt("bGroup");
int bStep = resultSet.getInt("bStep");
int bIndent = resultSet.getInt("bIndent");
dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if(resultSet != null) resultSet.close();
if(preparedStatement != null) preparedStatement.close();
if(connection != null) connection.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
return dto;
+++
- BController.class ==> 답변 입력
@RequestMapping(value="/reply", method=RequestMethod.POST)
public String reply(HttpServletRequest request, Model model) {
System.out.println("reply()");
model.addAttribute("request",request);
bExecuteService.bReplyCmd(model);
return "redirect:list";
}
질문목록
수업교재
오늘의 숙제
- BExecuteCommand.class (HW 1-1)
>>내가 한 것
// (0802) HW 1-1
public void bReplyCmd(Model model) {
System.out.println("BExecuteCommand bReplyCmd start...");
// 1) model이용 , map 선언
Map<String, Object> map = model.asMap();
// 2) request 이용 -> bid, bName , bTitle, bContent, bGroup, bStep , bIndent 추출
HttpServletRequest request = (HttpServletRequest) map.get("request");
String bId = request.getParameter("bId");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
String bGroup = request.getParameter("bGroup");
String bStep = request.getParameter("bStep");
String bIndent = request.getParameter("bIndent");
// 3) dao instance 선언
// 4) reply method 이용하여 댓글저장
BDto dto = new BDto(Integer.parseInt(bStep), bName, bTitle, bContent, bGroup, bStep, bIndent);
jdbcDao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);
}
>> 정답
// (0802) HW 1-1
public void bReplyCmd(Model model) {
System.out.println("BExecuteCommand bReplyCmd start...");
// 1) model이용 , map 선언
Map<String, Object> map = model.asMap();
// 2) request 이용 -> bid, bName , bTitle, bContent, bGroup, bStep , bIndent 추출
HttpServletRequest request = (HttpServletRequest) map.get("request");
String bId = request.getParameter("bId");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
String bGroup = request.getParameter("bGroup");
String bStep = request.getParameter("bStep");
String bIndent = request.getParameter("bIndent");
// 3) dao instance 선언
// 4) reply method 이용하여 댓글저장
int bIntGroup = Integer.parseInt(request.getParameter("bGroup"));
jdbcDao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);
}
- JdbcDao.class (HW 1-2)
>>내가 한 것
// (0802) HW 1-2
// - dao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);
@Override
public void reply(String bId, String bName, String bTitle, String bContent,
String bGroup, String bStep, String bIndent) {
// 홍해 기적
replyShape(bGroup,bStep);
Connection conn = null;
PreparedStatement pstmt = null;
System.out.println("reply start...");
try {
conn = getConnection();
String query = "insert into mvc_board values(mvc_board_seq.nextval,?,?,?,sysdate,0,?,?,?)";
// 위의 괄호 안의 값들은 오라클 데이터 열 기준
System.out.println("BDao reply query->"+query);
pstmt = conn.prepareStatement(query);
// 아래는 물음표 개수 기준으로 작성
pstmt.setString(1, bName);
pstmt.setString(2, bTitle);
pstmt.setString(3, bContent);
pstmt.setInt(4, Integer.parseInt(bGroup));
pstmt.setInt(5, Integer.parseInt(bStep));
pstmt.setInt(6, Integer.parseInt(bIndent));
int rn = pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("reply exception 1 -->" + e.getMessage() );
e.printStackTrace();
}
}
public void replyShape(String bGroup, String bStep) {
BDto dto = null;
Connection conn = null;
PreparedStatement pstmt = null;
System.out.println("replyShape start...");
int num = dto.getbId();
try {
if (num != 0) {
conn = getConnection();
String query = "update board set bStep = bStep+1, bIndent = bIndent+1 where bId = ?";
System.out.println("BDao replyShape query->"+query);
pstmt = conn.prepareStatement(query);
pstmt.setString(1, bGroup);
pstmt.setString(2, bStep);
pstmt.executeQuery();
pstmt.close();
dto.setbStep(dto.getbStep()+1);
dto.setbIndent(dto.getbIndent()+1);
}
} catch (Exception e) {
System.out.println("replyShape e.getMessage()->"+e.getMessage());
e.printStackTrace();
}
}
>> 정답
private void replyShape(String bGroup, String bStep) {
Connection conn = null;
PreparedStatement pstmt = null;
System.out.println("replyShape start...");
try {
conn = getConnection();
String query = "update mvc_board set bStep = bStep+1"
+ "where bGroup = ? and bStep > ?";
System.out.println("BDao replyShape query->"+query);
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, Integer.parseInt(bGroup));
pstmt.setInt(2, Integer.parseInt(bStep));
int rn = pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("replyShape e.getMessage()->"+e.getMessage());
e.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
'Spring' 카테고리의 다른 글
2024_08_06_화~08_07_수 (0) | 2024.08.06 |
---|---|
2024_08_05_월~08_06_화 (0) | 2024.08.05 |
2024_08_01_목 (0) | 2024.08.01 |
2024_07_31_수 (0) | 2024.07.31 |
2024_07_30_화 (0) | 2024.07.30 |