Skip to content

07. PageUtil

晓寻遥 edited this page Nov 6, 2021 · 1 revision

Introduce

the paging tool is not an encapsulation of database paging, but a conversion of paging methods. when we manually paging, we often use the page number + the number of pages per page, but some databases need to use the start position and the end position to indicate. Many times this conversion is prone to errors (boundary issues), so the PageUtil tool class is encapsulated.

method

1. transToStartEnd

Introduce

convert the number of pages and the number of entries per page into a start position and an end position. this method is used for paging methods that do not include the end position.

Example

public void transToStartEnd(){
		// 10 20
		int[] a = PageUtil.transToStartEnd(1, 10);
		for(int i : a){
			System.out.print(i + " ");
		}

		System.out.println();

		// 0 10
		int[] b = PageUtil.transToStartEnd(0, 10);
		for(int i : b){
			System.out.print(i + " ");
		}
}

2. totalPage

Introduce

calculate the total number of pages based on the total.

Example

public void totalPage(){
		int a = PageUtil.totalPage(10, 3);
		// 4
		System.out.println(a);
}

more

to learn more, refer to the API documentation

Clone this wiki locally