Java 分页
分页
1.实体类设计
Page.class
import java.util.List;
/**
* Created by Hjq on 2018/2/1.
*/
public class Page <T> {
private int pageSize = 5;
private int totalCount;
private int totalpage;
private int curenPage = 1;
private int nextpage;
private int prePage;
private int firstpage;
private int lastpage;
private List<T> arrys; //从数据库查询出来的结果集
public List<T> getArrys() {
return arrys;
}
public void setArrys(List<T> arrys) {
this.arrys = arrys;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/**
* 总共分多少页
* @return
*/
public int getTotalpage() {
if(totalCount%pageSize==0)
return totalCount/pageSize;
else
return totalCount/pageSize+1;
}
public int getCurenPage() {
return curenPage;
}
public void setCurenPage(int curenPage) {
this.curenPage = curenPage;
}
/**
* 下一页
* @return
*/
public int getNextpage() {
if(curenPage<this.getTotalpage())
return curenPage+1;
else
return this.getTotalpage();
}
/***
* 上一页
* @param
*/
public int getPrePage() {
if(curenPage>1)
return curenPage-1;
else
return this.getFirstpage();
}
/**
* 首页
* @return
*/
public int getFirstpage() {
return 1;
}
/**
* 尾页
* @return
*/
public int getLastpage() {
return this.getTotalpage();
}
}
2.在对应的Service中添加分页查询
/***
* 分页查询
* @param page
* @return
*/
public Page<TRoomInfo> QueryBypage(Page<TRoomInfo> page) {
String sql = "select count(*) from t_room_info";
int total = dao.executeQueryCount(sql,null);
page.setTotalCount(total);
String sql1 = "select * from t_room_info LIMIT ?,?";
Object[] objects = new Object[]{
(page.getCurenPage() - 1) * page.getPageSize(),
page.getPageSize()
};
List<TRoomInfo> list = dao.executeQuery(sql1,objects);
page.setArrys(list);
return page;
}
3.在servlet中调用
String pageNo = request.getParameter("pageNo");
Page<TRoomInfo> page = new Page<TRoomInfo>();
if(null != pageNo)
{
page.setCurenPage(Integer.valueOf(pageNo));
}
Page<TRoomInfo> pages = service.QueryBypage(page);
request.setAttribute("page",pages);
request.getRequestDispatcher("view/Query_RoomInfo.jsp").forward(request,response);
4.页面显示
<table class="table table-striped table-hover">
<thead>
<tr class="success">
<th>编号</th>
<th>宿舍名字</th>
<th>类型</th>
<th>人数</th>
<th>入住人数</th>
<th>电话</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="room" items="${page.arrys}">
<tr>
<td>${room.room_id}</td>
<td >${room.room_no}</td>
<td>${room.type}</td>
<td>${room.type_num}</td>
<td>${room.num}</td>
<td>${room.phone}</td>
<td>${room.status eq 'E'?"空闲":"满员"}</td>
<td>
<a class="btn btn-default" href="RoomServlet.do?OPER=LIST&method=queryByID&room_id=${room.room_id}">
<span class="glyphicon glyphicon-briefcase"></span> 修改</a>
<a class="btn btn-default" href="RoomServlet.do?OPER=MODIFYROOM&method=del&room_id=${room.room_id}">
<span class="glyphicon glyphicon-trash"></span> 删除</a>
</td>
</tr>
</c:forEach>
</tbody>
<tfoot>
<tr>
<td align="center" colspan="8">
一共有${page.totalCount}条记录,共${page.totalpage}页数,当前[${page.curenPage}/${page.totalpage}]页
<a class="btn btn-default btn-sm" href="RoomServlet.do?OPER=LIST&pageNo=${page.firstpage}">首页</a>
<a class="btn btn-default btn-sm" href="RoomServlet.do?OPER=LIST&pageNo=${page.prePage}">上一页</a>
<c:forEach var="index" begin="1" end="${page.totalpage}" step="1">
<a class="btn btn-default btn-sm" href="RoomServlet.do?OPER=LIST&pageNo=${index}">${index}</a>
</c:forEach>
<a class="btn btn-default btn-sm" href="RoomServlet.do?OPER=LIST&pageNo=${page.nextpage}">下一页</a>
<a class="btn btn-default btn-sm" href="RoomServlet.do?OPER=LIST&pageNo=${page.lastpage}">尾页</a>
</td>
</tr>
</tfoot>
</table>
5.效果图