zoukankan      html  css  js  c++  java
  • 在javaweb中使用分页查询的详细步骤

    首先在原有的数据库实体bean上在建立一个分页实体bean

    /**
    * 用于展示分页数据的Javabean对象
    * @author Lenovo
    *
    */
    public class PageenationBean {

    private Integer currPage;//当前页数

    private Integer totalPage;//总页数

    private List<UserBean> dataList;//table的数据

    public Integer getCurrPage() {
    return currPage;
    }

    public void setCurrPage(Integer currPage) {
    this.currPage = currPage;
    }

    public Integer getTotalPage() {
    return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
    this.totalPage = totalPage;
    }

    public List<UserBean> getDataList() {
    return dataList;
    }

    public void setDataList(List<UserBean> dataList) {
    this.dataList = dataList;
    }
    }

    然后在持久层和业务层书写分页查询的方法

    持久层:

    public interface IUserDao {

    /**
    * 获取总条数
    * @return
    */
    public int getTotalCount();
    /**
    * 通过起始索引获取数据
    * @param StartIndex
    * @return
    */
    public List<UserBean> getUserListByStartIndex(int StartIndex);

    业务层:

    public interface IUserService {


    /**
    * 获取总页数
    * @return
    */
    public int getTotalPage();

    /**
    * 通过起始索引获取数据
    * @param StartIndex
    * @return
    */
    public List<UserBean> getUserListByCurrPage(int currPage);

    然后实现接口方法

    持久类接口实现:

    public class UserDaoImpl extends BaseDao implements IUserDao{

    @Override
    public int getTotalCount() {
    this.setConnection();
    int totalCount = 0;
    try {
    ps = con.prepareStatement("select count(*) from stuscore");
    rs = ps.executeQuery();
    if(rs.next()) {
    totalCount =rs.getInt(1);
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    this.closeConnection();
    return totalCount;
    }

    @Override
    public List<UserBean> getUserListByStartIndex(int StartIndex) {
    List<UserBean> list = new ArrayList<UserBean>();
    this.setConnection();
    try {
    ps = con.prepareStatement("select * from stuscore limit ?,5");
    ps.setInt(1, StartIndex);
    rs=ps.executeQuery();
    while(rs.next()) {
    UserBean user=new UserBean();
    user.setId(rs.getInt("id"));
    user.setName(rs.getString("name"));
    user.setGrade(rs.getString("grade"));
    user.setSex(rs.getString("sex"));
    user.setAge(rs.getInt("age"));
    user.setScore(rs.getInt("score"));
    list.add(user);
    }

    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    this.closeConnection();
    }
    return list;
    }

    业务类接口实现:

    public class UserServiceImpl implements IUserService{

    private IUserDao dao= new UserDaoImpl();
    @Override
    public int getTotalPage() {
    //得到数据总条数
    int totalCount= dao.getTotalCount();
    //计算总页数公式:totalPage = (totalCount + pageSize -1)/pageSize
    int totalPage = (totalCount+5-1)/5;
    return totalPage;
    }

    @Override
    public List<UserBean> getUserListByCurrPage(int currPage) {
    //通过当前页计算起始索引
    int startIndex = (currPage-1)*5;
    //通过起始索引查询数据
    List<UserBean> userList =dao.getUserListByStartIndex(startIndex);
    return userList;
    }

    然后 书写servlet方法

    @WebServlet("/ShowUserListServlets")
    public class ShowUserListServlets extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //动态获取当前第几页并做计算
    String operation = request.getParameter("operation");
    String currPageStr = request.getParameter("currPage");
    int currPage = 1;
    //调取业务层查询数据
    IUserService service= new UserServiceImpl();
    //获取总页数
    int totalPage = service.getTotalPage();
    if(currPageStr == null) {
    currPage = 1;
    }else if("首页".equals(operation)){
    currPage = 1;
    }else if("上一页".equals(operation)) {
    currPage = Integer.parseInt(currPageStr)-1;
    if(currPage <= 0) {
    currPage = 1;
    }
    }else if("下一页".equals(operation)) {
    currPage = Integer.parseInt(currPageStr)+1;
    if(currPage >= totalPage) {
    currPage = totalPage;
    }
    }

    List<UserBean> userList = service.getUserListByCurrPage(currPage);

    //构建javabean对象-PagenationBean
    PageenationBean pagenationBean = new PageenationBean();
    pagenationBean.setCurrPage(currPage);
    pagenationBean.setTotalPage(totalPage);
    pagenationBean.setDataList(userList);

    //放入作用域
    request.setAttribute("page", pagenationBean);
    request.getRequestDispatcher("userList.jsp").forward(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

    最后用将jsp作为显示

    <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <table border="2" style="color:red,border-collapse: collapse;">
    <tr>
    <th>学号</th>
    <th>姓名</th>
    <th>性别</th>
    <th>年龄</th>
    </tr>
    <c:forEach items="${page.dataList }" var="u">
    <tr>
    <td>${u.id }</td>
    <td>${u.name }</td>
    <td>${u.sex}</td>
    <td>${u.age}</td>
    </tr>
    </c:forEach>
    </table>
    <h3>
    第${page.currPage }页/共${page.totalPage }页
    </h3>
    <br>
    <form action="ShowUserListServlets" method="get">
    <input type="submit" value="首页" name="operation">
    <input type="submit" value="上一页" name="operation">
    <input type="submit" value="下一页" name="operation">
    <input type="submit" value="尾页" name="operation">
    <%--隐藏表单域作用,记录当前是第几页 --%>
    <input type="hidden" name="currPage" value="${page.currPage }" >
    <hr>
    班级:<input type="text" name="class"><br>
    性别:<input type="text" name="sex"><br>
    成绩段:
    <input type="text" name="min">至
    <input type="text" name="max"><br>
    <input type="submit" value="查询">
    </form>
    </body>
    </html>

  • 相关阅读:
    Haskell Interactive Development in Emacs
    Access Java API in Groovy Script
    手工设置Eclipse文本编辑器的配色
    Color Theme of Emacs
    Gnucash的投资记录
    Special Forms and Syntax Sugars in Clojure
    Use w3m as Web Browser
    SSE指令集加速之 I420转BGR24
    【图像处理】 增加程序速度的方法
    TBB 入门笔记
  • 原文地址:https://www.cnblogs.com/wind-copy1234/p/11839378.html
Copyright © 2011-2022 走看看