天天看点

第十四天,分页

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" target="_blank" rel="external nofollow" >上一页</a></li>
        </c:if>
    
        <c:if test="${pageBean.currPage == 1}">
            <li><a href="/house/searchHouseViewByType.do?currentPage=1&houseType=0" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >上一页</a></li>
        </c:if>
    
        <c:forEach begin="1"  end="${pageBean.totalPage}" var="i">
            <li><a href="/house/searchHouseViewByType.do?currentPage=${i}&houseType=0" target="_blank" rel="external nofollow" >${i}</a></li>
        </c:forEach>
    
        <c:if test="${pageBean.currPage != pageBean.totalPage}">
            <li><a href="/house/searchHouseViewByType.do?currentPage=${pageBean.currPage+1}&houseType=0" target="_blank" rel="external nofollow" >下一页</a></li>
        </c:if>
    
        <c:if test="${pageBean.currPage == pageBean.totalPage}">
            <li><a href="/house/searchHouseViewByType.do?currentPage=${pageBean.totalPage}&houseType=0" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >下一页</a></li>
        </c:if>
    
        <li><a href="/house/searchHouseViewByType.do?currentPage=${pageBean.totalPage}&houseType=0" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  aria-label="Next">尾页</a></li>
    </ul>