今天小编那写了个分页请大家参考
dao:对数据库进行操作
public List<Product> findProduct(int start, int pageSize)throws SQLException {
//用数据库连接池初始化runner对象
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
//sql进行分页查询
String sql = "select * from product limit ?,?";
return qr.query(sql, new BeanListHandler<Product>(Product.class),
start, pageSize);
}
// 查询商品的总数
public int findTotalCount() throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from product";
return ((Long) qr.query(sql, new ScalarHandler())).intValue();
}
service:进行逻辑判断
private ProductDao pd = new ProductDao();
public List<Product> findProduct(int currentPage, int pageSize) {
try {
//调用dao层方法查出当前页码号即(currentPage - 1) * pageSize
return pd.findProduct((currentPage - 1) * pageSize, pageSize);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//用pagesize做参数计算出最大页码号
public int getMaxPage(int pageSize) {
int totalCount;
try {
totalCount = pd.findTotalCount();
//运用三目运算 如果总数/页数有余就让页码号加一否则不加
return totalCount % pageSize > 0 ? totalCount / pageSize + 1
: totalCount / pageSize;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
servlet:与前台进行交互
private ProductService ps = new ProductService();
private final int pageSize = 10;
public void list(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int currentPage = 1;
if (request.getParameter("currentPage") != null) {
currentPage = Integer.parseInt(request.getParameter("currentPage"));
}
int maxPage = ps.getMaxPage(pageSize);
List<Product> list = ps.findProduct(currentPage, pageSize);
request.setAttribute("list", list);
request.setAttribute("currentPage", currentPage);
request.setAttribute("maxPage", maxPage);
request.getRequestDispatcher("/admin/product/list.jsp").forward(
request, response);
}
前台遍历数据:
<c:forEach items="${list}" var="product">
<tr οnmοuseοver="this.style.backgroundColor = 'white'"
οnmοuseοut="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="18%">${product.pid}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">
<img width="40" height="45" src="${pageContext.request.contextPath}/${product.pimage}"></td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.pname}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.shop_price}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.is_hot}</td>
<td align="center" style="HEIGHT: 22px">
<ahref="${ pageContext.request.contextPath }/product?method=toUpdate&&pid=${product.pid}">
<imgsrc="${pageContext.request.contextPath}/images/i_edit.gif"border="0" style="CURSOR: hand">
</a></td>
<td align="center" style="HEIGHT: 22px">
<a href="${pageContext.request.contextPath}/product?method=delete&&pid=${product.pid}"> <img
src="${pageContext.request.contextPath}/images/i_del.gif"width="16" height="16" border="0"
style="CURSOR: hand">
</a></td>
</tr>
</c:forEach>
Thank you for reading