分页显示数据----后台(将数据库中的信息分页显示到网页)

问题分析:

  当我们想检索数据库中的信息并将其显示到网页上时,如果数据库中的信息过多时 。一方面会使数据库开销非常大,降低性能;另一方面在一张页面上显示过多数据也会降低用户体验。

解决办法: 

  1.由于在分页之后每一页显示的信息不再仅仅是数据空中的数据而是类似于下图:

  1)所以我们需要创建一个page类:

 1     package com.neuedu.manage.bean;
 2 
 3     import java.util.List;
 4 
 5     public class page <T>{
 6         private List<T> data;//数据库查询出的数据
 7         private int totalRecord;//总记录数,数据库查询
 8     
 9         private int pageNumber;//当前页码
10         private int pageSize;//每页的记录数
11     
12         private String path;//jsp页面链接所要跳转的地址
13     
14     //  private int index;    //当前索引,计算得到
15     //  private int totalPage;//总页数 计算得到
16    
17     } 

   2)其中我们为前5个变量创建GET和SET方法;由于后两个变量是计算得到的,只为他们创建GET方法:

 1      public int getIndex() {
 2             /**
 3              * 当前索引值,计算得到的
 4              */
 5             return (getPageNumber()-1)*pageSize;
 6         }
 7   
 8         public int getTotalPage() {
 9             /**
10              * 总页数
11              */
12             if(totalRecord%pageSize==0){
13                 return totalRecord/pageSize;
14             }
15             return (totalRecord/pageSize +1);
16         }

    3)为page创建一个有参构造函数

1   public page( int pageNumber, int pageSize ,int totalRecord) {
2         super();
3             this.pageNumber = pageNumber;
4             this.pageSize = pageSize;
5             this.totalRecord = totalRecord;
6       }

    2.dao层代码:根据当前索引值和每页的记录数将结果查询出来,然后将其封装成user对象并保存到Student类的LIst链表中,将其返回。

 1     public List<Student> getLimitStuList(int index, int pageSize) {
 2         JDBCUtil jdbcUtil=new JDBCUtil();
 3         Connection con = jdbcUtil.getConnection();
 4         PreparedStatement pst=null;
 5         ResultSet rSet=null;
 6         List<Student>list= new ArrayList<Student>();
 7         String sql="SELECT * FROM students limit ?,?";
 8         try {
 9             pst = con.prepareStatement(sql);
10             pst.setInt(1, index);
11             pst.setInt(2, pageSize);
12             rSet = pst.executeQuery(); 
13             while(rSet.next())
14             {
15                 int id=rSet.getInt("id");
16                 String name=rSet.getString("name");
17                 String school=rSet.getString("school");
18                 double score=rSet.getDouble("score");
19                 list.add(new Student(id, name, school, score));
20             }    
21         } catch (SQLException e) {
22             e.printStackTrace();
23         } finally{
24             JDBCUtil.close(con, pst, rSet);
25         }
26         System.out.println(list);
27         return list;    
28     }

 

   3.service层代码:查询数据库中的数据并将其放入page对象。

 1   public class studentService {
 2       private studentDao studentDao=new studentDao();
 3       public page<Student> getStudentList(int pageNo, int pageSize){
 4           //查询当前表的所有记录数
 5           int totalRecord = studentDao.getTotalRecord();
 6           //调用有参构造函数,创建page对象
 7           page<Student> page=new page<Student>(pageNo,pageSize,totalRecord);
 8           //第三步,查询分页列表数据并设置到page对象中
 9           List<Student> list=studentDao.getLimitStuList(page.getIndex(),page.getPageSize());
10           System.out.println(list);
11           page.setData(list);
12           return page;
13       }
14   }

  4.我们为jsp页面上的链接所要跳转的路径编写一个工具类,并将其放在util包下。

 1   //获取截取路径
 2     public class WEBUtils {
 3         public static String grtPath(HttpServletRequest request) {
 4             String requestURI = request.getRequestURI();//请求路径
 5             String queryString = request.getQueryString();//请求参数
 6             System.out.println("请求URI"+requestURI+"?"+queryString);
 7             String url=requestURI+"?"+queryString;
 8             if(url.contains("?pageNo")){
 9                 url=url.substring(0, url.indexOf("?pageNo"));
10             }
11             return url;
12         }
13     }

  5.创建一个servlet,用于接收jsp页面的分页查询数据的请求。

 1   @WebServlet("/studentCheck")
 2   public class studentCheck extends HttpServlet {
 3       private static final long serialVersionUID = 1L;
 4       private static int pageSize=2;//每页记录数
 5       private static int pageNo=1;//当前页码
 6       private studentService service= new studentService();
 7       public studentCheck() {
 8           super();
 9       }
10       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
11           try{
12               pageNo=Integer.parseInt(request.getParameter("pageNo"));//链接所要跳转的"当前页码", 当jsp页面的a标签发生跳转时,从地址栏直接携带过来
13               }catch (Exception e) {
14                   e.getMessage();
15               }
16           String url = WEBUtils.grtPath(request);//获取当前请求路径
17           page<Student> page= service.getStudentList(pageNo,pageSize);
18           page.setPath(url);//创建page对象
19           request.setAttribute("page",page);//将page对象放入请求域
20           request.getRequestDispatcher("/WEB-INF/view/stu-list.jsp").forward(request, response);
21       }
22 
23       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24           request.setCharacterEncoding("utf8");
25           doGet(request, response);
26       }
27 
28   }

   至此,我们基本完成分页显示的后台代码。接下来还要完成前端网页的显示,请见下篇。

原文地址:https://www.cnblogs.com/alternative/p/7373326.html