中软培训-实现分页功能

中软培训-实现分页功能

实体类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&currentPage=1" aria-label="Previous">首页</a></li>  
        
        //通过将当前页码数减一来实现上一页的功能 
    <c:if test="${pageInfo.currentPage!=1}">
     	<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1&currentPage=${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&currentPage=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&currentPage=${i}">${i}</a></li>
    </c:forEach>
        
        //通过将当前页码数加一来实现下一页的功能 
	<c:if test="${pageInfo.currentPage!=pageInfo.totalPage}">
       	<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1&currentPage=${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&currentPage=${pageInfo.totalPage}">下一页</a></li>
     </c:if>
        
        //跳转到总页数相对应的页面
		<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?houseType=1&currentPage=${pageInfo.totalPage}" aria-label="Next">尾页</a></li>
     </ul>
</div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值