import java.util.Arrays; import java.util.Collections; import java.util.List; /** * @author shuang.gao Date: 2016/1/28 Time: 12:26 */ public class Pager<T> { /** * Number of displayed characters per page*/ private int pageSize; /** * Original collection*/ private List<T> data; private Pager(List<T> data, int pageSize) { if (data == null || data.isEmpty()) { throw new IllegalArgumentException("data must be not empty!"); } this.data = data; this.pageSize = pageSize; } /** * Create a pager* * @param data The data that needs to be paging* @param pageSize The number of pieces displayed per page* @param <T> Business object* @return Pager*/ public static <T> Pager<T> create(List<T> data, int pageSize) { return new Pager<>(data, pageSize); } /** * Get the paged data* * @param pageNum Page number* @return Results after paging*/ public List<T> getPagedList(int pageNum) { int fromIndex = (pageNum - 1) * pageSize; if (fromIndex >= data.size()) { return Collections.emptyList(); } int toIndex = pageNum * pageSize; if (toIndex >= data.size()) { toIndex = data.size(); } return data.subList(fromIndex, toIndex); } public int getPageSize() { return pageSize; } public List<T> getData() { return data; } public static void main(String[] args) { Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; List<Integer> list = Arrays.asList(array); Pager<Integer> pager = Pager.create(list, 10); List<Integer> page1 = pager.getPagedList(1); System.out.println(page1); List<Integer> page2 = pager.getPagedList(2); System.out.println(page2); List<Integer> page3 = pager.getPagedList(3); System.out.println(page3); } } As the name suggests, false pagination is not really filtering in the database, but obtaining all the results after querying the database and doing some tricks when presenting it.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author shuang.gao Date: 2016/1/28 Time: 12:26
*/
public class Pager<T> {
/**
* Number of pieces displayed per page
*/
private int pageSize;
/**
* Original collection
*/
private List<T> data;
private Pager(List<T> data, int pageSize) {
if (data == null || data.isEmpty()) {
throw new IllegalArgumentException("data must be not empty!");
}
this.data = data;
this.pageSize = pageSize;
}
/**
* Create a pager
*
* @param data that requires pagination
* @param pageSize Number of pieces displayed per page
* @param <T> Business Object
* @return paging machine
*/
public static <T> Pager<T> create(List<T> data, int pageSize) {
return new Pager<>(data, pageSize);
}
/**
* Get the paged data
*
* @param pageNum Page number
* @return Pagination results
*/
public List<T> getPagedList(int pageNum) {
int fromIndex = (pageNum - 1) * pageSize;
if (fromIndex >= data.size()) {
return Collections.emptyList();
}
int toIndex = pageNum * pageSize;
if (toIndex >= data.size()) {
toIndex = data.size();
}
return data.subList(fromIndex, toIndex);
}
public int getPageSize() {
return pageSize;
}
public List<T> getData() {
return data;
}
public static void main(String[] args) {
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
List<Integer> list = Arrays.asList(array);
Pager<Integer> pager = Pager.create(list, 10);
List<Integer> page1 = pager.getPagedList(1);
System.out.println(page1);
List<Integer> page2 = pager.getPagedList(2);
System.out.println(page2);
List<Integer> page3 = pager.getPagedList(3);
System.out.println(page3);
}
}
This is a simple pager. The principle is very simple. The data queried from the database is passed into the pager and the return is a collection of divided pages.
The advantage is that it is compatible with all JDBC databases, the disadvantage is that this method is not suitable for large batches of data.
The above is all about this article, I hope it will be helpful to everyone's learning.