第十四天,分页

int selectCountByType(int houseType);

List searchHouserByPage(HashMap<String,Object> map);

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 rand() LIMIT 4 select count(*) 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} 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 limit #{start},#{size}

@Override
public PageBean searchHouserByType(int currentPage,int houseType) {
HashMap<String,Object> map = new HashMap<String,Object>();
PageBean pageBean = new PageBean();
//封装当前页数
pageBean.setCurrPage(currentPage);
//每页显示的数据
int pageSize=5;
pageBean.setPageSize(pageSize);
//封装总记录数
int totalCount = houseDao.selectCountByType(houseType);
pageBean.setTotalCount(totalCount);
//封装总页数
double tc = totalCount;
Double num =Math.ceil(tc/pageSize);//向上取整
pageBean.setTotalPage(num.intValue());
map.put(“start”,(currentPage-1)*pageSize);
map.put(“size”, pageBean.getPageSize());
map.put(“houseType”,houseType);
//封装每页显示的数据
List houseViews = houseDao.searchHouserByPage(map);
pageBean.setLists(houseViews);
return pageBean;
}

public class PageBean {
private int currPage;//当前页数

private int pageSize;//每页显示的记录数

private int totalCount;//总记录数

private int totalPage;//总页数

private List<T> lists;//每页的显示的数据

@RequestMapping(“searchHouseViewByType.do”)
public ModelAndView searchHouseViewByType(@RequestParam(value = “currentPage”, defaultValue = “1”, required = false) int currentPage, int houseType) {
PageBean pageBean = houseService.searchHouserByType(currentPage, houseType);
for (HouseView houseView : pageBean.getLists()) {
long createTime = houseView.getCreateTime();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String ct = sdf.format(new Date(createTime));
houseView.setDate(ct);
}
List randomHouse = houseService.searchThreeRandomHouse(houseType);

ModelAndView mv = new ModelAndView();
mv.addObject("pageBean", pageBean);
mv.addObject("randomHouses", randomHouse);
if (houseType == 0) {
    mv.setViewName("newhouse");
} else if (houseType == 1) {
    mv.setViewName("oldhouse");
} else if (houseType == 2) {
    mv.setViewName("renthouse");
}
return mv;

}

<%–分页按钮–%>

  • 首页
  •     <c:if test="${pageBean.currPage != 1}">
            <li><a href="/house/searchHouseViewByType.do?currentPage=${pageBean.currPage-1}&houseType=0">上一页</a></li>
        </c:if>
    
        <c:if test="${pageBean.currPage == 1}">
            <li><a href="/house/searchHouseViewByType.do?currentPage=1&houseType=0">上一页</a></li>
        </c:if>
    
        <c:forEach begin="1"  end="${pageBean.totalPage}" var="i">
            <li><a href="/house/searchHouseViewByType.do?currentPage=${i}&houseType=0">${i}</a></li>
        </c:forEach>
    
        <c:if test="${pageBean.currPage != pageBean.totalPage}">
            <li><a href="/house/searchHouseViewByType.do?currentPage=${pageBean.currPage+1}&houseType=0">下一页</a></li>
        </c:if>
    
        <c:if test="${pageBean.currPage == pageBean.totalPage}">
            <li><a href="/house/searchHouseViewByType.do?currentPage=${pageBean.totalPage}&houseType=0">下一页</a></li>
        </c:if>
    
        <li><a href="/house/searchHouseViewByType.do?currentPage=${pageBean.totalPage}&houseType=0" aria-label="Next">尾页</a></li>
    </ul>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值