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

hjb66300 2015-08-16 11:02:24
新手正在学习,想请教下各位牛人,用SSH怎么实现查询结果分页功能,最好有一段完整的代码就太感激了
...全文
1662 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
  • 打赏
  • 举报
回复
这儿有分页也就那么回事,自己写个方法就搞定。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧