JAVA WEB 实现分页技术

这段时间因为项目需要,要做一个分页的功能,具体来说就是希望从数据库每次只取一部分结果,这样每页就显示那些结果,实现原理很简单,就是建立一个Pageutil类,里面放当前访问的页数(这个是从客户浏览器传到后台的数据,所以你的分页需要用它来定位记录的条目)和每一页显示的记录行数。然后通过分页计算就可以得出下列数据。

分页对象代码:

public class PageUtil {
private int pageNo;//页号
private int pageSize;//页面大小
private int totalSize;//数据总大小
private int totalPage;//总共多少页
private int sta;//附加条件


@Override
public String toString() {
return "PageUtil [pageNo=" + pageNo + ", pageSize=" + pageSize
+ ", totalSize=" + totalSize + ", totalPage=" + totalPage
+ ", sta=" + sta + "]";
}




public int getPageNo() {
return pageNo;
}


public int getSta() {
return sta;
}




public void setSta(int sta) {
this.sta = sta;
}
//对页号的处理
public void setPageNo(int pageNo) {
if(pageNo<=0){
this.pageNo=1;
}else if(this.totalPage!=0&&pageNo>this.totalPage){
this.pageNo=this.totalPage;
}else{
this.pageNo = pageNo;
}
}




public int getPageSize() {
return pageSize;
}
//对页面大小的处理
public void setPageSize(int pageSize) {
if(pageSize<=0){
this.pageSize=5;
}else{
this.pageSize = pageSize;
}
}




public int getTotalSize() {
return totalSize;
}
//对数据条数的处理
public void setTotalSize(int totalSize) {
if(totalSize<0){
this.totalSize=0;
}else{
this.totalSize = totalSize;
}
}
//对数总页面数量的处理
public int getTotalPage() {
totalPage=this.totalSize%this.pageSize==0?this.totalSize/this.pageSize:this.totalSize/this.pageSize+1;
return totalPage;
}




@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + pageNo;
result = prime * result + pageSize;
result = prime * result + totalPage;
result = prime * result + totalSize;
result = prime * result + sta;
return result;
}








@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PageUtil other = (PageUtil) obj;
if (pageNo != other.pageNo)
return false;
if (pageSize != other.pageSize)
return false;
if (totalPage != other.totalPage)
return false;
if (totalSize != other.totalSize)
return false;
if (sta != other.sta)
return false;
return true;
}
}

分页方法的优化:

public String getOrderInfo(){ 
int usersId = orderInfoBean.getUsersId();
//如果前台提出的是按条件分页查询,则往附加属性注值
if(orderInfoBean.getOrdSta()!=0){
pageUtil.setSta(orderInfoBean.getOrdSta());
}
//判断是否是第一次调用这个方法
Object obj=session.get("pageUtil");
if(obj==null){
pageUtil=new PageUtil();
pageUtil.setPageNo(1);
pageUtil.setTotalSize(ordersService.getTotal(usersId));
pageUtil.setPageSize(5);
}
//对不同的操作请求进行判断
if("1".equals(op)){
pageUtil.setPageNo(1);
}else if("2".equals(op)){
pageUtil.setPageNo(pageUtil.getPageNo()-1);
}else if("3".equals(op)){
pageUtil.setPageNo(pageUtil.getPageNo()+1);
}else if("4".equals(op)){
pageUtil.setPageNo(pageUtil.getTotalPage());
}
//将最新的分页信息存起来
session.put("pageUtil", pageUtil);
//根据页面号查询信息
ordersList=ordersService.find(usersId,pageUtil.getSta(),pageUtil.getPageNo(),pageUtil.getPageSize());
//存到session中,方便前台的使用
session.put("yeorders", ordersList);
//转成json数据
jsonObject=new JsonObject();
//往前台传送
jsonObject.setRows(ordersList);
return "success";
}

html中的分页代码:


js中的函数:

function pageInfo(op){
var usersId = $("#Id_hidden").val();
$.post("front/orderpage_getOrderInfo.action",{op:op,usersId:usersId},function(data){
var myorders=data.rows;
var str="";
for(var i=0;ivar ordStas=myorders[i].ordSta;
// 0 取消订单1 未支付2已支付等待发货 3待收货  4已签收
if(ordStas==0){
var zhuatai="订单取消";
}else if(ordStas==1){
var zhuatai="未支付";
}else if(ordStas==2){
var zhuatai="已支付等待发货";
}else if(ordStas==3){
var zhuatai="待收货";
}else if(ordStas==4){
var zhuatai="已签收";
}
str+="

";
str+="
";
str+="

"+zhuatai+"(订单状态)

";
str+="
日期:"+myorders[i].ordDate+" |";
str+=" "+myorders[i].ordId+"(订单号) |  ";
str+="订单金额:"+myorders[i].ordTatol+"";
str+="
";
for(var j=0;jstr+="
";
str+="
";
str+="

"+myorders[i].orderdetails[j].goodsName+"

";
str+="

"+myorders[i].orderdetails[j].detaPrice+"元 × "+myorders[i].orderdetails[j].detaNum+"件

";
}
str+="
";
}
$("#bigDiv").html($(str)); 
},"json");
}

资源配置文件mapper.xml:

resultMap :一对多











 
 

你可能感兴趣的:(分页)