728x90
- 테이블 설계
CREATE SEQUENCE seq_board;
CREATE TABLE TBL_BOARD (
BNO NUMBER(10,0), --일련번호
TITLE VARCHAR2(200) NOT NULL, -- 제목
CONTENT VARCHAR2(2000) NOT NULL, -- 내용
WRITER VARCHAR2(50) NOT NULL, -- 작성자
REGDATE DATE DEFAULT SYSDATE, -- 등록일자
UPDATEDATE DATE DEFAULT SYSDATE -- 수정일자
);
COMMIT;
ALTER TABLE TBL_BOARD
ADD CONSTRAINT PK_BOARD PRIMARY KEY(BNO);
-- primary key 제약조건이름으로 인덱스가 자동생성됨. 인덱스명 PK_BOARD
- BoardVO.java
package com.demo.domain;
import java.util.Date;
import lombok.Data;
@Data
public class BoardVO {
// bno, title, content, writer, regdate, updatedate
private Long bno;
private String title;
private String content;
private String writer;
private Date regdate;
private Date updatedate;
}
- BoardController.java
package com.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.demo.domain.BoardVO;
import com.demo.service.BoardService;
import lombok.extern.log4j.Log4j;
@Log4j
@Controller
@RequestMapping("/board/*")
public class BoardController {
@Autowired
private BoardService boardService;
// 글쓰기 폼.
@GetMapping("/write")
public void write() {
log.info("write called...");
}
// 글쓰기 저장
@PostMapping("/write")
public String write(BoardVO vo, RedirectAttributes rttr) {
log.info("입력데이타 : " + vo);
boardService.insert(vo);
rttr.addFlashAttribute("msg", "write");
return "redirect:/board/list";
}
// 리스트(목록). 데이타를 JSP에 전달하여 출력
@GetMapping("/list")
public void list(Model model) {
log.info("list called...");
List<BoardVO> list = boardService.getList();
model.addAttribute("list", list);
}
// 게시물 읽기 및 수정폼. 데이타를 jsp에 전달하여 출력
// 읽기주소 /board/get?bno=3 수정폼주소 / board/modify?bno=3
// @GetMapping("/get")
@GetMapping(value = {"/get", "/modify"})
public void get(Long bno, Model model) {
log.info("게시물번호: " + bno);
BoardVO board = boardService.get(bno);
model.addAttribute("board", board);
}
// 게시물 수정하기
@PostMapping("/modify")
public String modify(BoardVO vo, RedirectAttributes rttr) {
log.info("수정 내용 : " + vo);
boardService.modify(vo);
rttr.addFlashAttribute("msg", "modify");
return "redirect:/board/list";
}
// 게시물 삭제하기
@GetMapping("/remove")
public String remove(Long bno) {
log.info("삭제 게시물번호 : " + bno);
boardService.remove(bno);
return "redirect:/board/list";
}
}
- BoardMapper.java
package com.demo.mapper;
import java.util.List;
import com.demo.domain.BoardVO;
public interface BoardMapper {
// 리스트
public List<BoardVO> getList();
// 글 저장하기
public void insert(BoardVO vo);
// 글 읽기(조회)
public BoardVO get(Long bno);
// 글 수정하기
public void modify(BoardVO vo);
// 글 삭제하기
public void remove(Long bno);
}
- BoardMapper.xml
<?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.demo.mapper.BoardMapper">
<insert id = "insert" parameterType = "com.demo.domain.BoardVO">
insert into tbl_board(bno, title, content, writer)
values(seq_board.nextval, #{title}, #{content}, #{writer})
</insert>
<select id = "getList" resultType="com.demo.domain.BoardVO">
select
bno, title, content, writer, regdate, updatedate
from
tbl_board
order by
bno desc
</select>
<select id = "get" resultType="com.demo.domain.BoardVO" parameterType="long">
select bno, title, content, writer, regdate, updatedate
from
tbl_board
where
bno = #{bno}
</select>
<update id = "modify" parameterType = "com.demo.domain.BoardVO">
update tbl_board
set title = #{title}, content = #{content}
where bno = #{bno}
</update>
<delete id = "remove" parameterType="long">
delete from tbl_board
where bno = #{bno}
</delete>
</mapper>
- BoardService.java
package com.demo.service;
import java.util.List;
import com.demo.domain.BoardVO;
public interface BoardService {
public void insert(BoardVO vo);
public List<BoardVO> getList();
public BoardVO get(Long bno);
public void modify(BoardVO vo);
public void remove(Long bno);
}
- BoardServiceImpl
package com.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.domain.BoardVO;
import com.demo.mapper.BoardMapper;
@Service
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardMapper boardMapper;
@Override
public void insert(BoardVO vo) {
boardMapper.insert(vo);
}
@Override
public List<BoardVO> getList() {
// TODO Auto-generated method stub
return boardMapper.getList();
}
@Override
public BoardVO get(Long bno) {
// TODO Auto-generated method stub
return boardMapper.get(bno);
}
@Override
public void modify(BoardVO vo) {
// TODO Auto-generated method stub
boardMapper.modify(vo);
}
@Override
public void remove(Long bno) {
// TODO Auto-generated method stub
boardMapper.remove(bno);
}
}
- get.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#btnModify").click(function() {
let bno = $(this).data("bno");
location.href = "/board/modify?bno=" + bno;
});
$("#btnRemove").click(function() {
if(!confirm("삭제 하시겠습니까?")) return;
let bno = $(this).data("bno");
location.href = "/board/remove?bno=" + bno;
});
});
</script>
// -----------------------------------------------------------------------
<!--------------------------
| Your Page Content Here |
-------------------------->
<div class="row">
<div class="col-md-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">게시판 조회</h3>
</div>
<div class="box-body">
<div class="form-group">
<label for="title">Bno</label>
<input type="text" class="form-control" value="${board.bno }" readonly>
</div>
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" value="${board.title }" readonly>
</div>
<div class="form-group">
<label for="content">Content</label>
<%-- <input type="text" class="form-control" value="${board.content }" readonly>--%>
<c:out value="${board.content }" />
</div>
<div class="form-group">
<label for="writer">Writer</label>
<input type="text" class="form-control" value="${board.writer }" readonly>
</div>
<div class="form-group">
<label for="writer">Regdate</label>
<input type="text" class="form-control" value="<fmt:formatDate value="${board.regdate }" pattern="yyyy-MM-dd hh:mm"/>" readonly>
</div>
<div class="form-group">
<label for="writer">Updatedate</label>
<input type="text" class="form-control" value="<fmt:formatDate value="${board.regdate }" pattern="yyyy-MM-dd hh:mm"/>" readonly>
</div>
<button type="button" class="btn btn-primary" id="btnModify" data-bno="${board.bno }">Modify</button>
<button type="button" class="btn btn-primary" id="btnRemove" data-bno="${board.bno }">Remove</button>
<button type="button" class="btn btn-primary" id="btnList">List</button>
</div>
</div>
</div>
</div>
</section>
- list.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<script>
if('${msg}' == 'write') {
alert("새글이 등록됨");
}else if('${msg}' == 'modify'){
alert("글이 수정됨");
}
</script>
<!--------------------------
| Your Page Content Here |
-------------------------->
<div class="row">
<div class="col-md-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Dorothy List</h3>
</div>
<div class = "box">
<div class = "box-header with-border">
<h3 class = "box-title"> Dorothy Table</h3>
</div>
<div class = "box-body">
<table class = "table table-bordered">
<tbody>
<tr>
<th style = "width : 10%">bno</th>
<th style = "width : 30%">Title</th>
<th style = "width : 20%">Writer</th>
<th style = "width : 40%">Regdate</th>
</tr>
<c:forEach items = "${list}" var="board">
<tr>
<td>${board.bno}</td>
<td>
<a href="/board/get?bno=${board.bno}"><c:out value="${board.title}"/></a>
</td>
<td>
<c:out value = "${board.writer}" /></td>
<td>
<fmt:formatDate value = "${board.regdate}" pattern = "yyyy-MM-dd hh:mm"/>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div class="box-footer clearfix">
<ul class="pagination pagination-sm no-margin pull-right">
<li><a href="#">«</a></li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">»</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</section>
- modify.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!--------------------------
| Your Page Content Here |
-------------------------->
<div class="row">
<div class="col-md-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">게시판 글수정</h3>
</div>
<form role="form" method="post" action="modify">
<div class="box-body">
<div class="form-group">
<label for="bno">Bno</label>
<input type="text" class="form-control" id="bno" name="bno" value="<c:out value="${board.bno }" />" readonly>
</div>
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="제목 입력" value="<c:out value="${board.title }" />">
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea class="form-control" aria-label="With textarea" name="content"><c:out value="${board.content }" /></textarea>
</div>
<div class="form-group">
<label for="writer">Writer</label>
<input type="text" class="form-control" id="writer" name="writer" placeholder="작성자 입력" value="<c:out value="${board.writer }" />" readonly>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
</section>
- write.jsp
<!--------------------------
| Your Page Content Here |
-------------------------->
<div class="row">
<div class="col-md-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">게시판 글쓰기</h3>
</div>
<form role="form" method="post" action="write">
<div class="box-body">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="제목 입력">
</div>
<div class="form-group">
<label for="content">Content</label>
<input type="text" class="form-control" id="content" name="content" placeholder="내용 입력">
</div>
<div class="form-group">
<label for="writer">Writer</label>
<input type="text" class="form-control" id="writer" name="writer" placeholder="작성자 입력">
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</section>
728x90
'🤯TIL > Spring' 카테고리의 다른 글
[Spring] @log4j 란? / @log4j 와 System.out.println (0) | 2022.12.21 |
---|---|
[어노테이션] @ResponseBody / @ResponseEntity<T> / @RequestParam("") 정리 (0) | 2022.12.15 |
[게시판 만들기 - 4 ] 게시판 수정하기 / 삭제하기 (0) | 2022.12.09 |
[게시판 만들기 - 3 ] 게시판 읽기 (0) | 2022.12.06 |
[게시판 만들기 - 2 ] 게시판 목록 (0) | 2022.12.05 |