[B -2-11] CRUD 게시판 수정 구현

2019. 9. 30. 13:06Project 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

modify()

 

...더보기
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";
	}
}

게시판 서비스 인터페이스 작성 : 수정

 

src/main/java

com.spms.service

BoardService.java 인터페이스

modify()

 

...더보기

 

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);
}

게시판 서비스 인터페이스 구현 작성 : 수정

 

src/main/java

com.spms.service

BoardServiceImpl.java

modify()

 

...더보기
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;
	}
}

 

return boardMapper.update(board) == 1;

boardMapper.update(board) 호출의 결과, 즉, 게시판 수정 처리를 한 결과 정상적으로 완료되었으면, 1, 비상적 완료이면, 0값이 리턴되어지는데, 정상완료(1)인지 비교해서 TRUE / FALSE 값인 boolean 값을 리턴한다. 


게시판 매퍼 인터페이스 작성 : 수정

 

src/main/java

com.spms.mapper

BoardMapper.java 인터페이스

update()

 

...더보기
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);
}

게시판 매퍼 인터페이스 구현 작성 (SQL XML) : 수정

 

src/main/resources

com

spms

mapper

BoardMapper.xml

update()

 

...더보기
<?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>
	
</mapper>

게시판 수정 유닛테스트 작성

 

src/test/java

com.spms.controller

BoardControllerTests.java

testModify()

 

...더보기
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()
		);
	}

	@Test
	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);

	}
}

 


 

게시판 수정 유닛테스트 결과

 

...더보기
INFO : jdbc.sqltiming - update TBL_BOARD set TITLE = '수정된 테스트 새글 제목', CONTENT = '수정된 테스트 새글 내용', WRITER = 'editor01', 
UPDATEDATE = SYSDATE where BNO = 2 

 

반응형