boardMapper.xml
<select id="getBoardName" parameterType="Integer" resultType="String">
SELECT board_name
FROM board_info
WHERE info_idx =#{info_idx}
</select>


๊ธ์ด์ด ์ด๋ฆ์ ์์ ,writer_idx๋ ์์
userํ ์ด๋ธ๊ณผ join์ด ํ์ํจ

user_idx์ writer_idx๋ ๋์ผํจ
MySQL์์ ํ ์คํธํด๋ณด๊ธฐ


<select id="getBoardList" parameterType="Integer" resultType="com.demo.domain.ContentVO">
SELECT board_idx, title, u.name, regDate
FROM board b
JOIN user u
ON b.writer_idx = u.user_idx
WHERE info_idx = #{info_idx}
ORDER BY board_idx DESC ;
</select>
์ต์ ๊ธ ์์ผ๋ก ์กฐํ, ๊ฒ์ํ ๋ฒํธ(๋ฉ๋ด)๋ก ๊ฒ์
๊ฒ์ํ ๋ฒํธ๋ก ๊ฒ์ํ๋๊น ํ๋ผ๋ฏธํฐ ํ์ ์ ์ ์
๊ฒฐ๊ณผ๋ ContentVO๋ก ๊ฐ์ ธ์ด
ContentVO์์ฑ - ์กฐ์ธํ ์ ๋ณด๋ค์ ๋ด์ ๊ฐ์ฒด(๊ฒ์ํ ๋ชฉ๋ก์ ๋ด์ ๊ฐ์ฒด)
package com.demo.domain;
import java.util.Date;
public class ContentVO {
private int board_idx; //๊ธ๋ฒํธ
private String title; //์ ๋ชฉ
private String name; //์์ฑ์
private Date regDate; //์์ฑ๋ ์ง
//๊ฒํฐ ์ธํฐ ์์ฑ
public int getBoard_idx() {
return board_idx;
}
public void setBoard_idx(int board_idx) {
this.board_idx = board_idx;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
}
BoardDAO
//๊ฒ์๊ธ ์ ๋ชฉ ์ถ๋ ฅ
public String getBoardName(int info_idx) {
return sqlTemplate.selectOne("board.getBoardName", info_idx);
}
//๊ฒ์๊ธ ๋ชฉ๋ก ์ถ๋ ฅ
public List<ContentVO> getContentList(int info_idx){
return sqlTemplate.selectList("board.getBoardList", info_idx);
}
BoardService
//๊ฒ์ํ ์ ๋ชฉ ์ถ๋ ฅ
public String getBoardName(int info_idx) {
return boardDAO.getBoardName(info_idx);
}
//๊ฒ์๊ธ ๋ชฉ๋ก ์ถ๋ ฅ
public List<ContentVO> getContentList(int info_idx){
return boardDAO.getContentList(info_idx);
}
BoardController
@GetMapping("/main")
public String main(@RequestParam("info_idx") int info_idx,Model model) {
model.addAttribute("info_idx", info_idx);
model.addAttribute("boardName",boardService.getBoardName(info_idx));
model.addAttribute("list", boardService.getContentList(info_idx));
return "board/main";
}
main.jsp ์์
<!-- ๊ฒ์๊ธ ๋ฆฌ์คํธ -->
<div class="container" style="margin-top: 100px">
<div class="card shadow">
<div class="card-body">
<h4 class="card-title">${boardName}</h4>
<table class="table table-hover" id="board_list">
<thead>
<tr>
<th class="text-center d-none d-md-table-cell">๊ธ๋ฒํธ</th>
<th class="w-50">์ ๋ชฉ</th>
<th class="text-center d-none d-md-table-cell">์์ฑ์</th>
<th class="text-center d-none d-md-table-cell">์์ฑ๋ ์ง</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="obj">
<tr>
<td class="text-center d-none d-md-table-cell">${obj.board_idx}</td>
<td><a href="board_read.html">${obj.title}</a></td>
<td class="text-center d-none d-md-table-cell">${obj.name}</td>
<td class="text-center d-none d-md-table-cell">${obj.regDate}</td>
</tr>
</c:forEach>
</tbody>
</table>


๊ธ์ฐ๊ธฐ์ ๊ฒฝ์ฐ ์ฌ์ง ์๋ฃ์ผ๋ฉด ์๋ฌ ๋ฐ์ํจ ใ
content_file์ด null๊ฐ์ผ ๊ฒฝ์ฐ์ NULL์ด ๋๋๋ก Mapper๊ณผ DB์ค์ ์์

<insert id="newBoard" parameterType="com.demo.domain.BoardVO">
INSERT INTO
board(title,content,content_file, writer_idx,info_idx)
VALUES( #{title},#{content},#{content_file,jdbcType=VARCHAR},#{writer_idx},#{info_idx}
)
</insert>



๊ทธ๋ฆฌ๊ณ ์๊น ์ฒ๋ผ

ํ๋๋ฐ์ ์๋ ๊ธ์ธ๋ฐ board_idx๋ก ๋ถ๋ฌ์ค๋๊น ๊ธ ๋ฒํธ๊ฐ 6๋ฒ์ผ๋ก ํ๊ธฐ๋จ
๊ทธ๋์ ์ด์ง ์์ ํจ
<%int i = 1; %>
<c:forEach items="${list}" var="obj">
<tr>
<td class="text-center d-none d-md-table-cell"> <%= i++ %></td><!--${obj.board_idx}์ด์๋ค๊ฐ ์์ -->
๋ ์ง ํ์ ์์ ํจ
main.jsp ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%int i = 1; %>
<c:forEach items="${list}" var="obj">
<tr>
<td class="text-center d-none d-md-table-cell"> <%= i++ %></td><!--${obj.board_idx}์ด์๋ค๊ฐ ์์ -->
<td><a href="board_read.html">${obj.title}</a></td>
<td class="text-center d-none d-md-table-cell">${obj.name}</td>
<td class="text-center d-none d-md-table-cell">
<fmt:formatDate value="${obj.regDate}" pattern="yyyy-MM-dd"/>
</td>
</tr>
</c:forEach>

DB์ ์ ์ฅ๋ ๋ ์ง์ ๋ค๋ฅด๊ฒ
๋ฐ์ฌ๋ฆผ? ๋๊ฒ ์ถ๋ ฅ์ด ๋ผ์ db๋ฐ VO์์
BoardMapper.xml
<select id="getBoardList" parameterType="Integer" resultType="com.demo.domain.ContentVO">
SELECT board_idx, title, u.name, DATE_FORMAT(regDate,'%y-%m-%d') AS regDate
FROM board b
JOIN user u
ON b.writer_idx = u.user_idx
WHERE info_idx = #{info_idx}
ORDER BY board_idx DESC ;
</select>
ContentVO - regDate String์ผ๋ก ์์ ํ๊ณ ๊ฒํฐ ์ธํฐ๋ ๋ฐ๊พผ๋ค
package com.demo.domain;
public class ContentVO {
private int board_idx; //๊ธ๋ฒํธ
private String title; //์ ๋ชฉ
private String name; //์์ฑ์
private String regDate; //์์ฑ๋ ์ง
//๊ฒํฐ ์ธํฐ ์์ฑ
public int getBoard_idx() {
return board_idx;
}
public void setBoard_idx(int board_idx) {
this.board_idx = board_idx;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegDate() {
return regDate;
}
public void setRegDate(String regDate) {
this.regDate = regDate;
}
}
main.jsp
<!-- ๊ฒ์๊ธ ๋ฆฌ์คํธ -->
<div class="container" style="margin-top: 100px">
<div class="card shadow">
<div class="card-body">
<h4 class="card-title">${boardName}</h4>
<table class="table table-hover" id="board_list">
<thead>
<tr>
<th class="text-center d-none d-md-table-cell">๊ธ๋ฒํธ</th>
<th class="w-50">์ ๋ชฉ</th>
<th class="text-center d-none d-md-table-cell">์์ฑ์</th>
<th class="text-center d-none d-md-table-cell">์์ฑ๋ ์ง</th>
</tr>
</thead>
<tbody>
<%int i = 1; %>
<c:forEach items="${list}" var="obj">
<tr>
<td class="text-center d-none d-md-table-cell"> <%= i++ %></td><!--${obj.board_idx}์ด์๋ค๊ฐ ์์ -->
<td><a href="board_read.html">${obj.title}</a></td>
<td class="text-center d-none d-md-table-cell">${obj.name}</td>
<td class="text-center d-none d-md-table-cell">${obj.regDate}</td>
</tr>
</c:forEach>
</tbody>
</table>'BACKEND > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - ๊ธ ์์ฑ ํ ์์ฑํ๋ ๋ฉ๋ด๋ก ๋์๊ฐ๊ธฐ (0) | 2023.11.01 |
|---|---|
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2- ๊ธ ์์ธ๋ณด๊ธฐ ํ์ด์ง (1) | 2023.11.01 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - ์ด๋ฏธ์ง ์ ๋ก๋ ์ค์ ๋ฐ DB์ ์ฅ (1) | 2023.10.31 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - ๊ธ ์์ฑํ๊ธฐ(์ ํจ์ฑ๊ฒ์ฌ) (0) | 2023.10.31 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ 2 - ๊ธ ์์ฑํ๊ธฐ (get) (1) | 2023.10.31 |