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>