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>