게시물 관리 프로그램 수정사항
- Repo -> mapper로 수정
- service 클래스를 만들어서 중간 처리를 담당하게함
- 조회수가 5이상인 게시물에 특정 마킹하는 기능 추가
- 1시간 이내 게시물 (새 게시물) 인지 확인하는 기능 추가
BoardRepository 내용-> BoardMapper로 모두 옮김
package com.study.springstudy.springmvc.chap04.mapper;
import com.study.springstudy.springmvc.chap04.entity.Board;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BoardMapper {
List<Board> findAll();
Board findOne(int boardNo);
boolean save(Board board);
boolean delete(int boardNo);
void upViewCount(int boardNo);
}
Controller에 있는 비즈니스 로직 Service로 옮김
- BoardListResponseDto에
조회수가 5이상인 게시물에 특정 마킹하는 기능 추가,
1시간 이내 게시물 (새 게시물) 인지 확인하는 기능 추가하여 Service에서 중간처리
package com.study.springstudy.springmvc.chap04.service;
import com.study.springstudy.springmvc.chap04.dto.BoarWriteRequestdDto;
import com.study.springstudy.springmvc.chap04.dto.BoardDetailResponseDto;
import com.study.springstudy.springmvc.chap04.dto.BoardListResponseDto;
import com.study.springstudy.springmvc.chap04.entity.Board;
import com.study.springstudy.springmvc.chap04.mapper.BoardMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@RequiredArgsConstructor
@Service
public class BoardService {
private final BoardMapper mapper;
public List<BoardListResponseDto> getList() {
List<Board> BoardList = mapper.findAll();
List<BoardListResponseDto> dtoList = BoardList.stream()
.map(b -> new BoardListResponseDto(b))
.collect(Collectors.toList());
return dtoList;
}
public boolean insert(BoarWriteRequestdDto dto) {
return mapper.save(new Board(dto));
}
public boolean remove(int boardNo) {
return mapper.delete(boardNo);
}
public BoardDetailResponseDto retrieve(int bno) {
Board b = mapper.findOne(bno);
if(b != null) mapper.upViewCount(bno);
BoardDetailResponseDto dto = new BoardDetailResponseDto(b);
return dto;
}
}
Controller 수정
@Controller
@RequestMapping("/board")
@RequiredArgsConstructor
public class BoardController {
private final BoardService service;
@GetMapping("/list")
public String list(Model model) {
List<BoardListResponseDto> dtos = service.getList(page);
model.addAttribute("BList", dtos);
return "board/list";
}
@GetMapping("/write")
public String open() {
return "board/write";
}
@PostMapping("/write")
public String register(BoarWriteRequestdDto dto) {
service.insert(dto);
return "redirect:/board/list";
}
@GetMapping("/delete")
public String delete(int boardNo) {
service.remove(boardNo);
return "redirect:/board/list";
}
@GetMapping("/detail")
public String detail(int bno, Model model) {
BoardDetailResponseDto b = service.retrieve(bno);
model.addAttribute("bbb", b);
return "board/detail";
}
}
BoardMapper.xml 작성 -> Repo에 있던 것 옮김, ? -> #{}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.study.springstudy.springmvc.chap04.mapper.BoardMapper">
<select id="findAll" resultType="board">
SELECT * FROM tbl_board
</select>
<select id="findOne" resultType="board">
SELECT * FROM tbl_board
WHERE board_no = #{boardNo}
</select>
<insert id="save">
INSERT INTO tbl_board
(title, content, writer)
VALUES (#{title}, #{content}, #{writer})
</insert>
<delete id="delete">
DELETE FROM tbl_board
WHERE board_no = #{boardNo}
</delete>
<update id="upViewCount">
UPDATE tbl_board
SET view_count = view_count + 1
WHERE board_no = #{boardNo}
</update>
</mapper>