// 게시글 상세 보기
@RequestMapping(value="detail", mehtod=RequestMethod.GET)
public String boardDetail(Integer no, //조회할 글 번호
Model model, //요청 응답에 사용
RedirectAttributes rdAttr, // 조회 실패시 이전목록으로 리다이렉트할때 메세지 전달
HttpServletRequest request // 이전 페이지(referer) url 얻어오기
) {
// 이전 페이지 주소를 얻어옴
String beforeUrl = request.getHeader("referer");
try {
// 1) 글 번호를 이용하여 Board 테이블에서 해당 게시글 조회
Board board = boardService.selectBoard(no);
// 2) 게시글 조회 성공시
// 글 번호를 이용하여 해당 Attachment 테이블에서
// 게시글에 포함된 이미지 목록 조회
if(Board != null) {
List<Attachment> files = boardService.selectFiles(no);
if(!files.isEmpty()) { //해당 게시글에 이미지가 있다면
model.addAttribute("files", files);
}
// 3) 조회수 증가
boardService.increaseCount(no);
board.setBoardCount(board.getBoardCount() + 1);
model.addAttribute("board", board);
return "board/boardDetail";
} else {
rdAttr.addFlashAttribute("msg", "게시글 상세 조회 실패");
return "redirect"+beforeUrl;
}
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("errorMsg", "게시글상세조회과정중오류");
return "common/errorPage";
}
}
ServiceImpl
/** 게시글 조회용 Service
* @param no
* @return board
* @throws Exception
*/
@Override
public Board selectBoard(Integer no) throws Exception {
return boardDAO.selectBoard(no);
}
/** 게시글 이미지 조회용 Service
* @param no
* @return files
* @throws Exception
*/
@Override
public List<Attachment> selectFiles(Integer no) throws Exception {
return boardDAO.selectFiles(no);
}
/** 게시글 조회수 증가용 Service
* @param no
* @throws Exception
*/
@Transactional(rollbackFor = Exception.class)
@Override
public void increaseCount(Integer no) throws Exception {
boardDAO.increaseCount(no);
}
boardDAO
/** 게시글 조회용 DAO
* @param no
* @return board
* @throws Exception
*/
public Board selectBoard(Integer no) throws Exception{
return sqlSession.selectOne("boardMapper.selectBoard", no);
}
/** 게시글 이미지 조회용 DAO
* @param no
* @return files
* @throws Exception
*/
public List<Attachment> selectFiles(Integer no) throws Exception{
return sqlSession.selectList("boardMapper.selectFiles", no);
}
/** 게시글 count 증가용 DAO
* @param boardNo
* @throws Exception
*/
public void increaseCount(int no) throws Exception{
sqlSession.update("boardMapper.increaseCount", no);
}
boardMapper
<!-- 게시글 조회용 조회 -->
<select id="selectBoard" parameterType="_int"
resultMap="boardResultSet>
SELECT * FROM V_BOARD
WHERE BOARD_NO = #{boardNo}
</select>
<!-- 게시글 이미지 리스트 조회 -->
<select id="selectFiles" parameterType="_int"
resultMap="attachmentResultSet">
SELECT * FROM ATTACHMENT
WHERE BOARD_ID = #{no}
AND FILE_STATUS = 'Y'
</select>
<!-- 게시글 조회수 증가 -->
<update id="increaseCount" parameterType="_int">
UPDATE BOARD SET BOARD_COUNT = BOARD_COUNT + 1
WHERE BOARD_NO = #{no}
</update>
'study > Spring' 카테고리의 다른 글
12_spring_(ajax사용한 댓글) (0) | 2020.03.16 |
---|---|
11_spring_(★파일수정) (0) | 2020.03.13 |
9_Spring(파일업로드2) (0) | 2020.03.11 |
8_Spring(파일업로드1) (0) | 2020.03.09 |
7_Spring(페이징) (0) | 2020.02.29 |