思路:首先用的是Mysql,主要分页是limit函数,limit第一个参数是从第几条开始(Mysql默认是从0开始),第二个是固定的,每页显示的条数。
PageUtil
属性:pageCount 每页显示的条数 、resultCount 一共有多少条、currentPage 当前页、totalPage 一共有多少页
方法:getCurrent(int currentPage,int pageCount) 用来计算limit的第一个参数,用(当前页数-1)*每页显示的条数
getFooter(int resultCount,int pageCount,int currentPage) 用来显示分页的页脚,控制上一页下一页的显示
package com.liyi.test.util;
public class PageUtil {
/**每页显示的条数**/
private int pageCount;
/**总共的条数**/
private int resultCount;
/**当前页**/
private int currentPage;
/**一共有多少页**/
private int totalPage;
public int getResultCount() {
return resultCount;
}
public void setResultCount(int resultCount) {
this.resultCount = resultCount;
}
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 getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public PageUtil() {
// TODO Auto-generated constructor stub
}
/**
* 计算从第几条开始
* @param currentPage
* @param pageCount
* @return
*/
public int getCurrent(int currentPage,int pageCount){
return (currentPage-1)*pageCount;
}
public String getFooter(int resultCount,int pageCount,int currentPage){
StringBuffer sb = new StringBuffer();
if(resultCount>0&&pageCount>resultCount){
totalPage = 1;
}else if(resultCount>0&&pageCount<resultCount){
totalPage = resultCount%pageCount==0?resultCount/pageCount:resultCount/pageCount+1;
}else if(resultCount==0){
totalPage = 0;
}
sb.append("共"+totalPage+"页").append(" ");
if(currentPage<=totalPage&¤tPage!=1){
sb.append("<a href='javascript:void(0);' id='pre'>上一页</a> ");
}else{
sb.append("上一页 ");
}
sb.append("当前第<b id='current'>"+currentPage+"</b>页 ");
if(currentPage<totalPage){
sb.append("<a href='javascript:void(0);' id='next'>下一页</a>").append(" ");
}else{
sb.append("下一页").append(" ");
}
sb.append("共"+resultCount+"条");
return sb.toString();
}
}
@RequestMapping("/selectUserList")
public ModelAndView selectUserList(HttpServletRequest request,@RequestParam
Map<String,Object> conds){
Map<String,Object> map = new HashMap<String, Object>();
int currentPage =1;
//用于判断是前一页还是下一页
String type = (String) conds.get("type");
//用来接受页面的当前页数,如果初始化是1
String current= (String) conds.get("currentPage");
if(null!=type){
if("1".equals(type)){
if(null!=current){
currentPage =Integer.valueOf(current)+1;
}
}else if("2".equals(type)){
if(null!=current){
currentPage =Integer.valueOf(current)-1;
}
}
}
//设置每页的条数
int pageCount =3;
map.put("pageCount",pageCount);
//查询出来记录总条数用于分页计算
int count = userService.getTotalPage();
PageUtil page = new PageUtil();
//用于计算Limit 第一个参数 根据当前页数
map.put("currentPage",page.getCurrent(currentPage,pageCount));
//下面页脚
String footer = page.getFooter(count, pageCount, currentPage);
List<UserPO> list = userService.findAll(map);
ModelMap modelMap = new ModelMap();
modelMap.addAttribute("list",list);
modelMap.addAttribute("footer",footer);
ModelAndView modelAndView = new ModelAndView("user/user_list",modelMap);
return modelAndView;
}
$("#next").click(function(){
var current=$("#current").html();
window.location.href="${app}/userController/selectUserList.do?currentPage="+current+'&type=1';
})
$("#pre").click(function(){
var current=$("#current").html();
window.location.href="${app}/userController/selectUserList.do?currentPage="+current+'&type=2';
})