- JSP 작성
<%@ taglib prefix="c" uri="<http://java.sun.com/jsp/jstl/core>" %>
<html>
<body>
<div>
<c:forEach var="entity" items="${entities}">
<!-- Display entity properties -->
</c:forEach>
</div>
<div>
<!-- Previous Page -->
<c:if test="${pagination.currentPage != 1}">
<a href="<c:url value='/yourMapping?page=${pagination.currentPage - 1}'/>">Previous</a>
</c:if>
<!-- Page Numbers -->
<c:forEach begin="1" end="${pagination.totalPage}" var="page">
<a href="<c:url value='/yourMapping?page=${page}'/>">${page}</a>
</c:forEach>
<!-- Next Page -->
<c:if test="${pagination.currentPage != pagination.totalPage}">
<a href="<c:url value='/yourMapping?page=${pagination.currentPage + 1}'/>">Next</a>
</c:if>
</div>
</body>
</html>
- 페이징 전용 클래스 작성
public class Pagination {
private int pageSize;
private int currentPage;
private long totalCount;
// 예외처리 실시 : 적합하지 않은 값이 들어왔을 때 예외를 던지도록 하였다.
public Pagination(int pageSize, int currentPage, long totalCount) {
if (pageSize <= 0 || currentPage <= 0 || totalCount < 0) {
throw new IllegalArgumentException("Invalid pagination parameters.");
}
this.pageSize = pageSize;
this.currentPage = currentPage;
this.totalCount = totalCount;
}
public int getPageSize() {
return pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public long getTotalCount() {
return totalCount;
}
public long getTotalPage() {
return (totalCount - 1) / pageSize + 1;
}
public int getOffset() {
return (currentPage - 1) * pageSize;
}
public int getLimit() {
return pageSize;
}
}
- 컨트롤러 작성
@RestController
@RequestMapping("/yourMapping")
public class YourController {
private final YourService yourService;
private static final int PAGE_SIZE = 10; // 페이지 크기 설정
// YourService를 주입받는 생성자
public YourController(YourService yourService) {
this.yourService = yourService;
}
@GetMapping
public String yourMethod(@RequestParam(value = "page", defaultValue = "1") int page, Model model) {
try {
// 전체 레코드 개수를 받아옵니다.
long totalCount = yourService.selectTotalCount();
// 페이지 정보를 가진 Pagination 객체를 생성합니다.
Pagination pagination = new Pagination(PAGE_SIZE, page, totalCount);
// Pagination 객체를 이용해 해당 페이지의 데이터를 가져옵니다.
List<YourEntity> entities = yourService.getEntitiesForPage(pagination);
// 모델에 데이터 리스트와 페이지 정보를 추가합니다.
model.addAttribute("entities", entities);
model.addAttribute("pagination", pagination);
} catch (IllegalArgumentException ex) {
// 잘못된 페이지 번호를 받았을 경우, 에러 메시지를 모델에 추가합니다.
model.addAttribute("errorMessage", "Invalid page number: " + page);
}
// 결과를 표시할 JSP 페이지의 이름을 리턴합니다.
return "yourJSPPage";
}
}
- 서비스 작성
@Service
public class YourService {
private final YourMapper yourMapper;
public YourService(YourMapper yourMapper) {
this.yourMapper = yourMapper;
}
public List<YourEntity> getEntitiesForPage(Pagination pagination) {
return yourMapper.selectWithPagination(pagination.getOffset(), pagination.getLimit());
}
public long selectTotalCount() {
return yourMapper.selectTotalCount();
}
}
- Mapper 작성
public interface YourMapper {
// 페이징 처리를 위한 메소드 추가
List<YourEntity> selectWithPagination(@Param("offset") int offset, @Param("limit") int limit);
// 전체 카운트 조회 메소드
long selectTotalCount();
}
- MyBatis xml작성
<mapper namespace="com.example.YourMapper">
<select id="selectWithPagination" resultType="com.example.YourEntity">
SELECT * FROM your_table
LIMIT #{offset}, #{limit}
</select>
<select id="selectTotalCount" resultType="long">
SELECT COUNT(*) FROM your_table
</select>
</mapper>
- 이렇게 구현하면, 컨트롤러에서는 **
currentPage
**와 **pageSize
**를 전달하면서 getEntitiesWithPagination
메소드를 호출해 해당 페이지의 데이터를 받아올 수 있다. 뷰에서는 페이지네이션을 구현하기 위해 컨트롤러에서 받은 Pagination
객체와 데이터를 사용하면 된다.