ssm框架实现分页(limit)

pojo层新建类PageBean

package com.qut.pojo;

import java.util.List;

public class PageBean<T>{
    private int pageNumber; //总记录
    private int pageCount; //总页数
    private Integer pageIndex; //当前页
    private int pageSize; //每页几条数据
    private List<T>list; //当前页的数据
    public int getPageNumber() {
        return pageNumber;
    }
    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }
    public int getPageCount() {
        return pageCount;
    }
    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
    public Integer getPageIndex() {
        return pageIndex;
    }
    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public List<T> getList() {
        return list;
    }
    public void setList(List<T> list) {
        this.list = list;
    }
}

实体类Dao层添加两个方法

    //分页显示所有岗位
    public List<Position> showlist(int index);

    //计算数据总数
    public int pagecount();

xml添加两条语句

  <select id="countIndex" parameterType="com.qut.pojo.Position" resultType="int">
        select count(*) from Position where 1=1
        <if test='p_name!=null and p_name!=""'>
            and p_name = #{p_name}
        </if>
        <if test='p_no!=0'>
            and p_no = #{p_no}
        </if>
        <if test='p_type!=null and p_type!=""'>
            and p_type = #{p_type}
        </if>
    </select>

    <select id="showlist" parameterType="int" resultType="com.qut.pojo.Position">
        select *
        from Position limit #{index},10
    </select>

Service层

    public List<Position> showlist(int index);

    public int pagecount();

ServiceImpl层

        public List<Position> showlist(int index) {
            // TODO Auto-generated method stub
            return poMapper.showlist(index);
        }

        public int pagecount() {
            // TODO Auto-generated method stub
            return poMapper.pagecount();
        }

Controller层

    @RequestMapping("/page")
    public ModelAndView showlist(HttpServletRequest request){

            ModelAndView view = new ModelAndView(); 

            Integer pageIndex = 1;
            int pageSize = 4;
            PageBean<Position> pageUtil = new PageBean<Position>();
            List<Position> list = new ArrayList<Position>();
            if(request.getParameter("pageIndex")!=null){
                pageIndex = Integer.parseInt((String)request.getParameter("pageIndex"));
            }
            pageUtil.setPageIndex(pageIndex);
            int number = ps.pagecount();
            pageUtil.setPageNumber(number);
            pageUtil.setPageSize(pageSize);
            if((int)pageUtil.getPageNumber() % (int)pageUtil.getPageSize() ==0){
                pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
                        .getPageNumber() / pageUtil.getPageSize())));
            }else{
                pageUtil.setPageCount((int) Math.ceil((double) (pageUtil
                        .getPageNumber() / pageUtil.getPageSize()))+1);
            }

            int index = (pageIndex-1)*pageSize;
            list = ps.showlist(index);
            pageUtil.setList(list);

            view.addObject("pageUtil",pageUtil);
            view.setViewName("show");
            return view;
        }

jsp遍历代码

<c:forEach items="${pageUtil.list}" var="c" begin="0" end="4">
<!--显示四条,这里要与controller中的pageSize以及xml中sql查询的条数对应-->
    <span class="STYLE1">${c.p_name}</span>
    <span class="STYLE1">${c.p_type}</span>
    <span class="STYLE1">${c.p_num}</span> 
</c:forEach>

jsp显示共多少页,现在在第几页,首页,上一页,下一页以及下一页代码

      <font size="2">共 ${pageUtil.pageCount} 页</font> <font size="2">第  
            ${pageUtil.pageIndex} 页</font> <a href="page.action?pageIndex=1&doaction=woaiyuxue">首页</a>  
        <c:choose>  
            <c:when test="${pageUtil.pageIndex - 1 > 0}">  
                <a href="page.action?pageIndex=${pageUtil.pageIndex - 1}&doaction=woaiyuxue">上一页</a>  
            </c:when>  

        </c:choose>  
        <c:choose>  
            <c:when test="${pageUtil.pageCount==0}">  
                <a href="page.action?pageIndex=${pageUtil.pageIndex }&doaction=woaiyuxue">下一页</a>  
            </c:when>  
            <c:when test="${pageUtil.pageIndex + 1 <= pageUtil.pageCount}">  
                <a href="page.action?pageIndex=${pageUtil.pageIndex+1}&doaction=woaiyuxue">下一页</a>  
            </c:when>  

        </c:choose>  
        <c:choose>  
            <c:when test="${page.pageCount==0}">  
                <a href="page.action?pageIndex=${pageUtil.pageIndex}&doaction=woaiyuxue">尾页</a>  
            </c:when>  
            <c:otherwise>  
                <a href="page.action?pageIndex=${pageUtil.pageCount}&doaction=woaiyuxue">尾页</a>  
            </c:otherwise>  
        </c:choose>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值