๊ฒ์๊ธ ์๊ฐ ๋ง์์ง๋ฉด ์คํฌ๋กค์ ๊ณ์ ๋ด๋ ค์ผ ํจ
๊ทธ๋์ ํ ํ์ด์ง์ 10๊ฐ ๋๋ 20๊ฐ , ๋ฑ ๊ฐ์๋ฅผ ์ ํ์ฌ ๋ณด์ฌ์ค ์ ์์.
ํ ์คํธ๋ฅผ ์ํด sql๋ฅผ ์์ ํด ๋ด
INSERT INTO tbl_board(title, content, writer)
SELECT title,content,writer
FROM tbl_board;
๊ธฐ์กด์ ์๋ ๋ด์ฉ๋ค์ ํ ๋ฒ ๋ ๋ฃ๋๋ค.

์ฌ๋ฌ ๋ฒ ํ๋ฉด ๊ฐ์๊ฐ ๋ง์์ง

์๋์ ๊ฐ์ด ์คํฌ๋กค์ด ๋๋ ์์ด์ง...ใ ใ


๊ธฐ์กด์ ๊ฒ์๊ธ ์กฐํ๋ฅผ ์์ ํ๋ค.
<!-- ๊ฒ์๊ธ ๋ชฉ๋ก ์กฐํ(10๊ฐ) ์ต์ ์ -->
<select id="list" resultType="com.board.domain.BoardVO">
SELECT bno, title, content, writer,
regDate, viewCnt
FROM tbl_board
ORDER BY bno desc
limit 10;
</select>
ํ ์คํธํ๊ธฐ
์ต์ ๊ธ 10๊ฐ๋ง ์ถ๋ ฅ๋จ์ ํ์ธ ๊ฐ๋ฅํ๋ค.

limit 0, 10 ์ผ๋ก ์์ ํ๋ฉด, ๊ฒฐ๊ณผ์ค 0๋ฒ์งธ๋ฅผ ์์์ผ๋ก 10๊ฐ๊ฐ ์ถ๋ ฅ๋จ.
select
bno, title, writer, regDate, viewCnt
from tbl_board
order by bno desc
limit 0, 10

๋ง์ฝ 2ํ์ด์ง
select
bno, title, writer, regDate, viewCnt
from tbl_board
order by bno desc
limit 10, 10

์์ SQL๋ฌธ์ ์ด์ฉํด์ ํ์ด์ง์ ํด๋ณผ๊ฑฐ์

boardMapper.xml
<!-- ๊ฒ์๋ฌผ ์ด ๊ฐฏ์ -->
<select id="count" resultType="int">
SELECT COUNT(bno)
FROM tbl_board
</select>
BoardDAO
//๊ฒ์๊ธ ์ด ๊ฐฏ์
public int count() throws Exception;
์ ๋ ฅ์ ์๊ณ ๋ฆฌํด์ ์ ์ํ์
BoardDAOImple
//๊ฒ์๊ธ ์ ์ฒด ๊ฐฏ์
@Override
public int count() throws Exception {
return sqlTemplate.selectOne("board.count");
}
BoardService
//๊ฒ์๊ธ ์ด ๊ฐฏ์
public int count() throws Exception;
BoardServiceImple
//๊ฒ์๊ธ ์ ์ฒด ๊ฐฏ์
@Override
public int count() throws Exception {
return boardDAO.count();
}

Mapper ์์
์๊น ์ต์ ์ 10๊ฐ ์ถ๋ ธํ๋ sql์
limit ์ง์ฐ๊ณ ์ต์ ์์ผ๋ก๋ง ์กฐํํ๋๊ฑธ๋ก ์์ ํจ
<!-- ๊ฒ์๊ธ ๋ชฉ๋ก ์กฐํ ์ต์ ์ -->
<select id="list" resultType="com.board.domain.BoardVO">
SELECT bno, title, content, writer,
regDate, viewCnt
FROM tbl_board
ORDER BY bno desc
</select>
๊ทธ๋ฆฌ๊ณ ๊ทธ ์๋์ ํ์ด์ง sql์ฟผ๋ฆฌ๋ฌธ ํ๋ ๋ ์ถ๊ฐํจ
<!-- ๊ฒ์๊ธ ๋ชฉ๋ก + ํ์ด์ง-->
<select id="listPage" parameterType="hashMap" resultType="com.board.domain.BoardVO">
SELECT bno, title, content, writer, regDate, viewCnt
FROM tbl_board
ORDER BY bno desc
limit #{displayPost}, #{postNum};
</select>
postNum = ํ ํ์ด์ง์ ๋ช ๊ฐ ํ์ํ ๊ฑด์ง
BoardDAO
//๊ฒ์๊ธ ๋ชฉ๋ก + ํ์ด์ง
public List<BoardVO> listPage(int displayPost, int postNum) throws Exception;
BoardDAOImple
//๊ฒ์๊ธ ๋ชฉ๋ก + ํ์ด์ง
@Override
public List<BoardVO> listPage(int displayPost, int postNum) throws Exception {
HashMap<String, Object> data = new HashMap<>();
data.put("displayPost", displayPost);
data.put("postNum", postNum);
return sqlTemplate.selectList("board.listPage",data );
}
BoardService
//๊ฒ์๊ธ ๋ชฉ๋ก + ํ์ด์ง
public List<BoardVO> listPage(int displayPost, int postNum) throws Exception;
BoardServiceImple
//๊ฒ์๊ธ ๋ชฉ๋ก + ํ์ด์ง
@Override
public List<BoardVO> listPage(int displayPost, int postNum) throws Exception {
return boardDAO.listPage(displayPost, postNum);
}
BoardController
// ๊ฒ์๋ฌผ ๋ชฉ๋ก + ํ์ด์ง ์ถ๊ฐ
@GetMapping("/listPage")
public void getListPage(@RequestParam("num") int num, Model model) throws Exception {
//๊ฒ์๋ฌผ ์ด ๊ฐฏ์
int count = boardService.count();
//ํ ํ์ด์ง์ ์ถ๋ ฅํ ๊ฒ์๊ธ ๊ฐฏ์
int postNum = 10;
//ํ๋จ ํ์ด์ง ๋ฒํธ([ ๊ฒ์๊ธ ์ด ๊ฐฏ์ / ํ ํ์ด์ง์ ์ถ๋ ฅํ ๊ฐฏ์])์ ๋ฐ์ฌ๋ฆผ)
int pageNum = (int)Math.ceil((double)count/postNum);
//์ถ๋ ฅํ ๊ฒ์๊ธ
int displayPost = (num - 1) * postNum;
List<BoardVO> list = null;
list = boardService.listPage(displayPost, postNum);
model.addAttribute("list", list); //ํ์ฌ num ํ์ด์ง์ ๊ฒ์๊ธ ๋ฐ์ดํฐ
model.addAttribute("pageNum", pageNum); //์ด ํ์ด์ง ์ซ์
}
๋งค๊ฐ๋ณ์๋ก num์ ํ์ด์ง ๋ฒํธ
1. ๊ฒ์๋ฌผ์ ์ด ๊ฐฏ์๋ฅผ ๊ตฌํ๊ณ
2. ํ ํ์ด์ง๋น ์ถ๋ ฅํ ๊ฒ์๋ฌผ ๊ฐฏ์๋ฅผ ์ ํ๊ณ (10๊ฐ)
3. ํ๋จ์ ํ์ํ ํ์ด์ง ๋ฒํธ์ ๊ฐฏ์๋ฅผ ๊ตฌํ๊ณ (์์์ ์ ์ฌ๋ฆผ)
4. ํ์ฌ ํ์ด์ง๋ฅผ ๊ธฐ์ค์ผ๋ก 10๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํฉ๋๋ค
ํ์ง๋ง ์ง๊ธ์ listPage์ ๋งค์นญ๋๋ jspํ์ผ์ด ์์ผ๋ ์์ฑํด์ผํฉ๋๋ค.
https://blog.naver.com/drv982/223219418341
[ํ์ด์ง] ๊ตฌํํ๊ธฐ
๊ฒ์๋ฌผ ์ด ๊ฐฏ์๋ฅผ ๊ตฌํ ์ฟผ๋ฆฌ๋ฅผ ๋งคํผ์ ์ถ๊ฐํฉ๋๋ค. DAO์ DAOImpl์ ์ฝ๋๋ฅผ ์ถ๊ฐํฉ๋๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก...
blog.naver.com
list.jsp๋ฅผ ๋ณต์ฌํด์ listPage.jsp๋ฅผ ์์ฑํ๋ค.

table์ด ๋๋๋ ๋ค์ ์ค์ ๋ฐ๋ณต๋ฌธ ์ถ๊ฐ- ํ์ด์ง๋ค์ด์
<div>
<c:forEach begin="1" end="${pageNum}" var="num">
<span>
<a href="/board/listPage?num=${num}">${num}</a>
</span>
</c:forEach>
</div>
๋ฐ๋ณต๋ฌธ
1๋ถํฐ pageNum๊น์ง ๋ฐ๋ณตํ๋ค.

<li>
<a href="/board/listPage?num=1">๊ธ ๋ชฉ๋ก(ํ์ด์ง)</a>
</li>
๋งํฌ๋ฅผ ํด๋ฆญํ๋ฉด 1ํ์ด์ง๋ถํฐ ํ์ํจ
home.jsp์๋ ์ถ๊ฐํ์
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
ํฌ๋ก์ฐ ์๋!
</h1>
<P> The time on the server is ${serverTime}. </P>
<p><a href="/board/listPage?num=1">๊ธ ๋ชฉ๋ก(ํ์ด์ง)</a></p>
<p><a href="/board/list">๊ฒ์๋ฌผ ๋ชฉ๋ก</a></p>
<p><a href="/board/write">๊ฒ์๋ฌผ ์์ฑ</a></p>
</body>
</html>
ํ ์คํธ


'BACKEND > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ๊ฒ์ ๊ธฐ๋ฅ (1) | 2023.10.24 |
|---|---|
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ํ์ด์ง ๊ธฐ๋ฅ ๊ตฌํ2 (0) | 2023.10.24 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ์กฐํ์ ์ฆ๊ฐ(UPDATE) ๊ธฐ๋ฅ ์ถ๊ฐ (0) | 2023.10.24 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ์ญ์ ํ๊ธฐ ๊ธฐ๋ฅ ์ถ๊ฐ (0) | 2023.10.24 |
| ๊ฒ์ํ ๋ง๋ค๊ธฐ - ๋ฉ๋ด ๋ชจ๋ํ include,๋ ์ง format (0) | 2023.10.23 |