用SSH怎么实现查询分页功能

hjb66300 2015-08-16 11:02:24
新手正在学习,想请教下各位牛人,用SSH怎么实现查询结果分页功能,最好有一段完整的代码就太感激了
...全文
1565 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
fjzhouxy 2015-08-28
跟数据有关,跟ssh没关
回复
zhuduikedui 2015-08-25
lz 这我毕设时候做的,你参考参考 ServiceImpl: //分页 public List executeQueryByPage(String hql, Object[] parameters, int pageNow, int pageSize) { // TODO Auto-generated method stub Query query=this.sessionFactory.getCurrentSession().createQuery(hql); if(parameters!=null && parameters.length>0){ for(int i=0;i<parameters.length;i++){ query.setParameter(i, parameters[i]); } //体现分页 } return query.setFirstResult((pageNow-1)*pageSize).setMaxResults(pageSize).list(); } ACTION: //查看所有商品,并带分页 public ActionForward goodslist(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String s_pageNow=request.getParameter("pageNow"); int pageNow=1; if(s_pageNow!=null){ pageNow=Integer.parseInt(s_pageNow); } // TODO Auto-generated method stub request.setAttribute("goodslist", goodsinfoService.showGoodsList(6,pageNow)); //测试 int pageCount=goodsinfoService.getPageCount(6); request.setAttribute("pageCount", pageCount); request.setAttribute("pageNow", pageNow); return mapping.findForward("querygoodslist"); } 前台JSP: <c:if test="${pageNow ne 1}"> <a href="${pageContext.request.contextPath}/houtaiLogin.do?flag=appraiselistUI&pageNow=1" class="firstPage">第一页</a> <a href="${pageContext.request.contextPath}/houtaiLogin.do?flag=appraiselistUI&pageNow=${pageNow - 1}" class="previousPage">上一页</a> </c:if> <c:forEach var="i" begin="1" end="${pageCount}"> <c:choose> <c:when test="${pageNow ne i}"> <a href="${pageContext.request.contextPath}/houtaiLogin.do?flag=appraiselistUI&pageNow=${i }">${i }</a> </c:when> <c:otherwise> <span class="currentPage">${i }</span> </c:otherwise> </c:choose> </c:forEach> <c:if test="${pageNow ne pageCount}"> <c:if test="${pageCount ne 0}"> <a class="nextPage" href="${pageContext.request.contextPath}/houtaiLogin.do?flag=appraiselistUI&pageNow=${pageNow + 1}">下一页</a> <a class="lastPage" href="${pageContext.request.contextPath}/houtaiLogin.do?flag=appraiselistUI&pageNow=${pageCount }">最后一页</a> </c:if> </c:if>
回复
java干货 2015-08-25
hibernate 自带分页查询、 然后知道当前页,每页需要显示多少条记录即可!建议定义一个分页类
/*
 * Copyright (C) 2014 China Telecom System Integration Co., Ltd.
 * All rights reserved.
 */

package cn.com.ctsi.commons.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * The type Page view.
 *
 * @param <T> the type parameter
 */
public class PageView<T> implements Serializable {
    private static final long serialVersionUID = 8191429276493862832L;

    /**
     * The constant DEFAULT_RECPERPAGE.
     */
    public static final Integer DEFAULT_RECPERPAGE = 15;
    /**
     * The constant DEFAULT_TABLE_HEIGHT.
     */
    public static final Integer DEFAULT_TABLE_HEIGHT = 240;

    /**
     * The Curr page.
     */
    protected Integer currPage = 1; // 当前页
    /**
     * The Rec num.
     */
    protected Integer recNum; // 总记录数
    /**
     * The Rec num per page.
     */
    protected Integer recNumPerPage = DEFAULT_RECPERPAGE; // 每页记录数

    /**
     * The Page num.
     */
    protected Integer pageNum = 1; // 总页数

    /**
     * The Tbl height.
     */
    protected Integer tblHeight; //
    /**
     * The Tbl hide cols.
     */
    protected String tblHideCols; //
    /**
     * The Tbl cols no.
     */
    protected String tblColsNo; //
    /**
     * The Tbl cols width.
     */
    protected String tblColsWidth; //

    /**
     * The Result.
     */
    protected List<T> result = new ArrayList<T>();

    /**
     * Instantiates a new Page view.
     */
    public PageView() {
        currPage = 1;
        recNumPerPage = DEFAULT_RECPERPAGE;
        recNum = 0;
    }

    /**
     * Instantiates a new Page view.
     *
     * @param currPage      the curr page
     * @param recNum        the rec num
     * @param recNumPerPage the rec num per page
     */
    public PageView(int currPage, int recNum, int recNumPerPage) {
        this.currPage = currPage;
        this.recNum = recNum;
        this.recNumPerPage = recNumPerPage;

    }

    /**
     * Gets curr page.
     *
     * @return the curr page
     */
    public int getCurrPage() {
        return currPage;
    }

    /**
     * Sets curr page.
     *
     * @param currPage the curr page
     */
    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    /**
     * Gets page num.
     *
     * @return the page num
     */
    public int getPageNum() {
        if (recNumPerPage > 0) {
            int num = recNum / recNumPerPage;
            if (num * recNumPerPage < recNum) {
                num++;
            }
            return num;
        }
        return 1;
    }

    /**
     * Gets rec num.
     *
     * @return the rec num
     */
    public int getRecNum() {
        return recNum;
    }

    /**
     * Sets rec num.
     *
     * @param recNum the rec num
     */
    public void setRecNum(int recNum) {
        this.recNum = recNum;
    }

    /**
     * Gets rec num per page.
     *
     * @return the rec num per page
     */
    public int getRecNumPerPage() {
        return recNumPerPage;
    }

    /**
     * Sets rec num per page.
     *
     * @param recNumPerPage the rec num per page
     */
    public void setRecNumPerPage(int recNumPerPage) {
        this.recNumPerPage = recNumPerPage;
    }

    /**
     * Gets result.
     *
     * @return the result
     */
    public List<T> getResult() {
        return result;
    }

    /**
     * Sets result.
     *
     * @param result the result
     */
    public void setResult(List<T> result) {
        this.result = result;
    }

    /**
     * Gets tbl height.
     *
     * @return the tbl height
     */
    public Integer getTblHeight() {
        return tblHeight;
    }

    /**
     * Sets tbl height.
     *
     * @param tblHeight the tbl height
     */
    public void setTblHeight(Integer tblHeight) {
        this.tblHeight = tblHeight;
    }

    /**
     * Gets tbl hide cols.
     *
     * @return the tbl hide cols
     */
    public String getTblHideCols() {
        return tblHideCols;
    }

    /**
     * Sets tbl hide cols.
     *
     * @param tblHideCols the tbl hide cols
     */
    public void setTblHideCols(String tblHideCols) {
        this.tblHideCols = tblHideCols;
    }

    /**
     * Gets tbl cols no.
     *
     * @return the tbl cols no
     */
    public String getTblColsNo() {
        return tblColsNo;
    }

    /**
     * Sets tbl cols no.
     *
     * @param tblColsNo the tbl cols no
     */
    public void setTblColsNo(String tblColsNo) {
        this.tblColsNo = tblColsNo;
    }

    /**
     * Gets tbl cols width.
     *
     * @return the tbl cols width
     */
    public String getTblColsWidth() {
        return tblColsWidth;
    }

    /**
     * Sets tbl cols width.
     *
     * @param tblColsWidth the tbl cols width
     */
    public void setTblColsWidth(String tblColsWidth) {
        this.tblColsWidth = tblColsWidth;
    }

}
回复
大数据小白 2015-08-25
楼主,最核心的内容就是在查询数据的时候使用hibernate设置两个参数,一个是查询记录的起始记录,一个是查询记录的个数。
回复
豫让_______ 2015-08-17
这儿有分页也就那么回事,自己写个方法就搞定。
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-08-16 11:02
社区公告
暂无公告