//把分页用的数据全部封装到一个类中
public class Page {
private int pagesize;//单页记录
private int currentpage = 1;//当前页面
private int countdate;//总记录数
private int lastPage;//上一页
private int nextPage;//下一页
private int countPage;
public int getCurrentpage()
{
return currentpage;
}
public void setCurrentpage(int currentpage)
{
if(currentpage>getCountpage())
{
this.currentpage = getCountpage();
}else {
if(currentpage < 1)
{
this.currentpage = 1;
}
else
{
this.currentpage = currentpage;
}
}
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getCountdate()
{
return countdate;
}
public void setCountdate(int countdate)
{
this.countdate = countdate;
}
//由记录数设定有关的页面数。
public int getCountpage()
{
int i = this.countdate / this.pagesize;
if((this.countdate % this.pagesize)!=0)
{
i+=1;
}
return i;
}
public int getLastPage()
{
return this.getCurrentpage()-1;
}
public int getNextPage()
{
return this.getCurrentpage()+1;
}
private int current;
private int next;
public void setCurrent(int current) {
this.current = current;
}
public void setNext(int next) {
this.next = next;
}
//给ibatis用数据
public int getCurrent()
{
return (this.currentpage-1)*pagesize+1;
}
public int getNext() {
return this.currentpage*pagesize;
}
public int getCountPage() {
return countPage;
}
public void setCountPage(int countPage) {
this.countPage = countPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
@Override
public String toString() {
return "Page [pagesize=" + pagesize + ", currentpage=" + currentpage
+ ", countdate=" + countdate + ", lastPage=" + lastPage
+ ", nextPage=" + nextPage + ", countPage=" + countPage
+ ", current=" + current + ", next=" + next + "]";
}
}
//mybatis SQL语句 如果是根据条件查询自行处理,可以在类中继承该page进行传参
<resultMap type="Goods" id="goodsResult">
<association property="type2" column="type_id" javaType="Goods"
select="Type2.findById"></association>
</resultMap>
<select id="booklist" parameterType="dao.Page" resultMap="goodsResult">
<!--select * from goods g left join type_2 t2 on g.type_id=t2.id order
by g.id -->
<![CDATA[
select * from (select rownum r,goods.* from goods where rownum<=#{next,jdbcType=INTEGER} )g
where r>=#{current,jdbcType=INTEGER} order by g.id asc
]]>
</select>
//spring mvc 控制器类代码
@RequestMapping(value="manager/booklist.do")
public String booklist(HttpServletRequest request,String chose){
Page page=new Page();
int total=goodService.book().size();//总数
page.setPagesize(2);
page.setCountdate(total);
page.setCurrentpage(1);
page.setCountPage(page.getCountpage());
page.setLastPage(page.getLastPage());
page.setNextPage(page.getNextPage());
page.setCurrent(page.getCurrent());
page.setNext(page.getNext());
if(chose!=null){
page.setCurrentpage(Integer.parseInt(chose));
}
List<Goods> boolist=goodService.getBooklist(page);
request.setAttribute("booklist",boolist);
request.setAttribute("page", page);
return "manager/bookList";
}
//页面调用代码
<div>
<c:if test="${page.currentpage==1 }">
<a href="booklist.do?chose=${page.countPage}">末 页</a>
</c:if>
<c:if test="${page.currentpage!=1 }">
<a href="booklist.do?chose=${page.countPage}">末 页</a>
</c:if>
</div>
<div>
<c:if test="${page.countpage>1 && page.currentpage<page.countpage }">
<a href="booklist.do?chose=${page.nextPage }">下一页</a>
</c:if>
<c:if test="${page.currentpage >=page.countpage}">下一页</c:if>
</div>
<div>
<c:if test="${page.currentpage>1 }">
<a href="booklist.do?chose=${page.lastPage }">上一页</a>
</c:if>
<c:if test="${page.currentpage <=1}">上一页</c:if>
</div>
<div>
<c:if test="${page.currentpage==1 }">
<a href="booklist.do?chose=${page.currentpage }">首 页</a>
</c:if>
<c:if test="${page.currentpage!=1 }">
<a href="booklist.do?chose=1">首 页</a>
</c:if>
</div>
<span>当前${page.currentpage }页/${page.countPage }页</span>
</div>
到此完毕