大二寒假作业之JavaWeb

今日学习了数据浏览分页功能的实现,要想做到分页需要几个数据,1,当前的页码;2,总页码;3,总记录数;4,每页的显示量;5,当前页的数据。

建立一个Page类来实现对这些数据的封装。下面改一次解决这些数据如何获得。

当前页码与每页的显示量可有用户进行设置,总记录数可有sql语句“select (*) from 表名”直接获得,这样有了总记录数与每页显示量则总页码也有了,

当前页的数据可由sql“select * from 表名 limit begin,pagesize”来获取,begin为开始的位置,pagesize为要查寻的数据量,开始的位置为当前页码减一乘每页显示量。

这样BaseDao需要新增一个函数用于查询总数目:

    public Object queryForSingleValue(String sql,Object...args)
    {
        try {
            conn=JDBCTools.getConnection();
            return qr.query(conn,sql,new ScalarHandler(),args);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

Page类如下:

public class Page<T> {
    private Integer pageno;//No page
    private Integer pagetotal;//page count
    private Integer pagetotalcount;//date count
    private Integer pagesize=4;
    private List<T> listdate;
    public Integer getPageno() {
        return pageno;
    }
    public void setPageno(Integer pageno) {
        this.pageno = pageno;
    }
    public Integer getPagetotal() {
        return pagetotal;
    }
    public void setPagetotal(Integer pagetotal) {
        this.pagetotal = pagetotal;
    }
    public Integer getPagetotalcount() {
        return pagetotalcount;
    }
    public void setPagetotalcount(Integer pagetotalcount) {
        this.pagetotalcount = pagetotalcount;
    }
    public Integer getPagesize() {
        return pagesize;
    }
    public void setPagesize(Integer pagesize) {
        this.pagesize = pagesize;
    }
    
    public Page(Integer pageno, Integer pagetotal, Integer pagetotalcount,
            Integer pagesize, List<T> listdate) {
        super();
        this.pageno = pageno;
        this.pagetotal = pagetotal;
        this.pagetotalcount = pagetotalcount;
        this.pagesize = pagesize;
        this.listdate = listdate;
    }
    public List<T> getListdate() {
        return listdate;
    }
    public void setListdate(List<T> listdate) {
        this.listdate = listdate;
    }
    public Page() {
        super();
        // TODO Auto-generated constructor stub
    }
    
}

servlet层也比较清晰,首先获取用户传来的当前页码与每页显示量,然后利用sql语句获取总记录数,这样就有了总页码和开始查询的位置,最后用sql查找对应区间的数据。然后封装乘page对象传到显示页面。

两个sql函数的实现:应为有了BaseDao所以相当简单。

public int qrDateCount()
    {
        String sql="select count(*) from studentdate";
        Number count =(Number) queryForSingleValue(sql);
        return count.intValue();
    }
    public List<StudentDate> qrForPage(int begin,int pagesize)
    {
        String sql="select * from studentdate limit ?,?";
        return queryForList(StudentDate.class,sql,begin,pagesize);
    }

servlet层:

HttpSession session=req.getSession();
        Integer pageno=Integer.parseInt(req.getParameter("pageno"));
        Integer pagesize=Integer.parseInt(req.getParameter("pagesize"));
        Integer pagetotalcount=studentdao.qrDateCount();
        Integer pagetotal=pagetotalcount/pagesize;
        if(pagetotalcount%pagesize>0)
        {
            pagetotal++;
        }
        Integer begin=(pageno-1)*pagesize;
        liststudentdate=studentdao.qrForPage(begin, pagesize);
        for(StudentDate studate:liststudentdate)
        {
            System.out.println(studate.getStudentid()+"  "+studate.getStudentname()+"  "+studate.getStudentclass());
        }
        page=new Page(pageno,pagetotal,pagetotalcount,pagesize,liststudentdate);
        session.setAttribute("page",page);
        session.setAttribute("pagestudentlist",page.getListdate());
        req.getRequestDispatcher("studentpage.jsp").forward(req, resp);

 

posted @ 2021-01-27 12:02  风吹过半夏  阅读(113)  评论(0编辑  收藏  举报