PageHelper
PageHelper๋ MyBatis๋ฅผ ์ํ ๊ฐ๋ ฅํ ํ์ด์ง ํ๋ฌ๊ทธ์ธ์
๋๋ค.
์ด ๋๊ตฌ๋ ๋ณต์กํ ํ์ด์ง ๋ก์ง์ ๋จ์ํํ์ฌ ๊ฐ๋ฐ์์ ์์ฐ์ฑ์ ํฌ๊ฒ ํฅ์์ํต๋๋ค. PageHelper๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ฉ๋ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์์ผ๋ฉฐ, ๋ค์ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์คํ
๊ณผ ํธํ๋ฉ๋๋ค.
๋ฏธ๋ฆฌ๋ณด๊ธฐ
๊ฐ์
โข
PageHelper Gradle ์์กด์ฑ
โข
PageHelper Maven ์์กด์ฑ
โข
application.properties ํ๋ฌ๊ทธ์ธ ์ค์
โข
์์ ์ฝ๋
โฆ
board ํ
์ด๋ธ ์์ฑ
โฆ
Board ๋๋ฉ์ธ ๊ฐ์ฒด ์์ฑ
โฆ
BoardMapper.xml
โฆ
BoardMapper.java
โฆ
BoardService.java
โฆ
BoardServiceImpl.java
โฆ
BoardController.java
โฆ
board/list.html
PageHelper Gradle ์์กด์ฑ
build.gradle
implementation 'com.github.pagehelper:pagehelper-spring-boot-starter:2.1.0'
Java
๋ณต์ฌ
์์ ์ฝ๋๋ฅผ build.gradle ํ์ผ์ ์ถ๊ฐํ์ฌ PageHelper๋ฅผ ํ๋ก์ ํธ์ ํฌํจ์ํฌ ์ ์์ต๋๋ค. ๋ฒ์ ๋ฒํธ๋ ์ต์ ๋ฒ์ ์ ๋ฐ๋ผ ๋ณ๊ฒฝ๋ ์ ์์ผ๋ฏ๋ก, ํญ์ ์ต์ ๋ฒ์ ์ ํ์ธํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
PageHelper Maven ์์กด์ฑ
pom.xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
XML
๋ณต์ฌ
application.properties ํ๋ฌ๊ทธ์ธ ์ค์
application.properties
# PageHelper ์ค์
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
Markdown
๋ณต์ฌ
์ค์ ํค | ์ค๋ช
|
pagehelper.helperDialect=mysql | PageHelper๊ฐ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ค์ ํฉ๋๋ค.
์ฌ๊ธฐ์๋ MySQL์ ์ฌ์ฉํฉ๋๋ค.
- mysql, oracle, postgresql, ๋ฑ |
pagehelper.reasonable=true | ํ์ด์ง ๋ฒํธ์ ํ์ด์ง ํฌ๊ธฐ์ ๋ํด ํฉ๋ฆฌ์ ์ธ ๊ฐ์ ์ฌ์ฉํฉ๋๋ค. |
pagehelper.supportMethodsArguments=true | ๋ฉ์๋๋ก ์ ๋ฌ๋ ํ์ด์ง ๋ฒํธ์ ํ์ด์ง ํฌ๊ธฐ๋ฅผ ์ง์ํฉ๋๋ค. |
pagehelper.params=count=countSql | ํ์ด์ง๋ค์ด์
์ฟผ๋ฆฌ์์ ์ฌ์ฉ๋๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ค์ ํฉ๋๋ค.
์ฌ๊ธฐ์๋ ์ด ๋ ์ฝ๋ ์๋ฅผ ๊ณ์ฐํ๋ SQL ์ฟผ๋ฆฌ๋ฅผ ์ง์ ํฉ๋๋ค. |
1.
pagehelper.helperDialect=mysql:
โข
PageHelper๊ฐ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฐฉ์ธ(dialect)์ ์ค์ ํฉ๋๋ค. ์ฌ๊ธฐ์๋ MySQL์ ์ฌ์ฉํ๋๋ก ์ค์ ํฉ๋๋ค. PageHelper๋ ์ด ์ค์ ์ ํตํด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ง๋ ํ์ด์ง๋ค์ด์
์ฟผ๋ฆฌ๋ฅผ ์์ฑํฉ๋๋ค.
โข
์ฃผ์ ์ต์
๊ฐ
๊ฐ | ์ค๋ช
|
mysql | MySQL, MariaDB, Drizzle์ฉ |
mariadb | MariaDB ์ ์ฉ |
oracle | Oracle Database์ฉ |
oracle9i | Oracle 9i ๋ฐ ์ด์ ๋ฒ์ ์ฉ |
sqlserver | SQL Server 2012 ์ด์ |
sqlserver2012 | SQL Server 2012 ์ด์(ํนํ SQL) |
db2 | IBM DB2์ฉ |
h2 | H2 Database์ฉ |
postgresql | PostgreSQL์ฉ |
sqlite | SQLite์ฉ |
informix | Informix์ฉ |
clickhouse | ClickHouse์ฉ |
hsqldb | HSQLDB(HyperSQL Database)์ฉ |
phoenix | Apache Phoenix์ฉ |
sybase | Sybase์ฉ |
derby | Apache Derby์ฉ |
dm | DM(Database Management)์ฉ |
xcloud | XCloud์ฉ |
2.
pagehelper.reasonable=true:
โข
์ด ์ค์ ์ดย true๋ก ์ค์ ๋๋ฉด, ํ์ด์ง ๋ฒํธ์ ํ์ด์ง ํฌ๊ธฐ์ ๋ํด ํฉ๋ฆฌ์ ์ธ ๊ฐ์ ์ฌ์ฉํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ํ์ด์ง ๋ฒํธ๊ฐ 0 ์ดํ์ด๊ฑฐ๋ ๋๋ฌด ํฐ ๊ฒฝ์ฐ ์๋์ผ๋ก 1๋ก ์ค์ ๋๊ณ , ํ์ด์ง ํฌ๊ธฐ๊ฐ 0 ์ดํ์ด๊ฑฐ๋ ๋๋ฌด ํฐ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๋ฉ๋๋ค.
3.
pagehelper.supportMethodsArguments=true:
โข
์ด ์ค์ ์ดย true๋ก ์ค์ ๋๋ฉด, PageHelper๋ ๋ฉ์๋ ์ธ์๋ก ์ ๋ฌ๋ ํ์ด์ง ๋ฒํธ์ ํ์ด์ง ํฌ๊ธฐ๋ฅผ ์ง์ํฉ๋๋ค. ์ด๋ฅผ ํตํด ์ปจํธ๋กค๋ฌ ๋ฉ์๋์์ ์ง์ ํ์ด์ง ๋ฒํธ์ ํ์ด์ง ํฌ๊ธฐ๋ฅผ ์ ๋ฌํ ์ ์์ต๋๋ค.
4.
pagehelper.params=count=countSql:
โข
์ด ์ค์ ์ ํ์ด์ง๋ค์ด์
์ฟผ๋ฆฌ์์ ์ฌ์ฉ๋๋ ๋งค๊ฐ๋ณ์๋ฅผ ์ค์ ํฉ๋๋ค. ์ฌ๊ธฐ์๋ย countย ๋งค๊ฐ๋ณ์๋ฅผย countSql๋ก ์ค์ ํ์ฌ, PageHelper๊ฐ ์ด ๋ ์ฝ๋ ์๋ฅผ ๊ณ์ฐํ ๋ ์ฌ์ฉํ SQL ์ฟผ๋ฆฌ๋ฅผ ์ง์ ํฉ๋๋ค.
์์ ์ฝ๋
โข
board ํ
์ด๋ธ ์์ฑ
โข
Board ๋๋ฉ์ธ ๊ฐ์ฒด ์์ฑ
โข
BoardMapper.xml
โข
BoardMapper.java
โข
BoardService.java
โข
BoardServiceImpl.java
โข
BoardController.java
โข
board/list.html
board ํ ์ด๋ธ ์์ฑ
DROP TABLE IF EXISTS `board`;
CREATE TABLE `board` (
`no` int NOT NULL AUTO_INCREMENT,
`id` varchar(255) NOT NULL,
`title` varchar(100) NOT NULL,
`writer` varchar(100) NOT NULL,
`content` text,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`no`)
) COMMENT='๊ฒ์ํ';
SQL
๋ณต์ฌ
Board ๋๋ฉ์ธ ๊ฐ์ฒด ์์ฑ
/**
* Board
* - ๊ฒ์๊ธ ์ ๋ณด
*/
@Data
public class Board {
private int no;
private String id;
private String title;
private String writer;
private String content;
private Date createdAt;
private Date updatedAt;
}
Java
๋ณต์ฌ
BoardMapper.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">
<!-- namespace="๋งคํผ ์ธํฐํ์ด์ค ๊ฒฝ๋ก" -->
<mapper namespace="com.aloha.pagehelper.mapper.BoardMapper">
<!-- ๊ฒ์๊ธ ๋ชฉ๋ก -->
<!-- <ํ๊ทธ id="๋ฉ์๋๋ช
"></ํ๊ทธ> -->
<!--
<select id="๋ฉ์๋๋ช
" resultType="ํจํค์ง๋ช
.๊ฐ์ฒด๋ช
" ></select>
resultType : ์กฐํ ๊ฒฐ๊ณผ๋ฅผ ๋งคํํ ๊ฐ์ฒด ์ ๋ณด
-->
<!-- <select id="list" resultType="com.aloha.pagehelper.domain.Board"> -->
<select id="list" resultType="Board">
SELECT *
FROM board
ORDER BY created_at DESC
</select>
<!-- ๊ฒ์๊ธ ์กฐํ -->
<!--
SQL ์ ์ง์ ํ ํ๋ผ๋ฏธํฐ : #{๋ณ์๋ช
}
* ๋์ด์ค๋ ๋งค๊ฐ๋ณ์๊ฐ 1๊ฐ์ด๋ฉด, ๋ณ์๋ช
์ด ์ผ์นํ์ง ์์๋ ๋งคํ
* ๊ฐ์ฒด โก ๊ฐ์ฒด์ ๋ณ์๋ช
๊ณผ ์ผ์นํ๋ ํ๋ผ๋ฏธํฐ ๋ช
์ผ๋ก ์์ฑ
-->
<select id="select" resultType="Board">
SELECT *
FROM board
WHERE no = #{no}
</select>
<!-- ๊ฒ์๊ธ ๋ฑ๋ก -->
<insert id="insert">
INSERT INTO board( title, writer, content )
VALUES ( #{title}, #{writer}, #{content} )
</insert>
<!-- ๊ฒ์๊ธ ์์ -->
<update id="update">
UPDATE board
SET title = #{title}
,writer = #{writer}
,content = #{content}
WHERE no = #{no}
</update>
<!-- ๊ฒ์๊ธ ์ญ์ -->
<delete id="delete">
DELETE FROM board
WHERE no = #{no}
</delete>
</mapper>
XML
๋ณต์ฌ
BoardMapper.java
@Mapper
public interface BoardMapper {
// ๊ฒ์๊ธ ๋ชฉ๋ก
public List<Board> list() throws Exception;
// ๊ฒ์๊ธ ์กฐํ
public Board select(int no) throws Exception;
// ๊ฒ์๊ธ ๋ฑ๋ก
public int insert(Board board) throws Exception;
// ๊ฒ์๊ธ ์์
public int update(Board board) throws Exception;
// ๊ฒ์๊ธ ์ญ์
public int delete(int no) throws Exception;
}
Java
๋ณต์ฌ
BoardService.java
public interface BoardService {
// โญ ๊ฒ์๊ธ ํ์ด์ง ๋ชฉ๋ก
public PageInfo<Board> list(int page, int size) throws Exception;
// ๊ฒ์๊ธ ๋ชฉ๋ก
public List<Board> list() throws Exception;
// ๊ฒ์๊ธ ์กฐํ
public Board select(int no) throws Exception;
// ๊ฒ์๊ธ ๋ฑ๋ก
public int insert(Board board) throws Exception;
// ๊ฒ์๊ธ ์์
public int update(Board board) throws Exception;
// ๊ฒ์๊ธ ์ญ์
public int delete(int no) throws Exception;
}
Java
๋ณต์ฌ
BoardServiceImpl.java
@Service // ์๋น์ค ์ญํ ์ ์คํ๋ง ๋น
public class BoardServiceImpl implements BoardService {
@Autowired
private BoardMapper boardMapper;
/**
* โญ ๊ฒ์๊ธ ํ์ด์ง ๋ชฉ๋ก ์กฐํ
*/
@Override
public PageInfo<Board> list(int page, int size) throws Exception {
// โญ PageHelper.startPage(ํ์ฌ ํ์ด์ง, ํ์ด์ง๋น ๊ฒ์๊ธ ์);
PageHelper.startPage(page, size);
List<Board> boardList = boardMapper.list();
// โญ PageInfo<Board>( ๋ฆฌ์คํธ, ๋
ธ์ถ ํ์ด์ง ๊ฐ์ );
PageInfo<Board> pageInfo = new PageInfo<Board>(boardList, 10);
return pageInfo;
}
/**
* ๊ฒ์๊ธ ๋ชฉ๋ก ์กฐํ
*/
@Override
public List<Board> list() throws Exception {
// TODO : boardMapper ๋ก list() ํธ์ถ
/*
* โก List<Board> boardList ๋ก ๋ฐ์์ด
* โก return boardList
*/
List<Board> boardList = boardMapper.list();
return boardList;
}
/**
* ๊ฒ์๊ธ ์กฐํ
* - no ๋งค๊ฐ๋ณ์๋ก ๊ฒ์๊ธ ๋ฒํธ๋ฅผ ์ ๋ฌ๋ฐ์์
* ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์กฐํ ์์ฒญ
*/
@Override
public Board select(int no) throws Exception {
// TODO : boardMapper ๋ก select(no) ํธ์ถ
/*
* โก Board board ๋ก ๋ฐ์์ด
* โก return board
*/
Board board = boardMapper.select(no);
return board;
}
/**
* ๊ฒ์๊ธ ๋ฑ๋ก
*/
@Override
public int insert(Board board) throws Exception {
// TODO : boardMapper ๋ก insert(Board) ํธ์ถ
/*
* โก int result ๋ก ๋ฐ์ดํฐ ์ฒ๋ฆฌ ํ(๊ฐ์) ๋ฐ์์ด
* โก return result
*/
int result = boardMapper.insert(board);
return result;
}
/**
* ๊ฒ์๊ธ ์์
*/
@Override
public int update(Board board) throws Exception {
// TODO : boardMapper ๋ก update(Board) ํธ์ถ
/*
* โก int result ๋ก ๋ฐ์ดํฐ ์ฒ๋ฆฌ ํ(๊ฐ์) ๋ฐ์์ด
* โก return result
*/
int result = boardMapper.update(board);
return result;
}
/**
* ๊ฒ์๊ธ ์ญ์
*/
@Override
public int delete(int no) throws Exception {
// TODO : boardMapper ๋ก delete(no) ํธ์ถ
/*
* โก int result ๋ก ๋ฐ์ดํฐ ์ฒ๋ฆฌ ํ(๊ฐ์) ๋ฐ์์ด
* โก return result
*/
int result = boardMapper.delete(no);
return result;
}
}
Java
๋ณต์ฌ
BoardController.java
/**
* /board ๊ฒฝ๋ก๋ก ์์ฒญ ์์ ๋ ์ฒ๋ฆฌ
* [GET] - /board/list : ๊ฒ์๊ธ ๋ชฉ๋ก ํ๋ฉด
* [GET] - /board/read : ๊ฒ์๊ธ ์กฐํ ํ๋ฉด
* [GET] - /board/insert : ๊ฒ์๊ธ ๋ฑ๋ก ํ๋ฉด
* [POST] - /board/insert : ๊ฒ์๊ธ ๋ฑ๋ก ์ฒ๋ฆฌ
* [GET] - /board/update : ๊ฒ์๊ธ ์์ ํ๋ฉด
* [POST] - /board/update : ๊ฒ์๊ธ ์์ ์ฒ๋ฆฌ
* [POST] - /board/delete : ๊ฒ์๊ธ ์ญ์ ์ฒ๋ฆฌ
*/
@Slf4j // ๋ก๊ทธ ์ด๋
ธํ
์ด์
@Controller // ์ปจํธ๋กค๋ฌ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก
@RequestMapping("/board") // ํด๋ ์ค ๋ ๋ฒจ ์์ฒญ ๊ฒฝ๋ก ๋งคํ
// - /board/~ ๊ฒฝ๋ก์ ์์ฒญ์ ์ด ์ปจํธ๋กค๋ฌ์์ ์ฒ๋ฆฌ
public class BoardController {
// โญ๋ฐ์ดํฐ ์์ฒญ๊ณผ ํ๋ฉด ์ถ๋ ฅ
// Controller --> Service (๋ฐ์ดํฐ ์์ฒญ)
// Controller <-- Service (๋ฐ์ดํฐ ์ ๋ฌ)
// Controller --> Model (๋ชจ๋ธ ๋ฑ๋ก)
// View <-- Model (๋ฐ์ดํฐ ์ถ๋ ฅ)
@Autowired // ์์กด์ฑ ์๋ ์ฃผ์
private BoardService boardService; // @Service๋ฅผ --Impl ์ ๋ฑ๋ก
/**
* ๊ฒ์๊ธ ๋ชฉ๋ก ์กฐํ ํ๋ฉด
* @return
* @throws Exception
*/
@GetMapping("/list")
public String list(Model model
,@RequestParam(name = "page", required = false, defaultValue = "1") Integer page
,@RequestParam(name = "size", required = false, defaultValue = "10") Integer size) throws Exception {
// ๋ฐ์ดํฐ ์์ฒญ
PageInfo<Board> pageInfo = boardService.list(page, size);
Pagination pagination = new Pagination();
pagination.setPage(page);
pagination.setSize(size);
pagination.setTotal( pageInfo.getTotal() );
// ๋ชจ๋ธ ๋ฑ๋ก
model.addAttribute("pageInfo", pageInfo);
model.addAttribute("pagination", pagination);
// ๋ทฐ ํ์ด์ง ์ง์
return "/board/list"; // resources/templates/board/list.html
}
/**
* ๊ฒ์๊ธ ์กฐํ ํ๋ฉด
* - /board/read?no=๐
* @param no
* @return
* @throws Exception
*/
// @RequestParam("ํ๋ผ๋ฏธํฐ๋ช
")
// - ์คํ๋ง ๋ถํธ 3.2๋ฒ์ ์ดํ, ์๋ตํด๋ ์๋ ๋งคํ๋๋ค.
// - ์คํ๋ง ๋ถํธ 3.2๋ฒ์ ์ด์, ํ์๋ก ๋ช
์ํด์ผ ๋งคํ๋๋ค.
@GetMapping("/read")
public String read(@RequestParam("no") int no, Model model) throws Exception {
// ๋ฐ์ดํฐ ์์ฒญ
Board board = boardService.select(no);
// ๋ชจ๋ธ ๋ฑ๋ก
model.addAttribute("board", board);
// ๋ทฐํ์ด์ง ์ง์
return "/board/read";
}
/**
* ๊ฒ์๊ธ ๋ฑ๋ก ํ๋ฉด
* @return
*/
@GetMapping("/insert")
public String insert() {
return "/board/insert";
}
/**
* ๊ฒ์๊ธ ๋ฑ๋ก ์ฒ๋ฆฌ
* @param board
* @return
* @throws Exception
*/
@PostMapping("/insert")
public String insertPro(Board board) throws Exception {
// ๋ฐ์ดํฐ ์์ฒญ
int result = boardService.insert(board);
// ๋ฆฌ๋ค์ด๋ ํธ
// โญ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ฑ๊ณต
if( result > 0 ) {
return "redirect:/board/list";
}
// โ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์คํจ
return "redirect:/board/insert?error";
}
/**
* ๊ฒ์๊ธ ์์ ํ๋ฉด
* @param no
* @param model
* @return
* @throws Exception
*/
@GetMapping("/update")
public String update(@RequestParam("no") int no, Model model) throws Exception {
Board board = boardService.select(no);
model.addAttribute("board", board);
return "/board/update";
}
/**
* ๊ฒ์๊ธ ์์ ์ฒ๋ฆฌ
* @param board
* @return
* @throws Exception
*/
@PostMapping("/update")
public String updatePro(Board board) throws Exception {
int result = boardService.update(board);
if( result > 0 ) {
return "redirect:/board/list";
}
int no = board.getNo();
return "redirect:/board/update?no="+ no + "&error";
}
/**
* ๊ฒ์๊ธ ์ญ์ ์ฒ๋ฆฌ
* @param no
* @return
* @throws Exception
*/
@PostMapping("/delete")
public String delete(@RequestParam("no") int no) throws Exception {
int result = boardService.delete(no);
if( result > 0 ) {
return "redirect:/board/list";
}
return "redirect:/board/update?no=" + no + "&error";
}
}
Java
๋ณต์ฌ
board/list.html
โข
๋ชฉ๋ก
โข
ํ์ด์ง๋ค์ด์
โข
์ ์ฒด์ฝ๋ list.html
๋ชฉ๋ก
<h1>๊ฒ์๊ธ ๋ชฉ๋ก</h1>
<a href="/board/insert">๊ธ์ฐ๊ธฐ</a>
<table border="1">
<tr>
<th width="50">๋ฒํธ</th>
<th width="300">์ ๋ชฉ</th>
<th width="100">์์ฑ์</th>
<th width="200">๋ฑ๋ก์ผ์</th>
<th width="200">์์ ์ผ์</th>
</tr>
<th:block th:each="board : ${pageInfo.list}">
<tr>
<td align="center" th:text="${board.no}"></td>
<td>
<!-- th:???="|๋ฌธ์์ด+${ํํ์}|" -->
<a th:href="|/board/read?no=${board.no}|"
th:text="${board.title}"></a>
</td>
<td align="center" th:text="${board.writer}"></td>
<!--
์ ํธ๋ฆฌํฐ๊ฐ์ฒด.๋ฉ์๋()
#dates.format( ๋ ์ง๊ฐ์ฒด๋ช
, '๋ ์งํฌ๋งท' )
- #dates.format( board.regDate, 'yyyy-MM-dd HH:mm:ss' )
-->
<td align="center">
<span th:text="${ #dates.format( board.createdAt, 'yyyy-MM-dd HH:mm:ss' ) }"></span>
</td>
<td align="center">
<span th:text="${ #dates.format( board.updatedAt, 'yyyy-MM-dd HH:mm:ss' ) }"></span>
</td>
</tr>
</th:block>
</table>
HTML
๋ณต์ฌ
ํ์ด์ง๋ค์ด์
ํ์ด์ง๋ค์ด์
์ ๋๋์ ๋ฐ์ดํฐ๋ฅผ ์ฌ๋ฌ ํ์ด์ง๋ก ๋๋์ด ํ์ํ๋ ๊ธฐ๋ฒ์ผ๋ก,
์ฌ์ฉ์ ๊ฒฝํ์ ๊ฐ์ ํ๊ณ ์๋ฒ ๋ถํ๋ฅผ ์ค์ด๋ ๋ฐ ๋์์ ์ค๋๋ค.
โข
์ ๋์ ์ธ ํ์ด์ง ๋ฒํธ ์ถ๋ ฅ (pagehelper)
โข
10(count)๊ฐ์ฉ ๋์ด์ ๊ณ ์ ์ถ๋ ฅ (pagination ์ง์ ๊ตฌํ)
โฆ
1~10, 11~20, โฆ
์ ๋์ ์ธ ํ์ด์ง ๋ฒํธ ์ถ๋ ฅ (pagehelper)
<!-- ํ์ด์ง๋ค์ด์
-->
<!-- โก ์ ๋์ ์ธ ํ์ด์ง ๋ฒํธ ์ถ๋ ฅ (pagehelper) -->
<th:block th:if="${pageInfo.pages > 0}">
<div class="pagination flex justify-content-center">
<ul style="display: flex; list-style-type: none; gap: 10px;">
<li th:if="${pageInfo.pageNum > 1}">
<a th:href="@{/board/list(page=1, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16" fill="currentColor" transform="scale(-1, 1)">
<path d="M224 239L29.66 44.66c-9.37-9.37-24.57-9.37-33.94 0L-26.34 67c-9.37 9.37-9.37 24.57 0 33.94L128.97 256-26.34 410.05c-9.37 9.37-9.37 24.57 0 33.94l22.34 22.34c9.37 9.37 24.57 9.37 33.94 0L224 273c9.37-9.37 9.37-24.57 0-33.94zM448 273L253.66 467.34c-9.37 9.37-24.57 9.37-33.94 0L197.34 445c-9.37-9.37-9.37-24.57 0-33.94L352.97 256 197.34 100.95c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L448 239c9.37 9.37 9.37 24.57 0 33.94z"/>
</svg>
</a>
</li>
<li th:if="${pageInfo.hasPreviousPage}">
<a th:href="@{/board/list(page=${pageInfo.prePage}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="16" height="16" fill="currentColor"><path d="M34.52 239l194.34-194.34c9.37-9.37 24.57-9.37 33.94 0l22.34 22.34c9.37 9.37 9.37 24.57 0 33.94L128.97 256l156.17 156.17c9.37 9.37 9.37 24.57 0 33.94l-22.34 22.34c-9.37 9.37-24.57 9.37-33.94 0L34.52 273c-9.37-9.37-9.37-24.57 0-33.94z"/></svg>
</a>
</li>
<li th:each="i : ${#numbers.sequence(pageInfo.navigateFirstPage, pageInfo.navigateLastPage)}">
<a th:href="@{/board/list(page=${i}, size=${pageInfo.pageSize})}"
th:text="${i}"
class="page"
th:classappend="${pageInfo.pageNum == i} ? 'active' : ''"></a>
</li>
<li th:if="${pageInfo.hasNextPage}">
<a th:href="@{/board/list(page=${pageInfo.nextPage}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="16" height="16" fill="currentColor"><path d="M285.48 273l-194.34 194.34c-9.37 9.37-24.57 9.37-33.94 0l-22.34-22.34c-9.37-9.37-9.37-24.57 0-33.94L191.03 256 34.86 99.83c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L285.48 239c9.37 9.37 9.37 24.57 0 33.94z"/></svg>
</a>
</li>
<li th:if="${pageInfo.pageNum < pageInfo.pages}">
<a th:href="@{/board/list(page=${pageInfo.pages}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16" fill="currentColor">
<path d="M224 239L29.66 44.66c-9.37-9.37-24.57-9.37-33.94 0L-26.34 67c-9.37 9.37-9.37 24.57 0 33.94L128.97 256-26.34 410.05c-9.37 9.37-9.37 24.57 0 33.94l22.34 22.34c9.37 9.37 24.57 9.37 33.94 0L224 273c9.37-9.37 9.37-24.57 0-33.94zM448 273L253.66 467.34c-9.37 9.37-24.57 9.37-33.94 0L197.34 445c-9.37-9.37-9.37-24.57 0-33.94L352.97 256 197.34 100.95c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L448 239c9.37 9.37 9.37 24.57 0 33.94z"/>
</svg>
</a>
</li>
</ul>
</div>
</th:block>
HTML
๋ณต์ฌ
10(count)๊ฐ์ฉ ๋์ด์ ๊ณ ์ ์ถ๋ ฅ (pagination ์ง์ ๊ตฌํ)
<!-- ํ์ด์ง๋ค์ด์
-->
<!-- โก 10(count)๊ฐ์ฉ ๋์ด์ ๊ณ ์ -->
<th:block th:if="${pageInfo.pages > 0}">
<div class="pagination flex justify-content-center">
<ul style="display: flex; list-style-type: none; gap: 10px;">
<li th:if="${pageInfo.pageNum > 1}">
<a th:href="@{/board/list(page=1, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16" fill="currentColor" transform="scale(-1, 1)">
<path d="M224 239L29.66 44.66c-9.37-9.37-24.57-9.37-33.94 0L-26.34 67c-9.37 9.37-9.37 24.57 0 33.94L128.97 256-26.34 410.05c-9.37 9.37-9.37 24.57 0 33.94l22.34 22.34c9.37 9.37 24.57 9.37 33.94 0L224 273c9.37-9.37 9.37-24.57 0-33.94zM448 273L253.66 467.34c-9.37 9.37-24.57 9.37-33.94 0L197.34 445c-9.37-9.37-9.37-24.57 0-33.94L352.97 256 197.34 100.95c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L448 239c9.37 9.37 9.37 24.57 0 33.94z"/>
</svg>
</a>
</li>
<li th:if="${pageInfo.hasPreviousPage}">
<a th:href="@{/board/list(page=${pageInfo.prePage}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="16" height="16" fill="currentColor"><path d="M34.52 239l194.34-194.34c9.37-9.37 24.57-9.37 33.94 0l22.34 22.34c9.37 9.37 9.37 24.57 0 33.94L128.97 256l156.17 156.17c9.37 9.37 9.37 24.57 0 33.94l-22.34 22.34c-9.37 9.37-24.57 9.37-33.94 0L34.52 273c-9.37-9.37-9.37-24.57 0-33.94z"/></svg>
</a>
</li>
<li th:each="i : ${#numbers.sequence(pagination.start, pagination.end)}">
<a th:href="@{/board/list(page=${i}, size=${pageInfo.pageSize})}"
th:text="${i}"
class="page"
th:classappend="${pageInfo.pageNum == i} ? 'active' : ''"></a>
</li>
<li th:if="${pageInfo.hasNextPage}">
<a th:href="@{/board/list(page=${pageInfo.nextPage}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="16" height="16" fill="currentColor"><path d="M285.48 273l-194.34 194.34c-9.37 9.37-24.57 9.37-33.94 0l-22.34-22.34c-9.37-9.37-9.37-24.57 0-33.94L191.03 256 34.86 99.83c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L285.48 239c9.37 9.37 9.37 24.57 0 33.94z"/></svg>
</a>
</li>
<li th:if="${pageInfo.pageNum < pageInfo.pages}">
<a th:href="@{/board/list(page=${pageInfo.pages}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16" fill="currentColor">
<path d="M224 239L29.66 44.66c-9.37-9.37-24.57-9.37-33.94 0L-26.34 67c-9.37 9.37-9.37 24.57 0 33.94L128.97 256-26.34 410.05c-9.37 9.37-9.37 24.57 0 33.94l22.34 22.34c9.37 9.37 24.57 9.37 33.94 0L224 273c9.37-9.37 9.37-24.57 0-33.94zM448 273L253.66 467.34c-9.37 9.37-24.57 9.37-33.94 0L197.34 445c-9.37-9.37-9.37-24.57 0-33.94L352.97 256 197.34 100.95c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L448 239c9.37 9.37 9.37 24.57 0 33.94z"/>
</svg>
</a>
</li>
</ul>
</div>
</th:block>
HTML
๋ณต์ฌ
์ ์ฒด์ฝ๋ list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>๊ฒ์๊ธ ๋ชฉ๋ก</title>
<style>
.page {
padding: 5px 10px;
border: 1px solid #ccc;
cursor: pointer;
text-decoration: none;
}
.page.active {
font-weight: bold;
}
</style>
</head>
<body>
<h1>๊ฒ์๊ธ ๋ชฉ๋ก</h1>
<a href="/board/insert">๊ธ์ฐ๊ธฐ</a>
<table border="1">
<tr>
<th width="50">๋ฒํธ</th>
<th width="300">์ ๋ชฉ</th>
<th width="100">์์ฑ์</th>
<th width="200">๋ฑ๋ก์ผ์</th>
<th width="200">์์ ์ผ์</th>
</tr>
<th:block th:each="board : ${pageInfo.list}">
<tr>
<td align="center" th:text="${board.no}"></td>
<td>
<!-- th:???="|๋ฌธ์์ด+${ํํ์}|" -->
<a th:href="|/board/read?no=${board.no}|"
th:text="${board.title}"></a>
</td>
<td align="center" th:text="${board.writer}"></td>
<!--
์ ํธ๋ฆฌํฐ๊ฐ์ฒด.๋ฉ์๋()
#dates.format( ๋ ์ง๊ฐ์ฒด๋ช
, '๋ ์งํฌ๋งท' )
- #dates.format( board.regDate, 'yyyy-MM-dd HH:mm:ss' )
-->
<td align="center">
<span th:text="${ #dates.format( board.createdAt, 'yyyy-MM-dd HH:mm:ss' ) }"></span>
</td>
<td align="center">
<span th:text="${ #dates.format( board.updatedAt, 'yyyy-MM-dd HH:mm:ss' ) }"></span>
</td>
</tr>
</th:block>
</table>
<!-- ํ์ด์ง๋ค์ด์
-->
<!-- โก ์ ๋์ ์ธ ํ์ด์ง ๋ฒํธ ์ถ๋ ฅ (pagehelper) -->
<th:block th:if="${pageInfo.pages > 0}">
<div class="pagination flex justify-content-center">
<ul style="display: flex; list-style-type: none; gap: 10px;">
<li th:if="${pageInfo.pageNum > 1}">
<a th:href="@{/board/list(page=1, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16" fill="currentColor" transform="scale(-1, 1)">
<path d="M224 239L29.66 44.66c-9.37-9.37-24.57-9.37-33.94 0L-26.34 67c-9.37 9.37-9.37 24.57 0 33.94L128.97 256-26.34 410.05c-9.37 9.37-9.37 24.57 0 33.94l22.34 22.34c9.37 9.37 24.57 9.37 33.94 0L224 273c9.37-9.37 9.37-24.57 0-33.94zM448 273L253.66 467.34c-9.37 9.37-24.57 9.37-33.94 0L197.34 445c-9.37-9.37-9.37-24.57 0-33.94L352.97 256 197.34 100.95c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L448 239c9.37 9.37 9.37 24.57 0 33.94z"/>
</svg>
</a>
</li>
<li th:if="${pageInfo.hasPreviousPage}">
<a th:href="@{/board/list(page=${pageInfo.prePage}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="16" height="16" fill="currentColor"><path d="M34.52 239l194.34-194.34c9.37-9.37 24.57-9.37 33.94 0l22.34 22.34c9.37 9.37 9.37 24.57 0 33.94L128.97 256l156.17 156.17c9.37 9.37 9.37 24.57 0 33.94l-22.34 22.34c-9.37 9.37-24.57 9.37-33.94 0L34.52 273c-9.37-9.37-9.37-24.57 0-33.94z"/></svg>
</a>
</li>
<li th:each="i : ${#numbers.sequence(pageInfo.navigateFirstPage, pageInfo.navigateLastPage)}">
<a th:href="@{/board/list(page=${i}, size=${pageInfo.pageSize})}"
th:text="${i}"
class="page"
th:classappend="${pageInfo.pageNum == i} ? 'active' : ''"></a>
</li>
<li th:if="${pageInfo.hasNextPage}">
<a th:href="@{/board/list(page=${pageInfo.nextPage}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="16" height="16" fill="currentColor"><path d="M285.48 273l-194.34 194.34c-9.37 9.37-24.57 9.37-33.94 0l-22.34-22.34c-9.37-9.37-9.37-24.57 0-33.94L191.03 256 34.86 99.83c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L285.48 239c9.37 9.37 9.37 24.57 0 33.94z"/></svg>
</a>
</li>
<li th:if="${pageInfo.pageNum < pageInfo.pages}">
<a th:href="@{/board/list(page=${pageInfo.pages}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16" fill="currentColor">
<path d="M224 239L29.66 44.66c-9.37-9.37-24.57-9.37-33.94 0L-26.34 67c-9.37 9.37-9.37 24.57 0 33.94L128.97 256-26.34 410.05c-9.37 9.37-9.37 24.57 0 33.94l22.34 22.34c9.37 9.37 24.57 9.37 33.94 0L224 273c9.37-9.37 9.37-24.57 0-33.94zM448 273L253.66 467.34c-9.37 9.37-24.57 9.37-33.94 0L197.34 445c-9.37-9.37-9.37-24.57 0-33.94L352.97 256 197.34 100.95c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L448 239c9.37 9.37 9.37 24.57 0 33.94z"/>
</svg>
</a>
</li>
</ul>
</div>
</th:block>
<!-- ํ์ด์ง๋ค์ด์
-->
<!-- โก 10(count)๊ฐ์ฉ ๋์ด์ ๊ณ ์ -->
<th:block th:if="${pageInfo.pages > 0}">
<div class="pagination flex justify-content-center">
<ul style="display: flex; list-style-type: none; gap: 10px;">
<li th:if="${pageInfo.pageNum > 1}">
<a th:href="@{/board/list(page=1, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16" fill="currentColor" transform="scale(-1, 1)">
<path d="M224 239L29.66 44.66c-9.37-9.37-24.57-9.37-33.94 0L-26.34 67c-9.37 9.37-9.37 24.57 0 33.94L128.97 256-26.34 410.05c-9.37 9.37-9.37 24.57 0 33.94l22.34 22.34c9.37 9.37 24.57 9.37 33.94 0L224 273c9.37-9.37 9.37-24.57 0-33.94zM448 273L253.66 467.34c-9.37 9.37-24.57 9.37-33.94 0L197.34 445c-9.37-9.37-9.37-24.57 0-33.94L352.97 256 197.34 100.95c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L448 239c9.37 9.37 9.37 24.57 0 33.94z"/>
</svg>
</a>
</li>
<li th:if="${pageInfo.hasPreviousPage}">
<a th:href="@{/board/list(page=${pageInfo.prePage}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="16" height="16" fill="currentColor"><path d="M34.52 239l194.34-194.34c9.37-9.37 24.57-9.37 33.94 0l22.34 22.34c9.37 9.37 9.37 24.57 0 33.94L128.97 256l156.17 156.17c9.37 9.37 9.37 24.57 0 33.94l-22.34 22.34c-9.37 9.37-24.57 9.37-33.94 0L34.52 273c-9.37-9.37-9.37-24.57 0-33.94z"/></svg>
</a>
</li>
<li th:each="i : ${#numbers.sequence(pagination.start, pagination.end)}">
<a th:href="@{/board/list(page=${i}, size=${pageInfo.pageSize})}"
th:text="${i}"
class="page"
th:classappend="${pageInfo.pageNum == i} ? 'active' : ''"></a>
</li>
<li th:if="${pageInfo.hasNextPage}">
<a th:href="@{/board/list(page=${pageInfo.nextPage}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="16" height="16" fill="currentColor"><path d="M285.48 273l-194.34 194.34c-9.37 9.37-24.57 9.37-33.94 0l-22.34-22.34c-9.37-9.37-9.37-24.57 0-33.94L191.03 256 34.86 99.83c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L285.48 239c9.37 9.37 9.37 24.57 0 33.94z"/></svg>
</a>
</li>
<li th:if="${pageInfo.pageNum < pageInfo.pages}">
<a th:href="@{/board/list(page=${pageInfo.pages}, size=${pageInfo.pageSize})}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" width="16" height="16" fill="currentColor">
<path d="M224 239L29.66 44.66c-9.37-9.37-24.57-9.37-33.94 0L-26.34 67c-9.37 9.37-9.37 24.57 0 33.94L128.97 256-26.34 410.05c-9.37 9.37-9.37 24.57 0 33.94l22.34 22.34c9.37 9.37 24.57 9.37 33.94 0L224 273c9.37-9.37 9.37-24.57 0-33.94zM448 273L253.66 467.34c-9.37 9.37-24.57 9.37-33.94 0L197.34 445c-9.37-9.37-9.37-24.57 0-33.94L352.97 256 197.34 100.95c-9.37-9.37-9.37-24.57 0-33.94l22.34-22.34c9.37-9.37 24.57-9.37 33.94 0L448 239c9.37 9.37 9.37 24.57 0 33.94z"/>
</svg>
</a>
</li>
</ul>
</div>
</th:block>
</body>
</html>
HTML
๋ณต์ฌ