위로
아래
글 삭제 컨트롤러
해당 게시글의 bno만 알면 손쉽게 데이터 행을 삭제할 수 있다.
jsp 파일도 따로 필요 없다.
결과를 받아 성공했는지 수정했는지 출력한다.
package com.ecom4.hi.board.control;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 com.ecom4.hi.HomeController;
import com.ecom4.hi.board.model.BoardDTO;
import com.ecom4.hi.board.model.PageDTO;
import com.ecom4.hi.board.model.RowInterPage;
import com.ecom4.hi.board.service.BoardService;
@Controller
public class BoardController {
private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
//JavaBoard에서 작업했던 Action 역할을 하는 객체 - 비즈니스 로직 BIZ를 갖고 있다
@Autowired
private BoardService boardService;
@RequestMapping(value = "/delete")
public String deleteArticle(HttpServletRequest request, HttpServletResponse response,
BoardDTO dto, Model model, PageDTO pdto) {
int r = boardService.deleteArticle(dto);
String msg = "";
if(r==0) {
msg = "삭제 실패";
} else if(r==1) {
msg = "삭제 성공";
}
model.addAttribute("pdto",pdto);
model.addAttribute("msg", msg);
model.addAttribute("page","list?curPage="+pdto.getCurPage());
return "board/MsgPage";
}
}
글삭제 Service
BoardService 인터페이스
package com.ecom4.hi.board.service;
import java.util.List;
import java.util.Map;
import com.ecom4.hi.board.model.BoardDTO;
import com.ecom4.hi.board.model.PageDTO;
public interface BoardService {
int deleteArticle(BoardDTO dto);
}
BoardServiceImpl 구현 클래스
package com.ecom4.hi.board.service;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ecom4.hi.board.dao.BoardDAO;
import com.ecom4.hi.board.model.BoardDTO;
import com.ecom4.hi.board.model.PageDTO;
import com.ecom4.hi.board.model.RowInterPage;
@Service(value = "boardService")
public class BoardServiceImpl implements BoardService {
// DAO DI(Dependency Injection)
@Autowired
private BoardDAO boardDao;
@Override
public int deleteArticle(BoardDTO dto) {
return boardDao.deleteArticle(dto);
}
}
글 삭제 DAO
package com.ecom4.hi.board.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ecom4.hi.board.model.BoardDTO;
@Repository(value = "boardDao")
public class BoardDAO {
// 자원을 DI (의존성 주입)
@Autowired
private SqlSession sqlSession;
private String namespace = "board.BoardDAO.";
public int deleteArticle(BoardDTO dto) {
return sqlSession.delete(namespace + "deleteArticle",dto);
}
}
글 삭제 Mapper
<?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 = "board.BoardDAO">
<delete id="deleteArticle">
DELETE FROM board
WHERE bno = #{bno}
</delete>
</mapper>