POJO:
package com.shop.pojo;
import java.io.Serializable;
import java.util.List;
public class PageBean<T> implements Serializable {
// 分頁中的數(shù)據(jù)
private List<T> list;
// 當(dāng)前頁
private Integer currPage;
// 每頁條數(shù)
private Integer pageSize;
// 總頁數(shù)(不能直接設(shè)置,只能通過計(jì)算得出結(jié)果)
private Integer totalPage;
// 總條數(shù)
private Integer totalCount;
// 無參構(gòu)造器(在調(diào)用的時(shí)候,可以直接傳遞值進(jìn)來,然后封裝數(shù)據(jù))
public PageBean(List<T> list, Integer currPage, Integer pageSize, Integer totalCount) {
super();
this.list = list;
this.currPage = currPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
}
public PageBean() {
super();
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public Integer getCurrPage() {
return currPage;
}
public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return (int) Math.ceil((totalCount * 1.0 / pageSize));
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
}
Controller層:
public String findProByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 獲取 cid 和 currPage 參數(shù)
// 當(dāng)獲取到 cid 的時(shí)候,就可以知道是哪個(gè)分類
// 獲取分類下所有的商品
// 商品要展示,先告訴頁面每頁要展示多少條數(shù)據(jù)(寫死)
String cid = request.getParameter("cid");
int currPage = Integer.parseInt(request.getParameter("currPage"));
// 設(shè)置每個(gè)分頁,固定顯示商品的條數(shù)
int pageSize = 12;
// 2. 調(diào)用 ProductService 執(zhí)行獲取分頁數(shù)據(jù),需要返回一個(gè) PageBean
PageBean<Product> pb = null;
try {
pb = ps.findProByPage(currPage, cid, pageSize);
System.out.println("分頁對象:" + pb);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 3. 將結(jié)果放入到 request 域中
request.setAttribute("pb", pb);
// 4. 請求轉(zhuǎn)發(fā)頁面
return "product_list";
}
Service層:
public PageBean<Product> findProByPage(int currPage, String cid, int pageSize) throws Exception {
// 獲取分頁數(shù)據(jù) --- 所有相關(guān)的商品
List<Product> proList = pdao.findProByPage(currPage, cid, pageSize);
// 總數(shù)量
int totalCount = pdao.getTotalCount(cid);
return new PageBean<Product>(proList, currPage, pageSize, totalCount);
}
Dao層:
public class ProductDaoImpl implements ProductDao {
private QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
@Override
public List<Product> findProByPage(int currPage, String cid, int pageSize) throws Exception {
String sql = "select * from product where cid = ? limit ?, ?";
return qr.query(sql, new BeanListHandler<>(Product.class), cid, (currPage - 1) * pageSize , pageSize);
// limit(m, n)
// m 是指記錄開始的 index,從 0 開始,表示第一條記錄
// n 是指從第 m + 1 條開始,取 n 條。每頁固定的條數(shù)。
}
@Override
public int getTotalCount(String cid) throws Exception {
String sql = "select count(*) from product where cid = ?";
return ((Long) qr.query(sql, new ScalarHandler(), cid)).intValue();
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。