java分页demo_java分页的知识 - writeademo的个人页面 - OSCHINA - 中文开源技术交流社区...

java仿google的分页算法

1.现将数据从数据库读取出来封装一个java类中,在java类中计算

public class Page {

//成员变量

//当前页

private int nowpage;

//总记录数

private int countrecord;

//总页数

private int countpage;

// 当前页记录开始的位置

private int pageindex;

// 每页显示的记录数

public static final int PAGESIZE = 5;

// 索引的sum值 代表的是 google页面中最大显示页数

private int sumindex = 6;

// 开始的索引值

private int startindex;

// 结束的索引值

private int endindex;

//当前页信息

private List allentities;

//构造器

public Page() {

}

public Page(int countrecord, int nowpage) {

// 计算当前页

this.nowpage = nowpage;

// 计算出当前页开始的位置

this.pageindex = (nowpage - 1) * PAGESIZE;

// 计算总页数

this.countrecord = countrecord;

if (this.countrecord % this.PAGESIZE == 0) {

this.countpage = this.countrecord / this.PAGESIZE;

} else {

this.countpage = this.countrecord / this.PAGESIZE + 1;

}

//计算开始和结束的索引值

//当当前页小于等于四时开始的索引值等于一,而结束的索引值分两种情况

if (this.nowpage <= 4) {

this.startindex = 1;

if (this.endindex > this.countpage) {

this.endindex = this.countpage;

}

this.endindex = this.nowpage + 2;

}

// 当当前页大于四时开始的索引值和结束的索引值均分三种情况

else if (this.nowpage > 4) {

if (this.endindex > this.countpage&& this.countpage < this.sumindex) {

this.startindex = 1;

this.endindex = this.countpage;

}

else if (this.countpage > this.sumindex) {

this.startindex = this.countpage - 5;

this.endindex = this.countpage;

}

else{

this.startindex = this.nowpage - 3;

this.endindex = this.nowpage + 2;

}

}

}

//省略成员变量的get和set方法

}

SQLServer的数据分页:

假设现在有这样的一张表:

CREATE TABLE test

(

id int primary key not null identity,

names varchar(20)

)

然后向里面插入大约1000条数据,进行分页测试

假设页数是10,现在要拿出第5页的内容,查询语句如下:

--10代表分页的大小

select top 10 *

from test

where id not in

(

--40是这么计算出来的:10*(5-1)

select top 40 id from test order by id

)

order by id

原理:需要拿出数据库的第5页,就是40-50条记录。首先拿出数据库中的前40条记录的id值,然后再拿出剩余部分的前10条元素

第二种方法:

还是以上面的结果为例,采用另外的一种方法

--数据的意思和上面提及的一样

select top 10 *

from test

where id >

(

select isnull(max(id),0)

from

(

select top 40 id from test order by id

) A

)

order by id

原理:先查询前40条记录,然后获得其最id值,如果id值为null的,那么就返回0

然后查询id值大于前40条记录的最大id值的记录。

这个查询有一个条件,就是id必须是int类型的。

第三种方法:

select top 10 *

from

(

select row_number() over(order by id) as rownumber,* from test

) A

where rownumber > 40

原理:先把表中的所有数据都按照一个rowNumber进行排序,然后查询rownuber大于40的前十条记录

这种方法和oracle中的一种分页方式类似,不过只支持2005版本以上的

第四种:

存储过程查询

创建存储过程

alter procedure pageDemo

@pageSize int,

@page int

AS

declare @temp int

set @temp=@pageSize*(@page - 1)

begin

select top (select @pageSize) * from test where id not in (select top (select @temp) id from test) order by id

end

执行存储过程

exec 10,5

分页的总页数算法

总记录数:totalRecord

每页最大记录数:maxResult

算法一:

totalPage = totalRecord % maxResult == 0 ? totalRecord / maxResult : totalRecord / maxResult + 1 ;

算法二:(推荐)

totalPage = (totalRecord + maxResult -1) / maxResult;

其中 maxResult  - 1 就是 totalRecord / maxResult 的最大的余数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值