分页功能

写一个工具类page
Page类用于存放分页信息:
start: 开始位置
count: 每页的个数
last: 最后一页的位置
caculateLast()方法: 通过总数total和每页的个数计算出最后一页的位置

public class Page {

	int start=0;
	int count = 5;
	int last = 0;
	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 caculateLast(int total) {
	    // 假设总数是50,是能够被5整除的,那么最后一页的开始就是45
	    if (0 == total % count)
	        last = total - count;
	    // 假设总数是51,不能够被5整除的,那么最后一页的开始就是50
	    else
	        last = total - total % count;		
	}	
}

显示的jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  
 <div style="width:500px;margin:0px auto;text-align:center">
    <table align='center' border='1' cellspacing='0'>
        <tr>
            <td>id</td>
            <td>name</td>
        </tr>
        <c:forEach items="${cs}" var="c" varStatus="st">
            <tr>
                <td>${c.id}</td>
                <td>${c.name}</td>
            </tr>
        </c:forEach>
    </table>
    <div style="text-align:center">
        <a href="?start=0">首  页</a>
        <a href="?start=${page.start-page.count}">上一页</a>
        <a href="?start=${page.start+page.count}">下一页</a>
        <a href="?start=${page.last}">末  页</a>
    </div>
 </div>

xml文件中的sql语句

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
	PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.sjq.mapper.CategoryMapper">
    <insert id="add" parameterType="Category" >
        insert into category_ ( name ) values (#{name})    
    </insert>
    
    <delete id="delete" parameterType="Category" >
        delete from category_ where id= #{id}   
    </delete>
    
    <select id="get" parameterType="_int" resultType="Category">
        select * from   category_  where id= #{id}    
    </select>

    <update id="update" parameterType="Category" >
        update category_ set name=#{name} where id=#{id}    
    </update>
    <select id="list" resultType="Category">
        select * from   category_      
        <if test="start!=null and count!=null">
                limit #{start},#{count}
        </if>
    </select>
    <select id="total" resultType="int">
        select count(*) from   category_      
    </select>	    	    
</mapper>

controller中的方法

@RequestMapping("listCategory")
public ModelAndView listCategory(Page page){    	
	ModelAndView mav = new ModelAndView();
	List<Category> cs= categoryService.list(page);
	int total = categoryService.total();
	
	page.caculateLast(total);
	
	// 放入转发参数
	mav.addObject("cs", cs);
	// 放入jsp路径
	mav.setViewName("listCategory");
	return mav;
}

执行过程:页面访问请求,调用controller中的方法,之后会调用sql语句,返回显示,其中total方法是为了计算最后一页。可以拥有翻页效果是因为前端例如<a href="?start=${page.start+page.count}">这样的语句,可以通过将修改传送到controller方法中的start值,再经由sql语句limit来进行查询start开始,count个的数据,传送到前端显示。

 select * from   category_      
	        <if test="start!=null and count!=null">
                    limit #{start},#{count}
            </if>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值