分页
原理:
LIMIT 5,5
重构dao实现分页
第一步:添加limit
//Dao中将查询语句后面添加limit 下标,条数,例如
public List<TblKnowledgeVO> findAll() throws Exception{
String sql = "SELECT tbl_knowledge.id,tbl_knowledge.title,tbl_knowledge.content,tbl_knowledge.createDate,tbl_knowledge.isPublish ,tbl_knowledge.fromName,tbl_knowledge.typeId,tbl_type.typeName ,tbl_type.typeDesc FROM tbl_knowledge JOIN tbl_type WHERE tbl_knowledge.typeId = tbl_type.id ORDER BY tbl_knowledge.id LIMIT 5,5";
return super.dbhelper.executeQuery( new TblKnowledgeMapperVO(),sql);
}
查询第二页,每页5条
第二步:将下标和条数设计成方法参数
public List<TblKnowledgeVO> findAll(int pageIndex,int pageSize) throws Exception{
String sql = "SELECT tbl_knowledge.id,tbl_knowledge.title,tbl_knowledge.content,tbl_knowledge.createDate,tbl_knowledge.isPublish ,tbl_knowledge.fromName,tbl_knowledge.typeId,tbl_type.typeName ,tbl_type.typeDesc FROM tbl_knowledge JOIN tbl_type WHERE tbl_knowledge.typeId = tbl_type.id ORDER BY tbl_knowledge.id LIMIT ?,?";
return super.dbhelper.executeQuery( new TblKnowledgeMapperVO(),sql,(pageIndex-1)*pageSize,pageSize);
}
其中pageIndex表示当前页码,就是第几页
pageSize表示页大小,就是一页有几条记录
若查询第pageIndex页,那么第pageIndex页的第一条记录的下标是
(pageIndex-1)*pageSize
第三步:重构service
public List<TblKnowledgeVO> findAll(int pageIndex,int pageSize) throws Exception{
try {
return dao.findAll(pageIndex,pageSize);
} catch (Exception e) {
throw e;
}finally{
dao.close();
}
}
第3.5步:定义PageUtil类封装分页信息
package cn.itlaobing.pager;
import java.util.ArrayList;
import java.util.List;
import cn.itlaobing.vo.TblKnowledgeVO;
public class PageUtil<T> {
private int pageIndex=1;
private int pageSize=5;
private int pageCount;
private int recordCount;
private int nums[]=null;
private List<T> list = new ArrayList<T>();
public int[] getNums() {
return nums;
}
public int getPageIndex() {
return pageIndex;
}
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageCount() {
return (recordCount-1)/pageSize +1 ;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
initNums();
}
private void initNums() {
if(this.getPageCount()>3) {
nums= new int[3];
}else {
nums= new int[this.getPageCount()];
}
int forcount = Math.min(3, this.getPageCount());
for (int i = 1; i <= forcount; i++) {
nums[i-1]= pageIndex -2 + i;
}
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
第四步:重构控制器
try {
PageUtil<TblKnowledgeVO> pageUtil = new PageUtil<TblKnowledgeVO>();
if(request.getParameter("pageIndex")!=null) {
pageUtil.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
}
TblKnowledgeService service = new TblKnowledgeService();
pageUtil = service.findAll(pageUtil);
request.setAttribute("pageUtil", pageUtil);
request.getRequestDispatcher("/knowledge/index.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
第五步:重构视图
<nav aria-label="Page navigation">
<ul class="pagination">
<c:if test="${ pageUtil.pageIndex ne '1' }">
<li>
<a href="${pageContext.request.contextPath }/knowledge/FindAllServlet?pageIndex=${pageUtil.pageIndex-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:forEach items="${pageUtil.nums }" var="item">
<li><a href="${pageContext.request.contextPath }/knowledge/FindAllServlet?pageIndex=${item}">${item}</a></li>
</c:forEach>
<c:if test="${ pageUtil.pageIndex ne pageUtil.pageCount }">
<li>
<a href="${pageContext.request.contextPath }/knowledge/FindAllServlet?pageIndex=${pageUtil.pageIndex+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
</ul>
</nav>