study/Spring
7_Spring(페이징)
스파이크12
2020. 2. 29. 08:43
PageInfo (vo)
package com.kh.spring.common.vo;
public class PageInfo {
private int listCount; // 전체 게시글 수
private int limit; // 한 페이지에 보여질 게시글 수
private int pagingBarSize; // 보여질 페이징바의 페이지 개수
private int currentPage; // 현재 페이지 번호를 표시할 변수
private int maxPage; // 전체 페이지에서 가장 마지막 페이지
private int startPage; // 페이징바 시작 페이지 번호
private int endPage; // 페이징바 끝 페이지 번호
public PageInfo() {}
public PageInfo(int listCount, int limit, int pagingBarSize, int currentPage, int maxPage, int startPage,
int endPage) {
super();
this.listCount = listCount;
this.limit = limit;
this.pagingBarSize = pagingBarSize;
this.currentPage = currentPage;
this.maxPage = maxPage;
this.startPage = startPage;
this.endPage = endPage;
}
public int getListCount() {
return listCount;
}
public void setListCount(int listCount) {
this.listCount = listCount;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getPagingBarSize() {
return pagingBarSize;
}
public void setPagingBarSize(int pagingBarSize) {
this.pagingBarSize = pagingBarSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getMaxPage() {
return maxPage;
}
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}
public int getStartPage() {
return startPage;
}
public void setStartPage(int startPage) {
this.startPage = startPage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
@Override
public String toString() {
return "PageInfo [listCount=" + listCount + ", limit=" + limit + ", pagingBarSize=" + pagingBarSize
+ ", currentPage=" + currentPage + ", maxPage=" + maxPage + ", startPage=" + startPage + ", endPage="
+ endPage + "]";
}
}
-> Pagination
package com.kh.spring.common;
import com.kh.spring.common.vo.PageInfo;
public class Pagination {
// 페이지 정보를 담아줄 PageInfo 참조변수 선언
private static PageInfo pi = null;
// PageInfo 객체를 리턴하는 static 메소드 추가
public static PageInfo getPageInfo(int limit, int pagingBarSize, int currentPage, int listCount) {
// currentPage와 listCount가 넘어온 상태이기 때문에
// 페이징 처리에 필요한 나머지 변수만 선언함
//int limit = 5; // 한 페이지에 보여질 게시글 개수
//int pagingBarSize = 10; // 한 페이지에서 보여질 페이징 수
int maxPage; // 전체 페이징 수 중 가장 마지막 페이지
int startPage; // 현재 페이지에서 보여질 페이징 버튼의 시작 페이지
int endPage; // 현재 페이지에서 보여질 페이징 버튼의 끝 페이지
// * maxPage - 총 페이지수
// 게시글의 개수가 100개일 경우 필요 페이지 수 : 10 페이지
// 게시글의 개수가 101개일 경우 필요 페이지 수 : 11 페이지
// 전체 게시글 수 / 한 페이지에 보여질 개수 결과를 올림 처리함.
maxPage = (int)Math.ceil(((double)listCount / limit));
// * startPage - 현재 페이지에 보여질 시작 페이지 수
// 아래쪽에 페이지 수가 10개씩 보여지게 할 경우
// 1, 11, 21, 31, .....
startPage = (currentPage-1)/pagingBarSize * pagingBarSize + 1;
// * endPage - 현재 페이지에서 보여질 마지막 페이지 수
// 아래쪽에 페이지 수가 10개씩 보여지게 할 경우
// 10, 20, 30, 40, .....
endPage = startPage + pagingBarSize - 1;
// 하지만 마지막 페이지 수가 총 페이지 수보다 클 경우
// maxPage가 13페이지고 endPage가 20페이지일 경우
if(maxPage < endPage) {
endPage = maxPage;
}
if(pi == null) {
pi = new PageInfo(listCount, limit, pagingBarSize, currentPage, maxPage, startPage, endPage);
}else {
pi.setListCount(listCount);
pi.setLimit(limit);
pi.setPagingBarSize(pagingBarSize);
pi.setCurrentPage(currentPage);
pi.setMaxPage(maxPage);
pi.setStartPage(startPage);
pi.setEndPage(endPage);
}
return pi;
}
}
-> Controller
// 검색 조건이 있는지 확인하여 map에 세팅
Map<String, String> map = null;
if(searchKey != null && searchValue != null) {
map = new HashMap<String, String>();
map.put("searchKey", searchKey);
map.put("searchValue", searchValue);
}
// 전체 공지사항 게시글 수를 조회(페이징 처리를 위해서)
int listCount = noticeService.getListCount(map);
//System.out.println("공지글개수 : " + listCount);
// 현재 페이지 계산
if(currentPage == null) currentPage = 1;
// 페이지 정보 저장
PageInfo pInf = Pagination.getPageInfo(10,
10, currentPage, listCount);
// 공지사항 목록 조회
List<Notice> list = noticeService.selectList(map, pInf);
-> DAO
public List<Notice> selectList(Map<String, String> map, PageInfo pInf) throws Exception {
// 건너뛸 레코드의 갯수
int offset = (pInf.getCurrentPage()-1)* pInf.getLimit();
// mybatis에서 제공하는 클래스(api)
// offset만큼 건너뛰고 pInf.getLimit()만큼 레코드를 가져온다
RowBounds rowBounds = new RowBounds(offset, pInf.getLimit());
return sqlSession.selectList("noticeMapper.selectList", map, rowBounds);
}