SSM框架基于自定义Page类实现分页的关键代码

1、mapper.xml文件中,如何添加if判断条件,实现limit分页查询,代码如下:

<select id="list" resultType="Category">
	        select * from   category_      
	        <if test="start!=null and count!=null">
	        	limit #{start},#{count}
	        </if>
</select>

其中,id为mapper借口的方法名称,resultType为返回类型,类似于public int add(int a, int b)中的返回类型int一样

 

2、自定义分页类Page,代码如下:

package com.how2java.page;

public class Page {
	
	public int start ;    //每一页的其实行
	public int count = 5 ;    //每一页显示的数据条数
	public int last ;    //最后一页的第一条记录的行数
	public int getStart() {    
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public int getLast() {
		return last;
	}
	public void setLast(int last) {
		this.last = last;
	}
	public void calculateLast(int total, int count) {    //计算last值
		if( 0 == total % count)	{
			last = total - count ;     //如果记录总数除以每页记录数余数为0,则直接等于last   
		}else {
			last = total - total % count ;    //这是最后一页记录数小于每页记录数的情况
		}
	}
	
}

3、在mapper.java、service以及serviceImpl对应的接口或者实现类添加public List<Category> list(Page page) ;方法,主要是传入的参数设置为Page

4、在前台jsp文件中,引入c标签,对特殊情况进行判断,新增分页模块,如下:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div>
    	<a href="?start=0">首页</a>
    	<c:if test="${page.start>0}"><!--如果当前页的第一条记录小于或等于0,则不显示上一页-->
	    	<a href="?start=${page.start-page.count}">上一页</a>
    	</c:if>
    	<c:if test="${(page.start + page.count) < (page.last+page.count)}">
<!--当前页第一条记录数加上每页记录数——如果大于总记录数则不显示下一页-->
    		<a href="?start=${page.start+page.count}">下一页</a>
    	</c:if>
    	<a href="?start=${page.last}">尾页</a>
    </div>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值