Project/2022.02_게시판 만들기

[스프링] 프로젝트 - 게시판 페이징 처리

멋쟁휘개발자 2022. 2. 19. 21:23
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Paging {

	private int currentPage = 1;
   	private int rowPage = 10;
	private int pageBlock = 10;			
	private int start;
    	private int end;
	private int startPage;
    	private int endPage;
	private int total;
    	private int totalPage;
	
	
	//생성자
	public Paging(int total, String currentPage1) {
		this.total = total;
        
		if(currentPage1 != null) {
			this.currentPage = Integer.parseInt(currentPage1);
		}
        
		start = (currentPage - 1) * rowPage + 1; 		 //시작시 1	11
		end = start + rowPage - 1;				 //시작시 10	20
		totalPage = (int) Math.ceil((double)total / rowPage); 	 //시작시 2
		startPage = currentPage - (currentPage - 1) % pageBlock; //시작시 1
		endPage = startPage + pageBlock - 1;
        
		if(endPage > totalPage) {
			endPage = totalPage;
		}
        
	}
	
}

 

[Paging에 사용하는 변수]

변수명 사용의미
currentPage 현재 페이지 1
rowPage 한 페이지 당, 게시글 몇 개? 10
pageBlock 보여줄 페이지 링크 숫자 몇 개? 10
start 시작 글 번호 (currentPage - 1) * rowPage + 1
end 끝 글 번호 start + rowPage - 1
startPage  시작 페이지 링크 숫자 currentPage - (currentPage - 1) % pageBlock
endPage  끝 페이지 링크 숫자 startPage + pageBlock - 1
total 게시글 수  DB를 통해 구해야 함
totalPage
총 페이지  (int) Math.ceil((double)total / rowPage)

 

 

[예제]

 

 

1. start

 

currentPage  (currentPage - 1) * rowPage + 1 결과
1 (1-1)*10 +1 1
2 (2-1)*10 +1 11
  ...  

 

2. end

start start + rowPage - 1 결과
1 1 + 10 - 1 10
11 11 + 10 - 1 20
  ...  

 

3. startPage

currentPage currentPage - (currentPage - 1) % pageBlock 결과
1 1 - (1-1) % 10 1
2 2 - (2-1) % 10 1.9 -->얜뭐야,,
11 11 - (11-1) % 10 10 --> 11이 나와야,,?

 

4. endPage

startPage startPage + pageBlock - 1 결과
1 1 + 10 - 1 10
11 11 + 10 - 1 20
  ...  

 

5. totalPage

total (int) Math.ceil((double)total / rowPage) 결과
14 14/10 = 1.4
(int) Math.ceil(1.4) = 2
2
28 28/10 = 2.8
(int) Math.ceil(2.8) = 3
3
  ...