中软培训-实现分页功能
实体类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>