day11 ssm分页

1.pageInfo 当前页 ,每页条数, 总条数 ,总页数 ,List,

2.dao a.查询总条数 count(*) b.查询分页后的需要的数据(limit)

3.service 把得到的数据 封装到pageInfo对象里面

定义pageInfo类

public class PageInfo<T> {//T为泛型
    private int currentPage;
    private int pageSize;
    private List<T> lists;
    private int totalPage;
    private int totalCount;
。。。。//get set 方法  和 toString
}

dao

和之前差不多

public interface IHouseDao {
	List<HouseView> searchHouseByTypeAndPage(HashMap<String,Object> map);
}

IService

编写方法 参数为currentPage 和 houseType

public interface IHouseService {
    PageInfo<HouseView> searchHouseByType(int currentPage, int houseType);
}

HouseService

实现方法

@Service
public class HouseServiceImpl implements IHouseService {

    @Autowired
    IHouseDao houseDao;

    @Override
    public PageInfo<HouseView> searchHouseByType(int currentPage,int houseType) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("size",5);
        PageInfo<HouseView> pageInfo = new PageInfo<>();
        pageInfo.setCurrentPage(currentPage);
        pageInfo.setPageSize(5);
        int count = houseDao.selectCount(houseType);
        Double c = Double.valueOf(count);
        Double num = Math.ceil(c / pageInfo.getPageSize());//向上取整 1.2 = 2, 1.3 = 2
        pageInfo.setTotalPage(num.intValue());
        pageInfo.setTotalCount(count);
        map.put("start",(currentPage-1)*pageInfo.getPageSize());
        map.put("houseType",houseType);

        List<HouseView> houseViews = houseDao.searchHouseByTypeAndPage(map);
        pageInfo.setLists(houseViews);
        return pageInfo;
    }

}

HouseController

编写.do

   

@RequestMapping("searchHouseByType.do")
    public ModelAndView searchHouseByType(@RequestParam(value = "currentPage",defaultValue = "1",
            required = false)int currentPage, int houseType){
        if(currentPage<1){
            currentPage=1;
        }
        PageInfo<HouseView> pageInfo= houseService.searchHouseByType(currentPage,houseType);//转换类型  方便页面显示
        List<HouseView> randomHouse = houseService.searchThreeRandomHouse(houseType);
        for (HouseView houseView :pageInfo.getLists()) {
            long createTime = houseView.getCreateTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String ct = format.format(new Date(createTime));
//            System.out.println(ct);
            houseView.setDate(ct);
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("randomHouses",randomHouse);
        if(houseType == 0){
            mv.setViewName("newhouse");
        }else if(houseType == 1){
            mv.setViewName("oldhouse");
        }else{
            mv.setViewName("renthouse");
        }
        return mv;
    }

JSP页面

<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?currentPage=1&houseType=0" aria-label="Previous">首页</a></li>
<c:if test="${pageInfo.currentPage <= 1}" var="flag">
    <li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?currentPage=1&houseType=0">上一页</a></li>
</c:if>
<c:if test="${!flag}">
    <li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?currentPage=${pageInfo.currentPage-1}&houseType=0">上一页</a></li>
</c:if>
<c:forEach begin="1" end="${pageInfo.totalPage}" var="i">
    <li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?currentPage=${i}&houseType=0">${i}</a></li>
</c:forEach>
<c:if test="${pageInfo.currentPage != pageInfo.totalPage}">
    <li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?currentPage=${pageInfo.currentPage+1}&houseType=0">下一页</a></li>
</c:if>
<c:if test="${pageInfo.currentPage == pageInfo.totalPage}">
    <li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?currentPage=${pageInfo.totalPage}&houseType=0">下一页</a></li>
</c:if>
<li><a href="${pageContext.request.contextPath}/house/searchHouseByType.do?currentPage=${pageInfo.totalPage}&houseType=0" aria-label="Next">尾页</a></li>
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值