java分页的写法

xiaoxiao2021-02-28  7

第一步:

      首先需要到如各部分岁对应的包,例如数据库驱动的包,实体类的包,c3p0的包,以及c3p0的配置文件

      创建数据库,创建数据库所对应的一个实体类,例如:student类,里边包含私有的属性,及set和get方法

第二步:

    创建另一个关于分页的实体类,里边只包含5项,如下可见

package com.qf.javabean; import java.util.List; public class PagStudent<T> { private int currentpage;//当前页 private int pagsize;//每页的数据量 private int totalpage;//总页数 private int totalcount;//总数据量 private List<T> list; public synchronized int getCurrentpage() { return currentpage; } public synchronized void setCurrentpage(int currentpage) { this.currentpage = currentpage; } public synchronized int getPagsize() { return pagsize; } public synchronized void setPagsize(int pagsize) { this.pagsize = pagsize; } public synchronized int getTotalpage() { return (int)Math.ceil(totalcount*1.0/pagsize); } //记住需要删除一个方法,是算出来的,不是被设置的 public synchronized int getTotalcount() { return totalcount; } public synchronized void setTotalcount(int totalcount) { this.totalcount = totalcount; } public synchronized List<T> getList() { return list; } public synchronized void setList(List<T> list) { this.list = list; } public PagStudent(int currentpage, int pagsize, int totalcount, List<T> list) { super(); this.currentpage = currentpage; this.pagsize = pagsize; this.totalcount = totalcount; this.list = list; } public PagStudent() { super(); } } 第三步:创建JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'StudentPaging.jsp' starting page</title> <meta charset="UTF-8"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <!-- Student [id=1, name=张无惧, sex=null, age=0, password=null, qq=null, phone=null, major=null, grade=null] --> <body> <table border="1px" align="center" width="90%" cellspacing="0px"> <tr> <th colspan="9"><h1>学员信息展示平台</h1></th> </tr> <tr> <th >学员编号</th> <th >姓名</th> <th >性别</th> <th >年龄</th> <th >密码</th> <th >QQ号</th> <th >手机号</th> <th >专业</th> <th >班级</th> </tr> <c:forEach items="${page.list }" var="it"> <!-- 属性导航 --> <tr> <td>${it.id }</td> <td>${it.name }</td> <td>${it.sex }</td> <td>${it.age }</td> <td>${it.password }</td> <td>${it.qq }</td> <td>${it.phone }</td> <td>${it.major }</td> <td>${it.grade }</td> </tr> </c:forEach> </table> <center> <c:if test="${page.currentpage != 1 }"> <a href="${pageContext.request.contextPath }/servlet/PagStudent?currentpage=${page.currentpage-1 }">[上一页]</a> </c:if> <!-- 表里中间位置 ${page.totalpage }--> <c:forEach var="i" begin="1" end="${page.totalpage}" step="1"> <%-- <c:forEach var="i" begin="${i }" end="${(i+8)>page.totalpage?page.totalpage:(i+8) }" step="1"> --%> <!-- 当页数过多时,显示部分页数,随着页数的增大,隐藏前边页数,显示后边的页数 --> <c:if test="${page.currentpage == i }"> ${i } </c:if> <c:if test="${page.currentpage !=i }"> <a href="${pageContext.request.contextPath }/servlet/PagStudent?currentpage=${i }">${i }</a> </c:if> </c:forEach> <c:if test="${page.totalpage != page.currentpage }"> <a href="${pageContext.request.contextPath }/servlet/PagStudent?currentpage=${page.currentpage+1 }">[下一页]</a> </c:if> <br/> 当前${page.currentpage }/总共${page.totalpage } </center> </body> </html>

第四步:创建servlet,里边包含(获取数据,调用逻辑,转发或重定向)

package com.qf.web; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.qf.javabean.Student; import com.qf.service.StudentService; public class PagStudent extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); int currentpage = Integer.parseInt(request.getParameter("currentpage")); // 当第一次访问的时候应该从请求中传递一个页数 // System.out.println(currentpage+"获取了数据"); int pagesize = 5;// 控制每页显示的数据量 try { com.qf.javabean.PagStudent<Student> pagStudent = new StudentService().findPage(currentpage, pagesize); request.getSession().setAttribute("page", pagStudent); System.out.println("我又回到了这个界面"); System.out.println(pagStudent.toString()); List<Student> list = pagStudent.getList(); for (Student student : list) { System.out.println(student); } System.out.println(pagStudent.getPagsize()); System.out.println(pagStudent.getList()); response.sendRedirect(request.getContextPath() + "/StudentPaging.jsp"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); request.getSession().setAttribute("msg", "分页展示数据错误"); response.sendRedirect(request.getContextPath() + "/msg.jsp"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } 第五步:创建一个Servlet包,里边包含两个方法

package com.qf.service; import java.sql.SQLException; import java.util.List; import com.qf.dao.PagDao; import com.qf.javabean.PagStudent; import com.qf.javabean.Student; public class StudentService { public com.qf.javabean.PagStudent<Student> findPage(int currentpage, int pagesize) throws SQLException { PagDao dao= new PagDao(); int totalcount= dao.findcount();//调用两个方法 List<Student> list=dao.findListPage(currentpage,pagesize); // return new PagStudent<Student>(currentpage,pagesize,totalcount,list); } } 第六步:连接数据库的Dao,里边执行SQL语句

package com.qf.dao; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.qf.javabean.Student; import com.qf.utils.DataSourceUtiles; public class PagDao { public int findcount() throws SQLException { // TODO Auto-generated method stub QueryRunner runner= new QueryRunner(); String sql = "select count(*) from student;"; Connection conn = DataSourceUtiles.getConnection(); Object query = runner.query(conn,sql,new ScalarHandler()); //DataSourceUtiles.close(conn); System.out.println("数据库可以走到聚合函数"); return ((Long)query).intValue();//此处的query为long型,转化 } public List<Student> findListPage(int currentpage, int pagesize) throws SQLException { // TODO Auto-generated method stub QueryRunner runner= new QueryRunner(); String sql="select *from student limit ?,?;"; Connection connection = DataSourceUtiles.getConnection(); List<Student> list = runner.query(connection, sql, new BeanListHandler<Student>(Student.class), (currentpage-1)*pagesize,pagesize); System.out.println("这一个数据也可以走到的"); for (Student student : list) { System.out.println(student); } //DataSourceUtiles.close(connection); return list; } }

转载请注明原文地址: https://www.6miu.com/read-1150229.html

最新回复(0)