controller

@GetMapping("/list")
    public String findAll(Model model){
        List<BoardDTO> boardDTOList  = boardService.findAll();
        model.addAttribute("boardList", boardDTOList);
        System.out.println("boardDTOList = " + boardDTOList);
        return "list";
    }

db에서 조회한 데이터를 화면으로 가져오기 위해 Model객체를 사용한다.
또 목록이기 때문에 데이터가 여러개니까 list로 가져오자.
마지막model.addAttribute 코드가 모델에 담는 과정.

service

   public List<BoardDTO> findAll() {
        return boardRepository.findAll();
    }

repository의 findAll을 받아와서 컨트롤러에 리턴하는 역할

Repository

 public List<BoardDTO> findAll() {
        return sql.selectList("Board.findAll");
    }

selectList를 호출한 결과를 넘긴다.

board-mapper

 <select id="findAll" resultType="board">
        select * from board_table order by id desc
    </select>

파라미터가 없기 때문에 resultType을 board로 한다.

list.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
  table, tr, td, th {
      border: 1px solid black;
      border-collapse: collapse;
  }
  th, td {
      padding: 10px;
  }
</style>
<body>
<a href="/save">글쓰기</a>
<table>
  <tr>
    <th>번호</th>
    <th>제목</th>
    <th>작성자</th>
    <th>작성시간</th>
    <th>조회수</th>
  </tr>
  <tr th:each="board: ${boardList}">
    <td th:text="${board.id}"></td>
    <td th:text="${board.boardTitle}"></td>
    <td th:text="${board.boardWriter}"></td>
    <td th:text="${board.createdAt}"></td>
    <td th:text="${board.boardHits}"></td>
  </tr>
</table>
</body>
</html>

boardDTO에 있는 객체 정보를 가져오는 것이다.


이렇게 결과를 확인할 수 있다.

profile
초보 개발자 이야기

0개의 댓글

Powered by GraphCDN, the GraphQL CDN