中軟培訓-實現分頁功能
實體類pojo
public class PageInfo<T> {
private int currentPage; //當前頁面
private int pageSize; //一個頁面顯示幾個房型
private List<T> lists; //數組集合
private int totalPage; //總頁數
private int totalCount; //查到的內容的總數
//實體的 Get Set方法和toString方法
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<T> getLists() {
return lists;
}
public void setLists(List<T> lists) {
this.lists = lists;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
@Override
public String toString() {
return "PageInfo{" +
"currentPage=" + currentPage +
", pageSize=" + pageSize +
", lists=" + lists +
", totalPage=" + totalPage +
", totalCount=" + totalCount +
'}';
}
}
ServiceImpl部分代碼
@Override
public PageInfo<HouseView> searchHouseByType(int currentPage, int houseType) {
HashMap<String,Object> map = new HashMap<>();
map.put("size",5);//設置每一頁的數量爲5
PageInfo<HouseView> pageInfo = new PageInfo<>();
pageInfo.setCurrentPage(currentPage); //設置當前頁爲第一頁 從JSP頁面傳了當前頁爲1
pageInfo.setPageSize(5); //傳給PageInfo每一頁的內容數量爲5
int count = houseDao.selectCount(houseType); //通過mapper來實現內容的總數
pageInfo.setTotalCount(count); //傳給PageInfo內容的總數
//start爲每次開始查詢的內容 第一頁從1開始第二頁就從6開始
map.put("start",(currentPage-1)*pageInfo.getPageSize());//設置開始查詢的內容
map.put("houseType",houseType);//設置房型
List<HouseView> houseViews = houseDao.searchHouseByTypeAndPage(map);
pageInfo.setLists(houseViews);
Double c = Double.valueOf(count);
Double totalPage = Math.ceil(c/pageInfo.getPageSize());
pageInfo.setTotalPage(totalPage.intValue());
return pageInfo;
}
Mapper頁面代碼 關鍵詞 limit
<select id="searchHouseByTypeAndPage" resultType="com.yiju.pojo.HouseView">
select * from tb_house a,tb_house_info b,tb_user c where a.is_delete=0 and a.house_id=b.house_id and
a.user_id=c.user_id AND a.house_type=#{houseType}
order by a.house_id desc limit #{start},#{size}
</select>
jsp部分
<div class="pull-right">
<ul class="pagination">
//判斷是否爲第一頁
<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1¤tPage=1" aria-label="Previous">首頁</a></li>
//通過將當前頁碼數減一來實現上一頁的功能
<c:if test="${pageInfo.currentPage!=1}">
<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1¤tPage=${pageInfo.currentPage-1}">上一頁</a></li>
</c:if>
//用c:if來判斷當前頁在首頁時不再向上一頁
<c:if test="${pageInfo.currentPage==1}">
<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1¤tPage=1">上一頁</a></li>
</c:if>
//使用 c:forEach 來呈現頁碼的數量
<c:forEach begin="1" end="${pageInfo.totalPage}" var="i">
<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1¤tPage=${i}">${i}</a></li>
</c:forEach>
//通過將當前頁碼數加一來實現下一頁的功能
<c:if test="${pageInfo.currentPage!=pageInfo.totalPage}">
<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1¤tPage=${pageInfo.currentPage+1}">下一頁</a></li>
</c:if>
//用c:if來判斷當前頁在尾頁時不再向下一頁
<c:if test="${pageInfo.currentPage==pageInfo.totalPage}">
<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1¤tPage=${pageInfo.totalPage}">下一頁</a></li>
</c:if>
//跳轉到總頁數相對應的頁面
<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1¤tPage=${pageInfo.totalPage}" aria-label="Next">尾頁</a></li>
</ul>
</div>