오늘은 제가 만든 CRUD프로젝트 코드를 공유해보자합니다.
대학에서 교수님께서 주신 자료와 구글링을 통해서 만들어졌고
제가 조금 수정을 진행하였습니다.
특히 이번 CRUD같은 경우는 Spring을 이용한 CRUD이므로 다음 포스팅때 어떻게 CRUD가 각각 동작하는지 올리도록 하겠습니다!
깃헙: https://github.com/Kim-YH-handong/Spring_CRUD
com.my.myapp.dao.BoardDAO
package com.my.myapp.dao;
import java.sql.ResultSet;
public class BoardDAO {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
private final String BOARD_INSERT = "insert into BOARD (name, salary, designation) values (?, ?, ?)";
private final String BOARD_UPDATE = "update BOARD set name=?, salary=?, designation=? where id=?";
private final String BOARD_DELETE = "delete from BOARD where id=?";
private final String BOARD_GET = "select * from BOARD where id=?";
private final String BOARD_LIST = "select * from BOARD order by id desc";
public int insertBoard(BoardVO vo) {
return template.update(BOARD_INSERT, new Object[] {
vo.getName(),
vo.getSalary(),
vo.getDesignation()
});
}
public int deleteBoard(int id) {
return template.update(BOARD_DELETE, new Object[] {
id
});
}
public int updateBoard(BoardVO vo) {
return template.update(BOARD_UPDATE, new Object[] {
vo.getName(),
vo.getSalary(),
vo.getDesignation(),
vo.getId()
});
}
public BoardVO getBoard(int id) {
return template.queryForObject(BOARD_GET,
new Object[] {id},
new BeanPropertyRowMapper<BoardVO>(BoardVO.class));
}
public List<BoardVO> getBoardList(){
return template.query(BOARD_LIST, new RowMapper<BoardVO>() {
@Override
public BoardVO mapRow(ResultSet rs, int rowNum) throws SQLException{
BoardVO data = new BoardVO();
data.setId(rs.getInt("id"));
data.setName(rs.getString("name"));
data.setSalary(rs.getInt("salary"));
data.setDesignation(rs.getString("designation"));
return data;
}
});
}
}
com.my.myapp.bean.BoardVO
package com.my.myapp.bean;
public class BoardVO {
private int id;
private String name;
private int salary;
private String designation;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
com.my.myapp.HomeController
package com.my.myapp;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
com.my.myapp.controller.BoardController
package com.my.myapp.controller;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.my.myapp.bean.BoardVO;
import com.my.myapp.dao.BoardDAO;
@Controller
public class BoardController {
@Autowired
BoardDAO boardDAO;
@RequestMapping(value = "/board/list", method = RequestMethod.GET)
public String boardlist(Model model) {
model.addAttribute("list", boardDAO.getBoardList());
return "board/list";
}
@RequestMapping(value = "/board/add", method = RequestMethod.GET)
public String addPost() {
return "board/addpostform";
}
@RequestMapping(value = "/board/addok", method = RequestMethod.POST)
public String addPostOk(BoardVO vo) {
int i = boardDAO.insertBoard(vo);
if (i==0)
System.out.println("데이터 추가 실패");
else
System.out.println("데이터 추가 성공!!!");
return "redirect:list";
}
@RequestMapping(value = "/board/editform/{id}", method = RequestMethod.GET)
public String editPost(@PathVariable("id") int id, Model model) {
BoardVO boardVO = boardDAO.getBoard(id);
model.addAttribute("boardVO", boardVO);
return "board/editform";
}
@RequestMapping(value = "/board/editok", method = RequestMethod.POST)
public String editPostOk(BoardVO vo) {
int i = boardDAO.updateBoard(vo);
if(i==0)
System.out.println("데이터 수정 실패");
else
System.out.println("데이터 수정 성공!!");
return "redirect:list";
}
@RequestMapping(value = "/board/deleteok/{id}", method = RequestMethod.GET)
public String deletePostOk(@PathVariable("id") int id, Model model) {
boardDAO.deleteBoard(id);
return "redirect:../list";
}
}
WEB-INF/views/home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
<P> <a href="board/list">게시판으로 이동</a>
</body>
</html>
WEB-INF/views/board/addpostform.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="addok" method="post">
<table id="edit">
<tr><td>이름</td><td><input type="text" name="name"/></td></tr>
<tr><td>salary</td><td><input type="text" name="salary"/></td></tr>
<tr><td>designation</td><td><input type="text" name="designation"/></td></tr>
</table>
<button type="button" onclick="location.href='list'">목록보기</button>
<button type="submit">등록하기</button>
</form>
</body>
</html>
WEB-INF/views/board/editform.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.my.myapp.bean.BoardVO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%
BoardVO vo = (BoardVO)request.getAttribute("boardVO");
%>
<body>
<form method="post" action="../editok">
<input type="hidden" name="id" value = "<%=vo.getId()%>"/>
<table id="edit">
<tr>
<td>이름</td>
<td><input type="text" name="name" value = "<%=vo.getName()%>"/></td>
</tr>
<tr>
<td>salary</td>
<td><input type="text" name="salary" value = "<%=vo.getSalary()%>" /></td>
</tr>
<tr>
<td>designation</td>
<td><input type="text" name="designation" value = "<%=vo.getDesignation()%>"/></td>
</tr>
</table>
<button type="button" onclick="history.back()">취소하기</button>
<button type="submit">수정하기</button>
</form>
</body>
</html>
WEB-INF/views/board/list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="com.my.myapp.bean.BoardVO" %>
<%@page import="java.util.List" %>
<jsp:useBean id="dao" class="com.my.myapp.dao.BoardDAO"></jsp:useBean>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>boardList</title>
</head>
<script>
function delete_ok(id){
var a = confirm("정말로 삭제하시겠습니까?");
if(a) location.href='deleteok/' + id;
}
</script>
<%
List<BoardVO> list = (List<BoardVO>)request.getAttribute("list");
%>
<body>
<table>
<c:forEach items="${list}" var="u">
<tr>
<td>${u.id}</td>
<td>${u.name}</td>
<td>${u.salary}</td>
<td>${u.designation}</td>
<td><a href="editform/${u.id}">글수정</a></td>
<td><a href="javascript:delete_ok('${u.id}')">글삭제</a></td>
</tr>
</c:forEach>
</table>
<br/><button type="button" onclick="location.href='add'">새글쓰기</button>
</body>
</html>