通用分页
Posted bf6rc9qu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通用分页相关的知识,希望对你有一定的参考价值。
通用分页核心思路:
将上一次查询请求再发一次,只是当前页变了而已。
首页 1 2 3 4 … 100 末页 跳转
mysql分页:select * from 表 limit 3,3
每页显示记录数 自己设置
当前页 来自前端
总页数 总记录数%每页显示记录数==0?总记录数/每页显示记录数:总记录数/每页显示记录数+1
总记录数 数据库统计count()
每页起始记录数 =(当前页-1)*每页显示记录数+1
总共101条记录,每页显示10条
第一页: 1-10
第二页: 11-20
第三页: 21-30
每页结尾记录数 =当前页*每页显示记录数
1、通用的查询方法代码实现
导入jar包
连接数据库之前查看自己的用户名、密码、数据库名是否正确
Book.java
package com.huangyucan.util; public class PageBean private int page = 1;// 页码 private int rows = 10;// 页大小 private int total = 0;// 总记录数 private boolean pagination = true;// 是否分页 public PageBean() super(); public int getPage() return page; public void setPage(int page) this.page = page; public int getRows() return rows; public void setRows(int rows) this.rows = rows; public int getTotal() return total; public void setTotal(int total) this.total = total; public void setTotal(String total) this.total = Integer.parseInt(total); public boolean isPagination() return pagination; public void setPagination(boolean pagination) this.pagination = pagination; /** * 获得起始记录的下标 * * @return */ public int getStartIndex() return (this.page - 1) * this.rows; @Override public String toString() return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
BookDao.java
package com.huangyucan.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.zking.pageBean.entity.Book; import com.zking.pageBean.util.BaseDao; import com.zking.pageBean.util.BaseEntityDao; import com.zking.pageBean.util.DBAccess; import com.zking.pageBean.util.PageBean; import com.zking.pageBean.util.StringUtils; public class BookDao extends BaseEntityDao<Book> // /** // * // * @param book 带条件查询需要用上 // * @param pageBean 分页 // * @return // * @throws SQLException // */ // public List<Book> list(Book book,PageBean pageBean) throws SQLException // List<Book> list = new ArrayList<>(); // String sql = "select * from t_mvc_book where true"; // if(StringUtils.isNotBlank(book.getBname())) // sql += " and bname like ‘%"+book.getBname()+"%‘"; // //// System.out.println(sql); // Connection con = DBAccess.getConnection(); // PreparedStatement pst = con.prepareStatement(sql); // ResultSet rs = pst.executeQuery(); // while(rs.next()) // list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price"))); // // return list; // // /** // * oop改造后的方法 // * @param book // * @param pageBean // * @return // * @throws SQLException // * @throws IllegalAccessException // * @throws InstantiationException // */ // public List<Book> list(Book book,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException // String sql = "select * from t_mvc_book where true"; // if(StringUtils.isNotBlank(book.getBname())) // sql += " and bname like ‘%"+book.getBname()+"%‘"; // // return executeQuery(sql, pageBean, new CallBack() // @Override // public List<Book> foreach(ResultSet rs) throws SQLException // List<Book> list = new ArrayList<>(); // while(rs.next()) // list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price"))); // // return list; // // ); // /** * 利用反射对查询进行二次增强 * @param book * @param pageBean * @return * @throws SQLException * @throws InstantiationException * @throws IllegalAccessException */ public List<Book> list(Book book,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException String sql = "select * from t_mvc_book where true"; if(StringUtils.isNotBlank(book.getBname())) sql += " and bname like ‘%"+book.getBname()+"%‘"; return executeQuery(sql, pageBean, Book.class);
BaseDao
package com.huangyucan.util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.zking.pageBean.entity.Book; public class BaseDao<T> /** * 借鉴ajax的回调函数现象 * 在调用方去处理返回的结果集 * @author Administrator * */ public abstract class CallBack public abstract List<T> foreach(ResultSet rs) throws SQLException, InstantiationException, IllegalAccessException; /** * 利用oop思想对原有的查询方法向上抽取共性部分 * @param sql * @param pageBean * @return * @throws SQLException * @throws IllegalAccessException * @throws InstantiationException */ public List<T> executeQuery(String sql, PageBean pageBean, CallBack callBack) throws SQLException, InstantiationException, IllegalAccessException List<T> list = new ArrayList<>(); Connection con = DBAccess.getConnection(); String countSql = getCountSql(sql); PreparedStatement pst = con.prepareStatement(countSql); ResultSet rs = pst.executeQuery(); if(rs.next()) pageBean.setTotal(rs.getString(1)); DBAccess.close(null, pst, rs); String pageSql = getPageSql(sql, pageBean); PreparedStatement psmt = con.prepareStatement(pageSql); ResultSet rst = psmt.executeQuery(); return callBack.foreach(rst); /** * 获取分页的sql语句 * @param sql 符合条件的查询语句 * @param pageBean 分页的实体类 * @return */ private String getPageSql(String sql,PageBean pageBean) return sql + " limit "+ pageBean.getStartIndex() +"," + pageBean.getRows(); /** * 获取符合查询条件的总记录数的sql * @param sql * @return */ private String getCountSql(String sql) return "select count(*) from ("+ sql +") t";
分页工具类
PageBase
package com.huangyucan.util; /** * 分页工具类 * */ public class PageBean private int page = 1;// 页码 private int rows = 10;// 页大小 private int total = 0;// 总记录数 private boolean pagination = true;// 是否分页 public PageBean() super(); public int getPage() return page; public void setPage(int page) this.page = page; public int getRows() return rows; public void setRows(int rows) this.rows = rows; public int getTotal() return total; public void setTotal(int total) this.total = total; public void setTotal(String total) this.total = Integer.parseInt(total); public boolean isPagination() return pagination; public void setPagination(boolean pagination) this.pagination = pagination; /** * 获得起始记录的下标 * * @return */ public int getStartIndex() return (this.page - 1) * this.rows; @Override public String toString() return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
以上是关于通用分页的主要内容,如果未能解决你的问题,请参考以下文章