/**
* @author gzj
*/
public class PageDemo {
public static void main(String[] args) {
// 从前端获取页码和页大小和查询条件
Page page = frontData(4, 10, "a");
System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
page = frontData(4, 10, "b");
System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
page = frontData(4, 10, "c");
System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
page = frontData(4, 10, "d");
System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
page = frontData(4, 10, "e");
System.out.println(handleQuery(page.getCondition(), page.getPageNow(), page.getPageSize()));
}
/**
* 根据页码和页大小和查询条件查询数据
*
* @param frontCondition 查询条件
* @param pageNow 页码
* @param pageSize 页大小
* @return 后端返回给前端的数据
*/
private static BackPage handleQuery(String frontCondition, int pageNow, int pageSize) {
// 条件查询 共多少数据
int total = conditionSelectCount(frontCondition);
// 余数
int remainder = total % pageSize;
// 总共多少页
int pageCount = remainder == 0 ? total / pageSize : (total / pageSize) + 1;
// 页码
pageNow = (pageNow * pageSize) > total ? pageCount : pageNow;
// 计算limit起始位置
int currentPage = pageNow > 0 ? (pageNow - 1) * pageSize : pageNow * pageSize;
// 怕页码超出界限了 超出界限就通过currenPage (3 * 10) > total (25) ? currentPage (20) - (pageSize (10) - remainder (3)) : currentPage
int startPage = currentPage > total ? currentPage - (pageSize - remainder) : currentPage;
// 查询数据
String data = conditionSelect(frontCondition, startPage, pageSize, total);
// 返回前端
BackPage backPage = new BackPage(pageNow, pageSize, total, data, pageCount);
return backPage;
}
/**
* 模拟查询
*
* @param condition 查询条件
* @param total
* @return 查询后得出来的具体数据
*/
public static String conditionSelect(String condition, int start, int pageSize, int total) {
if ("a".equals(condition)) {
return "按条件a 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
}
if ("b".equals(condition)) {
return "按条件b 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
}
if ("c".equals(condition)) {
return "按条件c 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
}
if ("d".equals(condition)) {
return "按条件d 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
}
return "其他条件 分页 limit " + start + "," + pageSize + createData(start, pageSize, total);
}
/**
*
* @param start
* @param pageSize
* @param total
* @return
*/
private static String createData(int start, int pageSize, int total) {
StringBuilder sb = new StringBuilder(" ,数据: ");
if (total > 0) {
// 找到比较小的充当数据最大值 因为start 是limit的起始位置 而pageSize是前端指定的页大小 total表示就这些数据
int maxData = (start + pageSize) > total ? total : (start + pageSize);
for (int i = start; i < maxData; i++) {
sb.append(i + ",");
}
return sb.deleteCharAt(sb.length() - 1).toString();
}
return sb.toString();
}
/**
* 模拟条件查询后的数据共多少条
*
* @param condition 条件
* @return total 共多少条
*/
public static int conditionSelectCount(String condition) {
// 先查询这个条件能查出来多少条数据 select count(*) from table_name where condition
if ("a".equals(condition)) {
return 54;
}
if ("b".equals(condition)) {
return 33;
}
if ("c".equals(condition)) {
return 11;
}
if ("d".equals(condition)) {
return 0;
}
return 5;
}
/**
* 模拟前端传参
*
* @return
*/
public static Page frontData() {
return new Page();
}
/**
* 模拟前端传参
*
* @param pageNow 第几页
* @param pageSize 一页多大
* @param condition 查询条件
* @return 封装前端传参
*/
public static Page frontData(int pageNow, int pageSize, String condition) {
return new Page(pageNow, pageSize, condition);
}
public static class Page {
int pageSize = 10;
int pageNow = 1;
String condition = "a";
public Page() {
}
/**
* @param pageNow 第几页
* @param pageSize 页大小
* @param condition 查询条件
*/
public Page(int pageNow, int pageSize, String condition) {
this.pageSize = pageSize;
this.pageNow = pageNow;
this.condition = condition;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
}
public static class BackPage {
private int pageNow;
private int pageSize;
private int pageCount;
private String data;
private int total;
/**
* @param pageNow 当前页
* @param pageSize 页大小啊
* @param total 共多少条
* @param data 查询的数据
* @param pageCount 共多少页
*/
public BackPage(int pageNow, int pageSize, int total, String data, int pageCount) {
this.pageNow = pageNow == 0 ? 1 : pageNow;
this.pageSize = pageSize;
this.pageCount = pageCount;
this.total = total;
this.data = data;
}
@Override
public String toString() {
return "BackPage{" +
"pageNow=" + pageNow +
", pageSize=" + pageSize +
", pageCount=" + pageCount +
", data='" + data + '\'' +
", total=" + total +
'}';
}
}
}
java代码所写的分页demo示例不用连接mysql
于 2024-02-28 14:53:36 首次发布