스프링 게시판 스터디 4 - List 구현하기
December 03, 2018
패키지 - 파일 만들기
-
com.javalec.bbs_prac
- HomeController.java
-
com.javalec.bbs_prac.controller
- BController.java
-
com.javalec.bbs_prac.dao
- IDao.java (인터페이스)
-
com.javalec.bbs_prac.dao.mapper
- IDao.xml
-
com.javalec.bbs_prac.dto
- BDto.java
4-1 DAO 설정
public interface IDao {
public ArrayList<BDto> list();
}
4-2 Mapper 설정
- IDao.xml
<?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="com.javalec.bbs_prac.dao.IDao">
<select id="list" resultType="com.javalec.bbs_prac.dto.BDto">
SELECT * FROM BOARD ORDER BY 1 DESC
</select>
</mapper>
4-3 Controller 설정
- BController
- 컨트롤러의 흐름을 전체적으로 이해하는 것이 필요합니다
- Controller에서 DB조작 후 리턴 -> model에 담아서 -> 뷰로 보내줌
@Controller
public class BController {
private static final Logger logger = LoggerFactory.getLogger(BController.class);
@Autowired
private SqlSession sqlSession;
// 전체적인 흐름
// 1. 사용자가 list로 url 요청
@RequestMapping("/list")
public String list(Model model) {
logger.info("list()");
// 2. DB에 접근
IDao dao = sqlSession.getMapper(IDao.class);
// 3. list()메서드 실행 -> IDao.xml에서 구현하고 있는 list메서드로 DB 조작
// 4. 뷰에서 쓸 수 있도록 model에 담음
model.addAttribute("list", dao.list());
// 5. list.jsp로 이동
return "list";
}
}
4-4 View 만들기 list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<title>게시판 리스트</title>
</head>
<body>
<div class="container">
<table class="table table-hover">
<tr>
<th>번호</th>
<th>제목</th>
<th>작성일</th>
<th>조회수</th>
<th>수정하기</th>
<th>삭제하기</th>
</tr>
<c:forEach items="${list}" var="list">
<tr>
<td>${list.bId}</td>
<td><a href="content_view?bId=${list.bId}">${list.bTitle}</a></td>
<td><%-- ${list.bDate} --%> <fmt:formatDate value="${list.bDate}" pattern="yyyy-MM-dd"/></td>
<td>${list.bCount}</td>
<td><a href="update_view?bId=${list.bId}" class="btn btn-default" role="button">수정하기</a></td>
<td><a href="delete?bId=${list.bId}" class="btn btn-default" role="button">삭제하기</a></td>
</tr>
</c:forEach>
</table>
<a href="write_view" class="btn btn-default" role="button">글쓰기</a>
</div>
<br />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
4-5 BDto 설정
- DB 컬럼 값과 멤버변수의 이름을 갖게 만들어줍니다.
- 기본 생성자, 초기화 생성자, Setter & Getter, toString 오버라이딩 해줍니다
package com.javalec.bbs_prac.dto;
import java.sql.Date;
public class BDto {
private int bId;
private String bUsername;
private String bTitle;
private String bContent;
private Date bDate;
private int bCount;
public BDto() {}
public BDto(int bId, String bUsername, String bTitle, String bContent, Date bDate, int bCount) {
this.bId = bId;
this.bUsername = bUsername;
this.bTitle = bTitle;
this.bContent = bContent;
this.bDate = bDate;
this.bCount = bCount;
}
public int getbId() {
return bId;
}
public void setbId(int bId) {
this.bId = bId;
}
public String getbUsername() {
return bUsername;
}
public void setbUsername(String bUsername) {
this.bUsername = bUsername;
}
public String getbTitle() {
return bTitle;
}
public void setbTitle(String bTitle) {
this.bTitle = bTitle;
}
public String getbContent() {
return bContent;
}
public void setbContent(String bContent) {
this.bContent = bContent;
}
public Date getbDate() {
return bDate;
}
public void setbDate(Date bDate) {
this.bDate = bDate;
}
public int getbCount() {
return bCount;
}
public void setbCount(int bCount) {
this.bCount = bCount;
}
@Override
public String toString() {
return "BDto [bId=" + bId + ", bUsername=" + bUsername + ", bTitle=" + bTitle + ", bContent=" + bContent
+ ", bDate=" + bDate + ", bCount=" + bCount + "]";
}
}
실행했을 때 데이터베이스에 있는 내용이 화면에 뜨면 반은 완성입니다. 흐름이 어떻게 되는지 다시한번 살펴봐 주세요