Day11
ssm分页
page实体类
public class PageInfo<T> {
private int currentPage; //当前页
private int pageSize; //一页多少个
private List<T> lists; //分页内容
private int totalPage; //共多少页
private int totalCount; //共多少条数据
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 +
'}';
}
}
dao接口
需要用到两个接口
public interface IHouseDao {
int SelectCount(int houseType);
List<HouseView> HouseMoreAndPage(HashMap<String,Object> map);
}
service层
public interface IHouseService {
PageInfo<HouseView> HouseMore(int currentPage , int houseType);
}
service实体类
@Service
public class HouseServiceImpl implements IHouseService {
@Autowired
private IHouseDao houseDao;
@Override
public PageInfo<HouseView> HouseMore(int currentPage,int houseType) {
//新建page实体类,给所有属性赋值
HashMap<String, Object> map = new HashMap<>();
PageInfo<HouseView> pageInfo = new PageInfo<>();
pageInfo.setCurrentPage(currentPage);
pageInfo.setPageSize(5);
pageInfo.setTotalCount(houseDao.SelectCount(houseType));
pageInfo.setTotalPage((int) Math.ceil(Double.valueOf(pageInfo.getTotalCount())/pageInfo.getPageSize()));
map.put("start",(currentPage-1)*pageInfo.getPageSize());
map.put("size",pageInfo.getPageSize());
map.put("houseType",houseType);
pageInfo.setLists(houseDao.HouseMoreAndPage(map));
return pageInfo;
}
}
controller层
@Controller
@RequestMapping("house")
public class HouseController {
@Autowired
private IHouseService houseService;
@RequestMapping("HouseMore.do")
public ModelAndView HouseMore(@RequestParam(value = "currentPage",defaultValue = "1",required = false)int currentPage
,@RequestParam(value = "houseType",defaultValue = "0",required = false)int houseType){
PageInfo<HouseView> pageInfo = houseService.HouseMore(currentPage,houseType);
for (HouseView house : pageInfo.getLists()){
long createTime = house.getCreateTime();
String date = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(date);
String ct = simpleDateFormat.format(new Date(createTime));
house.setDate(ct);
}
List<HouseView> randomhouse = houseService.RandomHouse();
ModelAndView mv = new ModelAndView();
mv.addObject("houses",pageInfo.getLists());
mv.addObject("randomHouses",randomhouse);
mv.addObject("pageInfo",pageInfo);
if(houseType==0)
mv.setViewName("newhouse");
else if(houseType==1)
mv.setViewName("oldhouse");
else if(houseType == 2)
mv.setViewName("renthouse");
return mv;
}
}
mapper
<mapper namespace="com.gread.dao.IHouseDao" >
<select id="SelectCount" resultType="int" parameterType="int">
select count(*) from tb_house,tb_house_info,tb_user
where tb_house.house_id = tb_house_info.house_id
and tb_user.user_id = tb_house.user_id
and tb_house.is_delete = 0
and tb_house.house_type = #{houseType}
</select>
<select id="HouseMoreAndPage" resultType="com.gread.pojo.HouseView" parameterType="Map">
select * from tb_house,tb_house_info,tb_user
where tb_house.house_id = tb_house_info.house_id
and tb_user.user_id = tb_house.user_id
and tb_house.is_delete = 0
and tb_house.house_type = #{houseType}
order by tb_house.house_id limit #{start},#{size}
</select>
</mapper>
jsp
<div class="pull-right">
<ul class="pagination">
<li><a href="/house/HouseMore.do?houseType=0" aria-label="Previous">首页</a></li>
<c:if test="${pageInfo.currentPage>1}">
<li><a href="/house/HouseMore.do?currentPage=${pageInfo.currentPage-1}&houseType=0">上一页</a></li>
</c:if>
<c:forEach begin="1" end="${pageInfo.totalPage}" var="page">
<li><a href="/house/HouseMore.do?currentPage=${page}&houseType=0">${page}</a></li>
</c:forEach>
<c:if test="${pageInfo.currentPage<3}">
<li><a href="/house/HouseMore.do?currentPage=${pageInfo.currentPage+1}&houseType=0">下一页</a></li>
</c:if>
<li><a href="/house/HouseMore.do?currentPage=${pageInfo.totalPage}&houseType=0" aria-label="Next">尾页</a></li>
</ul>
</div><!--底部分页栏-->