[B -2-12] CRUD 게시판 삭제 구현
2019. 9. 30. 13:30ㆍProject B (SPMS)/Project B 파트2
반응형
게시판 라우팅 테이블 설계
작 업 | URL | 메소드 | 파라미터 | 입력화면 필요 | URL 이동 |
전체 목록 | /board/list | GET | × | × | × |
등록 처리 | /board/register | POST | 전체 항목 | 〇 | 〇 |
상세 조회 | /board/read | GET | 글번호 | × | × |
수정 처리 | /board/remove | POST | 전체 항목 | 〇 | 〇 |
삭제 처리 | /board/modify | POST | 글번호 | 〇 | 〇 |
게시판 구현을 하기에 앞서서 게시판 라우팅 설계 작성을 하는게 좋다.
게시판 컨트롤러 작성 : 삭제
src/main/java
com.spms.controller
BoardController.java
remove()
...더보기
package com.spms.controller;
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.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.spms.domain.BoardVO;
import com.spms.service.BoardService;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
@Controller
@Log4j
@RequestMapping("/board/*")
@AllArgsConstructor
public class BoardController {
private BoardService boardService;
@GetMapping("/list")
public void list(Model model) {
log.info("list");
boardService.getList().forEach(board -> log.info(board));
model.addAttribute("list", boardService.getList());
}
@PostMapping("/register")
public String register(BoardVO board, RedirectAttributes rttr) {
log.info("register : " + board);
boardService.register(board);
rttr.addFlashAttribute("result", board.getBno());
return "redirect:/board/list";
}
@GetMapping("/get")
public void get(@RequestParam("bno") Long bno, Model model) {
log.info("/get");
model.addAttribute("board", boardService.get(bno));
}
@PostMapping("/modify")
public String modify(BoardVO board, RedirectAttributes rttr) {
log.info("modify:" + board);
if (boardService.modify(board)) {
rttr.addFlashAttribute("result", "success");
}
return "redirect:/board/list";
}
@PostMapping("/remove")
public String remove(@RequestParam("bno") Long bno, RedirectAttributes rttr) {
log.info("remove..." + bno);
if (boardService.remove(bno)) {
rttr.addFlashAttribute("result", "success");
}
return "redirect:/board/list";
}
}
게시판 서비스 인터페이스 작성 : 삭제
src/main/java
com.spms.service
BoardService.java 인터페이스
remove()
...더보기
package com.spms.service;
import java.util.List;
import com.spms.domain.BoardVO;
public interface BoardService {
public List<BoardVO> getList();
public void register(BoardVO board);
public BoardVO get(Long bno);
public boolean modify(BoardVO board);
public boolean remove(Long bno);
}
게시판 서비스 인터페이스 구현 작성 : 삭제
src/main/java
com.spms.service
BoardServiceImpl.java
remove()
...더보기
package com.spms.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.spms.domain.BoardVO;
import com.spms.mapper.BoardMapper;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
@Log4j
@Service
@AllArgsConstructor
public class BoardServiceImpl implements BoardService {
private BoardMapper boardMapper;
@Override
public List<BoardVO> getList() {
log.info("getList....");
return boardMapper.getList();
}
@Override
public void register(BoardVO board) {
log.info("register......" + board);
boardMapper.insertSelectKey(board);
}
@Override
public BoardVO get(Long bno) {
log.info("get......" + bno);
return boardMapper.read(bno);
}
@Override
public boolean modify(BoardVO board) {
// TODO Auto-generated method stub
log.info("modify......" + board);
return boardMapper.update(board) == 1;
}
@Override
public boolean remove(Long bno) {
log.info("remove...." + bno);
return boardMapper.delete(bno) == 1;
}
}
게시판 매퍼 인터페이스 작성 : 삭제
src/main/java
com.spms.mapper
BoardMapper.java 인터페이스
delete()
...더보기
package com.spms.mapper;
import java.util.List;
import com.spms.domain.BoardVO;
public interface BoardMapper {
public List<BoardVO> getList();
public Long insertSelectKey(BoardVO board);
public BoardVO read(Long bno);
public int update(BoardVO board);
public int delete(Long bno);
}
게시판 매퍼 인터페이스 구현 작성 (SQL XML) : 삭제
src/main/resources
com
spms
mapper
BoardMapper.xml
delete()
...더보기
<?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.spms.mapper.BoardMapper">
<!-- 목록 조회 -->
<select id="getList" resultType="com.spms.domain.BoardVO">
<![CDATA[
select *
from TBL_BOARD
where BNO > 0
]]>
</select>
<!-- 등록 (선택키 활용) -->
<insert id="insertSelectKey">
<selectKey keyProperty="bno" order="BEFORE" resultType="long">
select SEQ_BOARD.nextval
from DUAL
</selectKey>
insert into TBL_BOARD (
BNO, TITLE, CONTENT, WRITER
)
values (
#{bno}, #{title}, #{content}, #{writer}
)
</insert>
<!-- 조회 -->
<select id="read" resultType="com.spms.domain.BoardVO">
select *
from TBL_BOARD
where BNO = #{bno}
</select>
<!-- 수정 -->
<update id="update">
update TBL_BOARD set
TITLE = #{title},
CONTENT = #{content},
WRITER = #{writer},
UPDATEDATE = SYSDATE
where BNO = #{bno}
</update>
<!-- 삭제 -->
<delete id="delete">
delete TBL_BOARD
where BNO = #{bno}
</delete>
</mapper>
게시판 삭제 유닛테스트 작성
src/test/java
com.spms.controller
BoardControllerTests.java
testRemove()
...더보기
package com.spms.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {
com.spms.config.RootConfig.class,
com.spms.config.ServletConfig.class} )
@Log4j
public class BoardControllerTests {
@Setter(onMethod_ = { @Autowired })
private WebApplicationContext ctx;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
}
public void testList() throws Exception {
log.info(
mockMvc.perform(MockMvcRequestBuilders.get("/board/list")
).andReturn().getModelAndView().getModelMap());
}
public void testRegister() throws Exception {
String resultPage = mockMvc
.perform(MockMvcRequestBuilders.post("/board/register")
.param("title", "테스트 새글 제목")
.param("content", "테스트 새글 내용")
.param("writer", "user00"))
.andReturn().getModelAndView().getViewName();
log.info(resultPage);
}
public void testGet() throws Exception{
log.info(mockMvc.perform(MockMvcRequestBuilders
.get("/board/get")
.param("bno", "2")
).andReturn().getModelAndView().getModelMap()
);
}
public void testModify() throws Exception {
String resultPage = mockMvc
.perform(MockMvcRequestBuilders.post("/board/modify")
.param("bno", "2")
.param("title", "수정된 테스트 새글 제목")
.param("content", "수정된 테스트 새글 내용")
.param("writer", "editor01"))
.andReturn().getModelAndView().getViewName();
log.info(resultPage);
}
@Test
public void testRemove() throws Exception {
// 삭제전 데이터베이스에 게시물 번호 확인할 것
String resultPage = mockMvc
.perform(MockMvcRequestBuilders
.post("/board/remove")
.param("bno", "9"))
.andReturn().getModelAndView().getViewName();
log.info(resultPage);
}
}
MockMvc
브라우저에서 요청과 응답을 의미하는 객체로서 컨트롤러 사용을 용이하게 해주는 라이브러리이다.
MockMvc를 이용해서 파라미터를 전달할 때에는 문자열로만 처리해야한다.
게시판 삭제 유닛테스트 결과
...더보기
INFO : jdbc.sqltiming - delete TBL_BOARD where BNO = 9
반응형
'Project B (SPMS) > Project B 파트2' 카테고리의 다른 글
[B -2-14] 기본 화면 처리 2 (0) | 2019.09.30 |
---|---|
[B -2-13] 기본 화면 처리 1 (0) | 2019.09.30 |
[B -2-11] CRUD 게시판 수정 구현 (0) | 2019.09.30 |
[B -2-10] CRUD 게시판 조회 구현 (0) | 2019.09.30 |
[B -2-9] CRUD 게시판 등록 구현 (0) | 2019.09.30 |