一.总体思路
- 为了提高查询性能及节约网络流量,每次只查询指定的记录数,而不是全部,在数量比较大时很有用
- 当点击下一页或指定页面的数据时,将带着所有的查询条件,再次执行查询
二.分页信息实体(PageBean)
用于存储和传递分页参数,主要内容如下:
- 页码,从页面传递过来
- 每页行数,从也能传递过来
- 总记录数, 从数据库中统计得到
- 是否分页, 如果为false,则查询所有记录
- 查询参数, 点击上一页或下一页时需要及携带用户输入的所有查询参数
- 另外提供上页,下页,总页数等计算
参考实现:
public class PageBean { /** * 页码 */ private int page = 1; /** * 每页显示的记录数 */ private int rows = 10; /** * 总记录数 */ private int total = 0; /** * 是否分页 */ private boolean pagination = true; /** * 记录查询的url,以便于点击分页时再次使用 */ private String url; /** * 存放请求参数,用于生成隐藏域中的元素 */ private Map<String,String[]> parameterMap; /** * 根据传入的Request初始化分页对象 * @param request */ public void setRequest(HttpServletRequest request) { if(!StringUtils.isNullOrEmpty(request.getParameter("page"))) { this.page = Integer.valueOf(request.getParameter("page")); } if(!StringUtils.isNullOrEmpty(request.getParameter("rows"))) { this.rows = Integer.valueOf(request.getParameter("rows")); } if(!StringUtils.isNullOrEmpty(request.getParameter("pagination"))) { this.pagination = Boolean.valueOf(request.getParameter("pagination")); } this.url = request.getRequestURI(); this.parameterMap = request.getParameterMap(); request.setAttribute("pageBean", this); } 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 boolean isPagination() { return pagination; } public void setPagination(boolean pagination) { this.pagination = pagination; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Map<String, String[]> getParameterMap() { return parameterMap; } public void setParameterMap(Map<String, String[]> parameterMap) { this.parameterMap = parameterMap; } //计算起始页码 public int getStartIndex() { return (this.page - 1) * this.rows; } //获取总页数 public int getTotalPage() { if (this.getTotal() % this.rows == 0) { return this.getTotal() / this.rows; } else { return this.getTotal() / this.rows + 1; } } //上一页 public int getPreviousPage() { return this.page - 1 > 0 ? this.page - 1 : 1; } //下一页 public int getNextPage() { return this.page + 1 > getTotalPage() ? getTotalPage() : this.page + 1; } }
三.后台分页数据查询
3.1 处理流程
- 查询满足条件的总记录数
- 查询满足条件的当前页的数据
- 上两个步骤的查询条件要一致
流程图:
3.2 实现
- 为简化数据库操作,需要一个DBUtil工具类
public final class DBUtil { private static String DRIVER_NAME = "com.mysql.jdbc.Driver"; private static String DB_URL = "jdbc:mysql://localhost:3306/t234?useUnicode=true&characterEncoding=utf-8&useSSL=false"; private static String DB_USER = "root"; private static String DB_PASSWORD = "123456"; private DBUtil() { } static { try { Class.forName(DRIVER_NAME); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConection() throws SQLException { Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); return connection; } public static void closeDB(ResultSet rs, Statement ps, Connection con) { try { if (rs != null && !rs.isClosed()) { rs.close(); } if (ps != null && !ps.isClosed()) { ps.close(); } if(con != null && !con.isClosed()) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void closeDB(ResultSet rs, Statement ps) { try { if (rs != null && !rs.isClosed()) { rs.close(); } if (ps != null && !ps.isClosed()) { ps.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
- 分页查询
public class StudentDao { public List<Student> getStudents(String sname, PageBean pageBean) { String sql = "select * from t_student t "; if (!Objects.isNull(sname) && sname.length() > 0) { sql += " where t.sname like ?"; } List<Student> students = new ArrayList<>(); Connection con = null; PreparedStatement ps = null; ResultSet rs = null; /* * 如果不需要分页,则直接查询查询,并返回查询结果。 */ if (pageBean == null || !pageBean.isPagination()) { //3. try { con = DBUtil.getConection(); ps = con.prepareStatement(sql); //设置查询参数 if(sname != null) { ps.setObject(1, sname+"%"); } rs = ps.executeQuery(); //1. 差异化部分 while(rs.next()) { Student stu = new Student(); stu.setSid(rs.getInt("sid")); stu.setSname(rs.getString("sname")); stu.setAge(rs.getInt("age")); stu.setRemark(rs.getString("remark")); students.add(stu); } } catch (Exception e) { e.printStackTrace(); //2. } finally { DBUtil.closeDB(rs, ps, con); } return students; } else { /* * 如果需要分页,则分两步操作: * 1. 查询总记录数 * 2. 查询当前页记录 */ //1. 生成统计总记录数的SQL, 查询总记录数 try { String countSql = "select count(*) from (" + sql + ") tmp"; con = DBUtil.getConection(); ps = con.prepareStatement(countSql); //设置查询参数 if(sname != null) { ps.setObject(1, sname+"%"); } rs = ps.executeQuery(); while(rs.next()) { pageBean.setTotal(rs.getInt(1)); } /* * 如果统计的总记录数为0,则表示没有符合条件的记录,直接返回一个空结果集即可。 */ if(pageBean.getTotal() == 0) { return students; } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.closeDB(rs, ps); } /* * 生成分页sql,查询当前页的数据 */ try { String pagingSql = sql + " limit " + pageBean.getStartRow() + ", " + pageBean.getRows(); ps = con.prepareStatement(pagingSql); //设置查询参数 if(sname != null) { ps.setObject(1, sname+"%"); } rs = ps.executeQuery(); //差异化 while(rs.next()) { Student stu = new Student(); stu.setSid(rs.getInt("sid")); stu.setSname(rs.getString("sname")); stu.setAge(rs.getInt("age")); stu.setRemark(rs.getString("remark")); students.add(stu); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.closeDB(rs, ps, con); } return students; } } public static void main(String[] args) { StudentDao dao = new StudentDao(); List<Student> students = dao.getStudents("张", new PageBean()); students.forEach(s -> System.out.println(s)); } }
Student实体,及对应的数据库表可自行准备
四.重构-提取公用方法
1.为了进行公共方法的抽取,需要找出上面实习中的可通用部分,和差异化部分。
- 只要是分页,就会统计总记录数,而总记录数的统计是在业务sql外封装了一个select count(*)是有规律可循的,可以通用
- 只要是分页,则封装分页sql也是有规律可循的(在业务sql后加limit子句即可),可以通用
- 因为每个查询对应的业务实体(即模型)不同,所以ORM映射部分不能通用
2.公用方法封装思路
- 将可通用的部分封装到模板中
- 差异化部分(即不可通用部分),可以定义一个处理接口,以便于通过参数传入个性化的实现部分
- 具体实现
通用分页查询模板类:
public final class DBTemplate { private DBTemplate() { } public static interface IORMConvert<T> { List<T> convert(ResultSet rs) throws SQLException; } public static <T> List<T> query(String sql, Object[] params, PageBean pageBean, IORMConvert<T> convert) { List<T> datas = new ArrayList<>(); Connection con = null; PreparedStatement ps = null; ResultSet rs = null; //不需要分页 if (pageBean == null || !pageBean.isPagination()) { try { con = DBUtil.getConection(); ps = con.prepareStatement(sql); setParam(params, ps); rs = ps.executeQuery(); datas = convert.convert(rs); return datas; } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.closeDB(rs, ps, con); } } else { //1. 查询总记录数 //2. 查询当前页数据 //1. 生成统计总记录数的SQL, 查询总记录数 try { String countSql = "select count(*) from (" + sql + ") tmp"; con = DBUtil.getConection(); ps = con.prepareStatement(countSql); setParam(params, ps); rs = ps.executeQuery(); while(rs.next()) { pageBean.setTotal(rs.getInt(1)); } /* * 如果统计的总记录数为0,则表示没有符合条件的记录,直接返回一个空结果集即可。 */ if(pageBean.getTotal() == 0) { return datas; } } catch (Exception e) { e.printStackTrace(); } finally { if(pageBean.getTotal() == 0) { DBUtil.closeDB(rs, ps, con); } DBUtil.closeDB(rs, ps); } //查询当前页数据 try { String pagingSql = sql + " limit " + pageBean.getStartRow() + ", " + pageBean.getRows(); ps = con.prepareStatement(pagingSql); setParam(params, ps); rs = ps.executeQuery(); datas = convert.convert(rs); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.closeDB(rs, ps, con); } } return datas; } private static void setParam(Object[] params, PreparedStatement ps) throws SQLException { if (params != null) { int i = 1; for (Object param : params) { ps.setObject(i, param); i++; } } } }
使用示例:
public class StudentDao2 { public List<Student> getStudents(String sname, PageBean pageBean) { String sql = "select * from t_student where sname like ?"; return DaoTemplate.query(sql, new Object[] {sname}, pageBean, new IORMConvert<Student>() { @Override public List<Student> convert(ResultSet rs) throws SQLException { List<Student> stus = new ArrayList<>(); while(rs.next()) { Student stu = new Student(); stu.setSid(rs.getInt("sid")); stu.setSname(rs.getString("sname")); stu.setAge(rs.getInt("age")); stu.setRemark(rs.getString("remark")); stus.add(stu); } return stus; } }); } public static void main(String[] args) { StudentDao2 dao = new StudentDao2(); PageBean pageBean = new PageBean(); pageBean.setPage(3); List<Student> students = dao.getStudents("张%", pageBean); students.forEach(s -> System.out.println(s)); } }