← 홈으로 돌아가기
Spring

Spring 게시판 만들기(11)

insert된 데이터를 다시 가져와서 출력하기

MaBatios xml 작성

INSERT문을 통해 SQL에 입력된 데이터를 다시 가져와서 JSP로 출력을 한다

컨트롤러에서 Get방식으로 호출을 한다

BoardMapper.xml 파일에서 select문을 작성한다

	<!-- SELECT문 사용시만 resultType속성 필수. 다른 sql 구문은 사용 안함 -->
	<select id="getList" resultType="com.demo.domain.BoardVO">
		select bno, title, content, writer, regdate from tbl_board order by bno desc
	</select>
BoardMapper 작성

이후 SELECT문 안에 있는 getList를 메서드로 하는 BoardMapper 작성

package com.demo.mapper;

import java.util.List;

import com.demo.domain.BoardVO;

public interface BoardMapper {

	void insert(BoardVO vo);
	
	// List가 util 메서드로 void 사용 X
	// BoardVO에 들어간 데이터를 SQL에서 리스트 형식으로 가져온다
	List<BoardVO> getList();
	
}
Impl 구현

이후 BoardServiceImpl에서 override형식으로 getList를 가져온다

	@Override
	public List<BoardVO> getList(){
		return mapper.getList();
	}
인터페이스 BoardService에서 연결
List<BoardVO> getList();
컨트롤러 연결

컨트롤러해서 GetMapping을 통해서 데이터를 가져오고 jsp로 연결해 출력하는 작업을 한다

	@GetMapping("/list")
	public void list(Model model) {
		
		// list를 가져오는 역할
		List<BoardVO> list = service.getList();
		
		// addAttribute를 통해 list.jsp에 넣는 역할
		model.addAttribute("list", list);
		// 첫번째 "list"는 list.jsp를 의미함
		
		log.info("list called...");
	}

그리고 받는 list.jsp 파일을 수정한다

꼭 taglib 설정을 다시 확인한다

주의사항

<%-- html 주석태그에 서버관련을 삽입하면 그대로 실행된다 되도록 jsp 주석 이용 --%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>


<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Hugo 0.88.1">
    <title>Pricing example · Bootstrap v4.6</title>

    <link rel="canonical" href="https://getbootstrap.com/docs/4.6/examples/pricing/">

    

    <!-- Bootstrap core CSS -->
<link href="/resources/css/bootstrap.min.css" rel="stylesheet">


    <style>
      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>

    
    <!-- Custom styles for this template -->
    <link href="/resources/css/pricing.css" rel="stylesheet">
  </head>
  <body>


<!-- header -->
<%@ include file="/WEB-INF/views/include/header.jsp" %>

<%@ include file="/WEB-INF/views/include/carousel.jsp" %>

<div class="container">
	<h3>게시판 글목록</h3>
	<table class="table table-bordered">
  <thead>
    <tr>
      <th scope="col">글번호</th>
      <th scope="col">제목</th>
      <th scope="col">작성자</th>
      <th scope="col">등록일</th>
    </tr>
  </thead>
  <tbody>
  
  	<!-- var에 넣은 변수가 BoardVO의 필드가 된다 -->
  	<%-- html 주석태그에 서버관련을 삽입하면 그대로 실행된다 되도록 jsp 주석 이용 --%>
  	<%-- ${} 안에 넣는 입력은 model.addAttribute의 앞쪽 ""내용 --%>
  	<c:forEach items="${list }" var="board">
    
    <tr>
    <!-- BoardVO의 get메서드를 호출한다 -->
      <th scope="row"><c:out value="${board.bno }" /></th>
      <td><c:out value="${board.title }" /></td>
      <td><c:out value="${board.writer }" /></td>
      <td><fmt:formatDate value="${board.regdate }" pattern="yyyy-MM-dd hh:mm" /></td>
    </tr>
    </c:forEach>
    
  </tbody>
</table>

<!-- footer -->
<%@ include file="/WEB-INF/views/include/footer.jsp" %>
</div>

  </body>
</html>
개발공부 정리노트 | Progressive overload