从开始学习到现在,一直逃不过分页,学习总结如下。
设计思路:
要有一个对象贯穿前后端,包含了当前页面,分页总条数,每页条数等。
流程:页面提交当前页数currentPage-->controller创建一个Page对象,将这个值设置到currentPage上,传给service-->service调用dao,查询当前需要查询的总条数,设置到totalNumber,通过这两个参数计算总页数,和数据库查询用到的两个参数dbIndex,dbEnd,Page对象传给dao-->根据dbIndex,dbEnd分页查询,返回查询结果-->对象通过controller返回到页面,页面根据Page显示总条数,当前页数,总页数等。
注意:分页查询最好加order by,否则按照数据库默认排序,不能保证每次排序方式都是相同的,查询结果会出问题。
Page类
根据当前查询的总条数totalNumber, 每页显示几条pageNumber,准备显示哪一页currentPage,计算出总共有多少页totalNumber, 和数据库查询时需要的两个参数dbIndex,dbEnd
/**
* 分页对应的实体类
*/
public class Page {
/**
* 总条数
*/
private int totalNumber;
/**
* 当前第几页
*/
private int currentPage;
/**
* 总页数
*/
private int totalPage;
/**
* 每页显示条数
*/
private int pageNumber = 10;
/**
* 数据库中limit的参数,从第几条开始取(dbIndex, dbNumber配合在mysql中使用)
*/
private int dbIndex;
/**
* 数据库中limit的参数,一共取多少条
*/
private int dbNumber;
// 取到第几条(dbIndex, dbEnd配合在oracle中使用)
private int dbEnd;
/**
* 根据当前对象中属性值计算并设置相关属性值
*/
public void count() {
// 计算总页数
int totalPageTemp = this.totalNumber / this.pageNumber;
int plus = (this.totalNumber % this.pageNumber) == 0 ? 0 : 1;
totalPageTemp = totalPageTemp + plus;
if(totalPageTemp <= 0) {
totalPageTemp = 1;
}
this.totalPage = totalPageTemp;
// 设置当前页数
// 总页数小于当前页数,应将当前页数设置为总页数
if(this.totalPage < this.currentPage) {
this.currentPage = this.totalPage;
}
// 当前页数小于1设置为1
if(this.currentPage < 1) {
this.currentPage = 1;
}
// 设置limit的参数
this.dbIndex = (this.currentPage - 1) * this.pageNumber;
this.dbNumber = this.pageNumber;
this.dbEnd = this.dbIndex + this.dbNumber;
}
public int getTotalNumber() {
return totalNumber;
}
public void setTotalNumber(int totalNumber) {
this.totalNumber = totalNumber;
this.count();
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
this.count();
}
public int getDbIndex() {
return dbIndex;
}
public void setDbIndex(int dbIndex) {
this.dbIndex = dbIndex;
}
public int getDbNumber() {
return dbNumber;
}
public void setDbNumber(int dbNumber) {
this.dbNumber = dbNumber;
}
public int getDbEnd() {
return dbEnd;
}
public void setDbEnd(int dbEnd) {
this.dbEnd = dbEnd;
}
@Override
public String toString() {
return "Page [totalNumber=" + totalNumber + ", currentPage="
+ currentPage + ", totalPage=" + totalPage + ", pageNumber="
+ pageNumber + ", dbIndex=" + dbIndex + ", dbNumber="
+ dbNumber + ", dbEnd=" + dbEnd + "]";
}
}
jsp
<input type="hidden" name="currentPage" id="currentPage" value="${PAGE.currentPage}"/>
<div class=''>
共 <b>${PAGE.totalNumber}</b> 条
<c:if test="${PAGE.currentPage != 1}">
<a href="javascript:changeCurrentPage('${SHEETID}', '1')" class='first'>首页</a>
<a href="javascript:changeCurrentPage('${SHEETID}', '${PAGE.currentPage-1}')" class='pre'>上一页</a>
</c:if>
当前第<span>${PAGE.currentPage}/${PAGE.totalPage}</span>页
<c:if test="${PAGE.currentPage != PAGE.totalPage}">
<a href="javascript:changeCurrentPage('${SHEETID}', '${PAGE.currentPage+1}')" class='next'>下一页</a>
<a href="javascript:changeCurrentPage('${SHEETID}', '${PAGE.totalPage}')" class='last'>末页</a>
</c:if>
跳至 <input id="currentPageText" type='text' value='${PAGE.currentPage}' class='allInput w28' style="width: 3%;"/> 页
<a href="javascript:changeCurrentPage('${SHEETID}', $('#currentPageText').val())" class='go'>GO</a>
</div>
<script type="text/javascript">
//修改当前页码,调用后台重新查询
function changeCurrentPage(sheetid, currentPage) {
$("#currentPage").val(currentPage);
var currentPageVal = $("#currentPage").val();
console.log("currentPageVal:" + currentPageVal);
// 获取tabIndex
var TABINDEX = $('#TABINDEX').val();
window.location.href = '<%=basePath %>data/findRequireListBySheetId/' + sheetid + '/' + currentPageVal + '/' + TABINDEX;
}
</script>
controller 创建Page对象,设置当前页数
@RequestMapping(value = "/findRequireListBySheetId/{sheetId}/{currentPage}/{tabIndex}", method=RequestMethod.GET)
public String findRequireListBySheetId(@PathVariable String sheetId,
@PathVariable String currentPage, @PathVariable String tabIndex, Map<String, Object> model) {
// 创建分页对象
Page page = new Page();
Pattern pattern = Pattern.compile("[0-9]{1,9}");
// 如果当前页为空,或者不符合正则表达式,就设为第1页
if(currentPage == null || !pattern.matcher(currentPage).matches()) {
page.setCurrentPage(1);
} else {
page.setCurrentPage(Integer.valueOf(currentPage));
}
// 获取requirelist列表
List<Map<String, String>> requireList = dataService.findRequireListBySheetIdPage(sheetId, page);
model.put(ReturnConstant.FIND_REQUIRE_LIST_BY_SHEETID_PAGE, requireList);
model.put(ReturnConstant.PAGE, page);
model.put(ReturnConstant.SHEETID, sheetId);
model.put(ReturnConstant.TABINDEX, tabIndex);
logger.info("当前tabIndex:" + tabIndex);
return "/requireList";
}
service 调用dao
public List<Map<String, String>> findRequireListBySheetIdPage(String sheetId, Page page) {
Map<String,Object> map = new HashMap<String, Object>();
if("null".equals(sheetId)) {
map.put("sheetId", null);
} else {
map.put("sheetId", sheetId);
}
map.put("page", page);
// 分页查询并返回结果
return requireDao.findRequireListBySheetIdPage(map);
}
RequireMapping.xml 没有加分页,分页逻辑在拦截器中实现,见下一节。有排序。
<select id="findRequireListBySheetIdPage" parameterType="java.util.Map" resultType="java.util.HashMap" >
select t1.*, ermuser.realname from ermuser join (
select ermrequire.*, ermusersheet.userid from ermrequire join ermusersheet on
(ermrequire.sheetid = ermusersheet.sheetid)
<choose>
<when test="sheetId != null">
WHERE ermrequire.sheetid = #{sheetId} order by to_number(serialno)
</when>
<otherwise>
order by to_date(ermrequire.requireaccepttime, 'yyyy/mm/dd')
</otherwise>
</choose>
) t1 on (ermuser.userid = t1.userid)
</select>