java 后端简单分页

好久没有搞分页了,,一般好点的项目, 都有 集成第三方 的分页插件了,直接用就好啦,根本不需要管那么多。。 可是 在 开发一个 项目的时候, 他的分页没有使用 分页插件,,而是 使用原生的 limit分页,而我又想 集成 分页插件进来。。。所以把我 绕了半天了。。。绝对是没有睡好了,,,下面 解释一下分页吧。

Java后端的分页,要看 前端的需求,,有些他们需要 返回 pageALL 即总页数,, 有些不需要,只需要 总数即可。 前端 请求后端需要 带过来 页数,和 每页的 数据 条数,我们就可以后台分页了。。。 这里不说 什么 物理分页,,,内存分页,,这些 只需要 集成了 分页插件, 都会帮我 们搞定了,,,这里 只说 原生的 mysql limit 分页 实现的。

一般 分页 是很简单的代码的:

@Override public JSONObject findAssetAll(int page, int rows) { // 分页逻辑处理 if (page == 0) { page = 1;// 为了防止0页的情况发生 } int num = 0; if (page == 1) { num = 0; } else {

		num = rows * (page - 1);
	}
	Example example=new Example(Asset.class);
	int total=assetMapper.selectCountByExample(example);
	
	String selectSql="asset_id desc limit "+num+" , "+rows;
	
	example.setOrderByClause(selectSql);
	List<Asset> assets=assetMapper.selectByExample(example);
public class PagerLimit {

	public static void main(String[] args) {
		
		int page = 2; //页数
		int rows = 7; // 即 pageSize
		
		// 分页逻辑处理
		if (page == 0) {
			page = 1;// 为了防止0页的情况发生
		}
		int num = 0;
		if (page == 1) {
			num = 0;
		} else {

			num = rows * (page - 1);
		}
		
		System.out.println(">>>page>>"+page);
		//  num =7 rows =7 反推出  page
		System.out.println(">>>>offset>>"+num);
		
		// 反算出 当前的需要的 页数 
		System.out.println(num/rows +1);
		
		
	}

}

上面没有计算出总 页数,其实并不难计算出来的。。。这里不说明了。百度吧

我今天下午掉进坑里了,,,把limit 的 用法 搞混了,,, 忘记了,,

比如 limit 1,3 不是求出 1到 3 的 数据,,,不是 2条数据。。 而是求出 从1 开始的 后台 3条 数据,如果有的话,,,即使 3条数据。 pageSize 就是 对应 limit 的 最后 一个 数字 即 3 的数字。。。

我这个项目 的分页 的实现有点 不一样的。 前端会根据总数和页数 直接帮我们计算出来 MySQL limit的 2个 数字,后端直接 拿到了 数据了。 直接 放入 SQL里面 limit 就执行就可以了。。。

是挺方便的,可是目前我要集成 分页插件的时候,就郁闷了,,分页插件 需要 放入 page 页数的值的。 即 这样就可以了 将 查询的 起始偏移量 除以 rows 即 每页的数据量 再加 1 即可以取得 正确的 页数的。。 就可以了,,,就可以放心的 集成 分页插件了

page  =  num/rows +1

最后

逻辑分页即是 查询出来 总数据量,然后 分页,,,数据多的话很不好的 limit 就是 物理分页,包括 分页插件也是 物理分页的,,,mybatis 自带的分页是逻辑分页因此 会使用第三方的分页插件 。

逻辑分页代码有兴趣可以看看:

package test.page;

import java.io.Serializable;
import java.util.List;

public class Pager<T> implements Serializable {
	
	public static void main(String[] args) {
		
	}
	

	private static final long serialVersionUID = -8741766802354222579L;
	
	private int pageSize; // 每页显示多少条记录
	
	private int currentPage; //当前第几页数据
	
	private int totalRecord; // 一共多少条记录
	
	private int totalPage; // 一共多少页记录
	
	private List<T> dataList; //要显示的数据
	
	public Pager(int pageNum, int pageSize, List<T> sourceList){
		if(sourceList == null || sourceList.isEmpty()){
			return;
		}
		
		// 总记录条数
		this.totalRecord = sourceList.size();
		
		// 每页显示多少条记录
		this.pageSize = pageSize;
		
		//获取总页数
		this.totalPage = this.totalRecord / this.pageSize;
		if(this.totalRecord % this.pageSize !=0){
			this.totalPage = this.totalPage + 1;
		}
		
		// 当前第几页数据
		this.currentPage = this.totalPage < pageNum ?  this.totalPage : pageNum;
				
		// 起始索引
		int fromIndex	= this.pageSize * (this.currentPage -1);
		
		// 结束索引
		int toIndex  = this.pageSize * this.currentPage > this.totalRecord ? this.totalRecord : this.pageSize * this.currentPage;
				
		this.dataList = sourceList.subList(fromIndex, toIndex);
	}
	
	public Pager(){
		
	}

	public Pager(int pageSize, int currentPage, int totalRecord, int totalPage,
			List<T> dataList) {
		super();
		this.pageSize = pageSize;
		this.currentPage = currentPage;
		this.totalRecord = totalRecord;
		this.totalPage = totalPage;
		this.dataList = dataList;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public List<T> getDataList() {
		return dataList;
	}

	public void setDataList(List<T> dataList) {
		this.dataList = dataList;
	}

}

转载于:https://my.oschina.net/ouminzy/blog/1476638

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值