jsp分页

本文详细介绍了在Java JSP中实现数据库分页的方法,包括分页处理函数的编写、调用实现、Mapper文件配置、函数调用流程以及在页面上的不同数据状态显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、分页处理函数

package com.wangyin.platarch.util;

import java.util.List;
public class PageUtil <T>{
	private List<T> values;
	private int totalSize = 0;
	private int maxResults = 10;
	private int firstResult = 0;
	public List<T> getValues() {
		return values;
	}
	public void setValues(List<T> values) {
		this.values = values;
		this.finger();
	}
	public int getTotalSize() {
		return totalSize;
	}
	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}
	public int getMaxResults() {
		return maxResults;
	}
	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}
	public int getFirstResult() {
		return firstResult;
	}
	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}
	
	
	private String pageMaker = "";
	public String getPageMaker() {
		return pageMaker;
	}
	public void setPageMaker(String pageMaker) {
		this.pageMaker = pageMaker;
	}
	
	private void finger(){
		if(this.totalSize==0){
			this.setPageMaker("");
			return;
		}
		
		StringBuffer buffer = new StringBuffer();
		
		if(this.getFirstResult()==0){
			buffer.append("‹‹ 首页  ");
			buffer.append("‹ 上一页  ");
		}else{
			buffer.append("<a class=\"more\" href=\"?first=0\" onclick=\"pageFlag\">‹‹ 首页</a>   ");
			buffer.append("<a class=\"more\" href=\"?first="+(this.firstResult-this.maxResults)+"\"  title=\"上一页\">‹ 上一页</a>    ");
		}
		buffer.append("<span class=\"ft-normal\">"+(this.getFirstResult()+1)+"-"+((this.getFirstResult()+this.getMaxResults())>totalSize?totalSize:(this.getFirstResult()+this.getMaxResults()))+"条,共"+this.getTotalSize()+"条</span>    ");
		int totalPage = this.totalSize/this.maxResults;
		if(0==this.totalSize%this.maxResults)totalPage--;
		if(this.firstResult+this.maxResults>=totalSize){
			buffer.append("下一页 ›  ");
			buffer.append("末页 ››");
		}else{
			buffer.append("<a class=\"more\" href=\"?first="+(this.firstResult+this.maxResults)+"\">下一页 ›</a>   ");
			buffer.append("<a class=\"more\" href=\"?first="+totalPage*this.maxResults+"\" >末页 ››</a>");
		}
		
		
		

		this.setPageMaker(buffer.toString());
	}
	@Override
	public String toString() {
		return "PageUtil [ totalSize=" + totalSize
				+ ", maxResults=" + maxResults + ", firstResult=" + firstResult +"]";
	}
	
	
}


 

2、分页调用实现函数

@Autowired
	private PlaDefrayInfoMapper plaDefrayInfoMapper;
	
	public PageUtil<PlaDefrayInfo> queryDefraysByPage(PlaDefrayInfo defray,
			int first) {
		PageUtil<PlaDefrayInfo> pu=new PageUtil<PlaDefrayInfo>();
		Map<String , Object> map=new HashMap<String , Object>();
		map.put("vo", defray);
		pu.setTotalSize(this.plaDefrayInfoMapper.totalCount(map));
		pu.setFirstResult(first);
		map.put("pageno",first);
		map.put("maxresult", pu.getMaxResults());
		List<PlaDefrayInfo> plaOrderInfos = plaDefrayInfoMapper.queryPaging(map);
		//赋值内容到信息详情类中
		pu.setValues(plaOrderInfos);
		return pu;
	}

 

3、Mapper文件

<select id="totalCount" resultType="java.lang.Integer" parameterType="java.util.Map">
    select count(*) from pla_defray_info where 1=1
     <if test="vo.merchantid!='' and vo.merchantid!=null">
        and merchantid like CONCAT('%',#{vo.merchantid},'%') 
      </if>
      <if test="vo.ordernum!='' and vo.ordernum!=null">
        and ordernum like CONCAT('%',#{vo.ordernum},'%') 
     </if>
     <if test="vo.orderType!=null and vo.orderType!=''">
        and order_type=#{vo.orderType}
     </if>
     <if test="vo.defraynum!=null and vo.defraynum!=''">
        and defraynum like CONCAT('%',#{vo.defraynum},'%') 
     </if>
     <if test="vo.defrayState!=null and vo.defrayState!=''">
        and defray_state=#{vo.defrayState}
     </if>
  </select>
  <select id="queryPaging" resultMap="BaseResultMap" resultType="java.util.ArrayList" parameterType="java.util.Map">
    select * from pla_defray_info where 1=1
     <if test="vo.merchantid!='' and vo.merchantid!=null">
        and merchantid like CONCAT('%',#{vo.merchantid},'%') 
      </if>
      <if test="vo.ordernum!='' and vo.ordernum!=null">
        and ordernum like CONCAT('%',#{vo.ordernum},'%') 
     </if>
     <if test="vo.orderType!=null and vo.orderType!=''">
        and order_type=#{vo.orderType}
     </if>
     <if test="vo.defraynum!=null and vo.defraynum!=''">
        and defraynum like CONCAT('%',#{vo.defraynum},'%') 
     </if>
     <if test="vo.defrayState!=null and vo.defrayState!=''">
        and defray_state=#{vo.defrayState}
     </if>
     order by created_date desc
     limit #{pageno},#{maxresult}
  </select>


 

4、函数调用

@RequestMapping("/query")
public ModelAndView query(@ModelAttribute("defray") PlaDefrayInfo defray,@RequestParam(required=false,defaultValue="0") int first){
	ModelAndView model=new ModelAndView("/defray/defrayDetail");
	PageUtil<PlaDefrayInfo> pu=defrayService.queryDefraysByPage(defray,first);
	model.addObject("pu", pu);
	return model;
}


5、页面显示

当有数据时显示

<pre class="html" name="code"><c:if test="${fn:length(pu.values)>0}">
<div>显示数据</div>
<!--进行分页--!>
<div>${pu.pageMaker }</div>
</c:if>

<span style="font-size:12px;"></span> 

没有数据时显示

<span style="font-size:12px;"><c:if test="${fn:length(pu.values)==0}">
<div class="ui-box-container" style="width:100%;font-size: 20px;color:blue;" align="center">无数据</div>
</c:if></span>

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值