分页功能的实现

it2023-01-23  56

分页功能

属性封装分页工具类统计总记录数分页查询功能的实现

属性

private int totalCount;//总记录数 private int pageSize;//页面行数 private int pageCount;//页面总数 private int currentPageIndex;//当前页 private boolean hasPrevious;//上一页; private boolean hasNext;//下一页; private int beginRow;//起始行;

封装分页工具类

package com.neusoft.utils; public class PageInfo { private int totalCount;//总记录数 private int pageSize;//页面行数 private int pageCount;//总页数 private int currentPageIndex=1;//当前页 private int beginRow;//起始行 private boolean hasPrevious;//上一页 private boolean hasNext;//下一页 public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /*计算总页数*/ public int getPageCount() { if (totalCount%pageSize==0){ pageCount=totalCount/pageSize; }else { pageCount=(totalCount/pageSize)+1; } return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getCurrentPageIndex() { return currentPageIndex; } public void setCurrentPageIndex(int currentPageIndex) { this.currentPageIndex = currentPageIndex; } /*计算起始行*/ public int getBeginRow() { beginRow=(currentPageIndex-1)*pageSize; return beginRow; } public void setBeginRow(int beginRow) { this.beginRow = beginRow; } /*判断是否有上一页*/ public boolean isHasPrevious() { hasPrevious = currentPageIndex==1?false:true; return hasPrevious; } public void setHasPrevious(boolean hasPrevious) { this.hasPrevious = hasPrevious; } /*判断是否有下一页*/ public boolean isHasNext() { hasNext=currentPageIndex==pageCount?false:true; return hasNext; } public void setHasNext(boolean hasNext) { this.hasNext = hasNext; } }

统计总记录数

UserDaoImpl:

@Override public int getTotalCount() { int result=0; try { conn = DBUtils.getConn(); ps = conn.prepareStatement("select count(*) from t_user"); rs = ps.executeQuery(); if (rs.next()){ result = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); }finally { DBUtils.closeAll(rs,ps,conn); } return result; }

分页查询功能的实现

public List<User> pageQueryAll(PageInfo pageInfo) { List<User> users = new ArrayList<>(); User user = null; try { conn = DBUtils.getConn(); ps = conn.prepareStatement("select * from t_user limit ?,?"); ps.setInt(1,pageInfo.getBeginRow()); ps.setInt(2,pageInfo.getPageSize()); rs = ps.executeQuery(); while(rs.next()){ user = new User(); user.setUserName(rs.getString("user_name")); users.add(user); } } catch (SQLException e) { e.printStackTrace(); } return users; }
最新回复(0)