通用分页核心思路:
将上一次查询请求再发一次,只是当前页变了而已。
1.完成基本的数据绑定
搭建一个web项目,实现数据的完整绑定
1.1 创建数据库
1.2 创建动态web项目,并创建好相应包:
web :—>Servlet
dao 第一次查满足条件的总记录数
第二次查指定页码并满足条件的记录
二次查询的条件要一致
entity
utils
1.2 前端JSP
bookList.jsp 书籍列表
2.实现基本的数据展示
2.1 创建BaseDao,实现基本数据涉及到泛型,内部静态接口,resultSet转集合类等。
3.分页工具类PageBean
分页三要素:
currentPage 当前页 视图层传递过来
pageSize 每页显示记录数 视图层传递过来
totalCount 总记录数 后台查出来
pagination 是否分页 视图层传递过来
4.JSP分页标签
标签助手类:PageTag
标签描述文件
JSP表单提交分页
- JUnit
java单元测试/白盒测试
setUp
tearDown
测试用例
//
package com.xzy.util;
import java.lang.reflect.Field;
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.xzy.entity.Book;
public class BaseDao<T> {
/**
*
* @param sql 决定查询哪张表的数据
* @param clz 查询出来的数据封装到哪个实体类中
* @param pageBean 决定是否分页
* @return
* @throws IllegalAccessException
* @throws InstantiationException
* @throws SQLException
*/
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
public List<T> executeQuery(String sql, Class clz , PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
List<T> list = new ArrayList<>();
try {
con = DBAccess.getConnection();
if (pageBean != null && pageBean.isPagination()) {
//该分页了
String countSql = getcountSql(sql);
pst = con.prepareStatement(countSql);
rs = pst.executeQuery();
if(rs.next()) {
pageBean.setTotal(rs.getLong(1)+"");
}
String pageSql = getPageSql(sql,pageBean);
pst = con.prepareStatement(pageSql);
rs = pst.executeQuery();
} else {
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
}
while (rs.next()) {
// list.add(new Book(rs.getInt("bid"), rs.getString("bname"), rs.getFloat("price")));
/**
* 1、创建了一个Book对象
* 2、从ResultSet结果集中获取值放入book对象属性中
* 2.1、获取到Book的属性对象
* 2.2、给属性对象赋值
* 3、将已有值的book对象放入list集合中
*/
T t = (T) clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
field.set(t, rs.getObject(field.getName()));
}
list.add(t);
}
} finally {
DBAccess.close(con, pst, rs);
}
return list;
}
/**
* 将原生sql拼接出符合条件的某一页的数据查询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(1) from (" + sql + ") t";
}
}
分页工具类PageBean
package com.zking.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* 分页工具类
*
*/
public class PageBean {
private int page = 1;// 页码
private int rows = 10;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
private String url;
private Map<String, String[]> paramMap = new HashMap<>();
public void setRequest(HttpServletRequest req) {
this.setPage(req.getParameter("page"));
this.setRows(req.getParameter("rows"));
this.setPagination(req.getParameter("pagination"));
// getRequestURL获取到浏览器请求的全路径
this.setUrl(req.getRequestURL().toString());
// getParameterMap可以获取到一次url请求所携带的所有参数
this.setParamMap(req.getParameterMap());
}
public void setPagination(String pagination) {
if (StringUtils.isNotBlank(pagination)) {
this.setPagination(!"false".equals(pagination));
}
}
public void setRows(String rows) {
if (StringUtils.isNotBlank(rows))
this.setRows(Integer.valueOf(rows));
}
public void setPage(String page) {
if (StringUtils.isNotBlank(page)) {
this.setPage(Integer.valueOf(page));
}
}
public PageBean() {
super();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map<String, String[]> getParamMap() {
return paramMap;
}
public void setParamMap(Map<String, String[]> paramMap) {
this.paramMap = paramMap;
}
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 + "]";
}
/**
* 获取到总页数
* @return
*/
public int getMaxPage() {
return this.total % this.rows == 0 ?
this.total / this.rows :
(this.total / this.rows) + 1;
}
/**
* 获取下一页页码
* @return
*/
public int getNextPage() {
return this.page < this.getMaxPage() ? this.page+1 : this.page;
}
/**
* 获取上一页页码
* @return
*/
public int getPreviousPage() {
return this.page > 1 ? this.page-1 : this.page;
}
}