通过sql语句分页 如:SELECT * FROM users limit 3,2 从第4条记录开始后续显示两行,索引从0开始 (服务器机制)该方式的优缺点如下:
优点:操作便捷(利用服务器内存),适合于大数据量操作,对底层数据库服务器起到保护作用。
缺点:增加了和数据库交互的次数,访问速度较慢。
代码实现如下:
- 首先我们定义一个pageBean用来定义分页的相关信息。包括:pageSize每页显示的条目数,nowPage当前页码,rowCount总行数即总结果集,pageCount总页数,rowDate是list集合用来存放每页显示的结果集。
Java代码
- public class PageBean {
- // 成员变量
- private int pageSize = 0;// 页大小
- private int nowPage = 0;// 当前页面
- private int rowCount = 0;// 总行数
- private int pageCount = 0;// 页数
- private List<?> rowDate = null;
- // 成员函数
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getNowPage() {
- return nowPage;
- }
- public void setNowPage(int nowPage) {
- this.nowPage = nowPage;
- }
- public int getRowCount() {
- return rowCount;
- }
- public void setRowCount(int rowCount) {
- this.rowCount = rowCount;
- }
- public int getPageCount() {
- this.pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
- : rowCount / pageSize + 1;
- return pageCount;
- }
- public List<?> getRowDate() {
- return rowDate;
- }
- public void setRowDate(List<?> rowDate) {
- this.rowDate = rowDate;
- }
- public PageBean(int pageSize, int nowPage, int rowCount, int pageCount,
- List<?> rowDate) {
- super();
- this.pageSize = pageSize;
- this.nowPage = nowPage;
- this.rowCount = rowCount;
- this.pageCount = pageCount;
- this.rowDate = rowDate;
- }
- public PageBean() {
- super();
- // TODO Auto-generated constructor stub
- }
- }
- 在对系统中的每个实体的DAO层实现分页。在该函数中需要pageSize ,nowPage两个参数;foodInfoCount())是用来获取总结果集的行数;该函数返回的是个pageBean对象。
Java代码
- public PageBean foodInfoByPage(int pageSize, int nowPage) {
- PageBean pb = new PageBean();
- pb.setPageSize(pageSize);
- pb.setNowPage(nowPage);
- pb.setRowCount(foodInfoCount());
- //step1:sql
- String sql ="select food_id,food_name,food_price,food_img,food_desc from tb_foodinfo limit "+(nowPage-1)*pageSize +", "+pageSize;//为什么不用?;?支持where
- //step2:执行sql
- ResultSet rs = dbconn.exeStmtS(sql);
- //step3:返回结果
- List<FoodInfo> list = new ArrayList<FoodInfo>();
- try {
- while(rs.next())
- {
- System.out.println(rs.getString(2));
- FoodInfo finfo = new FoodInfo();
- finfo.setFood_id(rs.getInt(1));
- finfo.setFood_name(rs.getString(2));
- finfo.setFood_price(rs.getFloat(3));
- finfo.setFood_img(rs.getString(4));
- finfo.setFood_desc(rs.getString(5));
- list.add(finfo);
- }
- pb.setRowDate(list);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return pb;
- }
- 在业务层中调用DAO层的分页 完成分页的业务逻辑
Java代码
- public class FoodInfoBizImpl implements FoodInfoBiz {
- //声明foodInfoDao对象
- private FoodInfoDao fid = null;
- public FoodInfoBizImpl(){
- fid = new FoodInfoDaoImpl();
- }
- public List<FoodInfo> foodInfoAll() {
- return fid.foodInfoAll();
- }
- public PageBean foodInfoByPage(int pageSize, int nowPage) {
- return fid.foodInfoByPage(pageSize, nowPage);
- }
- }
- 在servlet中的doPost()方法中 控制分页。每页显示的条目数 通过在web.xml 文件中配置参数实现,当前页数通过从jsp视图界面获取,之后将返回的pageBean放入request中
Java代码
- int pageSize = Integer.parseInt(this.getServletConfig().getInitParameter("pageSize"));
- int nowPage = 1;
- if(request.getParameter("np")!=null)
- {
- nowPage = Integer.parseInt(request.getParameter("np"));
- }
- //step2:调用biz得到结果集
- FoodInfoBiz fbiz = new FoodInfoBizImpl();
- PageBean pb = fbiz.foodInfoByPage(pageSize, nowPage);//改变调用方法
- //step3:把结果集存储在媒介【session、request】中 request
- request.setAttribute("pageBean", pb);
- //step4:跳向index.jsp页面[response.sendRedirect - request必杀技]
- request.getRequestDispatcher("index.jsp").forward(request,response);//request跳向方式才能传值
- jsp页面中的实现 通过从request中获取pageBean 将其中的rowDate遍历循环出来 即可实现当前页的显示
Java代码
- <c:forEach items="${pageBean.rowDate}" var="fInfo">
- <td style="background-color:#E3EAEB;">
- <table style="width: 100%">
- <tr>
- <td style="width: 300px">
- <a href="FoodDetail?foodsid=11">
- <img style="border:0px" src="UpFile/foodImage/${fInfo.food_img}" alt="" height="150" width="200"/></a></td>
- <td style="width: 550px; color: #000000; text-align: left">
- 食品名称: ${fInfo.food_name }<br />
- 食品价格: ${fInfo.food_price}<br />
- 所在类别: 热菜<br/>
- 食品介绍: ${fInfo.food_desc}<br />
- <a href="OnePro.htm?pid=1">餐品留言</a>
- <form action="AddCart" method="post" name="form1">
- <input name="txtCount" type="text" style="width:27px;" />
- <input type="hidden" name="foodid" value="${fInfo.food_id }"/>
- <input type="hidden" name="foodname" value="${fInfo.food_name }"/>
- <input type="hidden" name="foodprice" value="${fInfo.food_price }"/>
- <input type="submit" name="ok" value="订餐"/>
- </form>
- </td>
- </tr>
- </table>
- </td>
- </c:forEach>
- 完成当前页的实现 即可来进行上一页 下一页的功能实现 只是传递当前页数即可
Java代码
- <table style="width: 100%">
- <tr>
- <td>
- 一共有<span style="color:Red;">${pageBean.pageCount }</span>页</td>
- <td>
- 每页显示<span style="color:Red;">${pageBean.pageSize }</span>条</td>
- <td> 当前为第<span style="color:Red;">${pageBean.nowPage }</span>页</td>
- <td> 共<span style="color:Red;">${pageBean.rowCount}</span>条</td>
- <td>
- <c:if test="${pageBean.nowPage !=1 }">
- <a href="IndexServlet?np=${pageBean.nowPage-1}"><img src="images/prev.gif" style="border-width:0px;" /></a>
- </c:if>
- <c:if test="${pageBean.nowPage !=pageBean.pageCount }">
- <a href="IndexServlet?np=${pageBean.nowPage+1}"> <img src="images/next.gif" style="border-width:0px;" /></a>
- </c:if>
- </td>
- </tr>
- </table>