//page实体类
package com.bw.util;
/**
* @author Administrator
* 分页信息类
*/
public class Page
{
private int pageSize; //每页显示记录数
private int totalCount; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private int beginIndex; //查询起始点
private boolean hasPrePage; //是否有上一页
private boolean hasNextPage; //是否有下一页
//自定义构造
public Page(int pageSize, int totalCount, int totalPage, int currentPage,
int beginIndex, boolean hasPrePage, boolean hasNextPage)
{
this.pageSize = pageSize; //每页记录数
this.totalCount = totalCount; //总记录数
this.totalPage = totalPage; //总页数
this.currentPage = currentPage; //当前页
this.beginIndex = beginIndex; //起始页数
this.hasPrePage = hasPrePage; //是否有上一页
this.hasNextPage = hasNextPage; //是否有下一页
}
//setter和getter方法
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;
}
public int getTotalPage()
{
return totalPage;
}
public void setTotalPage(int totalPage)
{
this.totalPage = totalPage;
}
public int getCurrentPage()
{
return currentPage;
}
public void setCurrentPage(int currentPage)
{
this.currentPage = currentPage;
}
public int getBeginIndex()
{
return beginIndex;
}
public void setBeginIndex(int beginIndex)
{
this.beginIndex = beginIndex;
}
public boolean isHasPrePage()
{
return hasPrePage;
}
public void setHasPrePage(boolean hasPrePage)
{
this.hasPrePage = hasPrePage;
}
public boolean isHasNextPage()
{
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage)
{
this.hasNextPage = hasNextPage;
}
}
```pageutil工具类
package com.bw.util;
/**
* @author Administrator
* 分页辅助类开发
*/
public class PageUtil{
/**
* 得到封装好所有分页信息的Page对象
* @param pageSize 每页记录数
* @param totalCount 总记录数
* @param currentPage 当前页
* @return
*/
//总页数=总记录数/总页数
public static Page createPage(int pageSize, int totalCount, int currentPage)
{
pageSize = getPageSize(pageSize);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(pageSize, totalCount);
int beginIndex = getBeginIndex(pageSize, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(pageSize, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
//得到每页显示记录数,默认显示10条
public static int getPageSize(int pageSize)
{
return pageSize == 0 ? 10:pageSize;
}
//获得当前页,默认为1
public static int getCurrentPage(int currentPage)
{
return currentPage == 0 ? 1:currentPage;
}
//获得总页数
public static int getTotalPage(int pageSize, int totalCount)
{
int totalPage = 0;
if(totalCount != 0 && totalCount % pageSize == 0)
{
totalPage = totalCount / pageSize;
}else
{
totalPage = totalCount / pageSize + 1;
}
return totalPage;
}
//获得起始位置
public static int getBeginIndex(int pageSize, int currentPage)
{
return (currentPage - 1) * pageSize;
}
//是否有上一页
public static boolean getHasPrePage(int currentPage)
{
return currentPage == 1 ? false:true;
}
//是否有下一页
public static boolean getHasNextPage( int totalPage, int currentPage)
{
return currentPage == totalPage || totalPage == 0 ? false:true;
}
}
<div class="se-preview-section-delimiter"></div>
这里写代码片
```mapper接口
public List<User> findbypage(Map<String,Object> params);
<div class="se-preview-section-delimiter"></div>
这里写代码片
```mapper.xml
<select id="findbypage" parameterType="java.util.Map"
resultType="user">
select id,username,password from t_user limit
#{page.beginIndex},#{page.pageSize}
</select>
<div class="se-preview-section-delimiter"></div>
这里写代码片
```controller
@RequestMapping(value="list.action")
public String pagelist(Model model,Integer pageSize,Integer currentPage){
if(pageSize == null){
pageSize = 4;
}
if(currentPage == null){
currentPage = 1;
}
Map<String,Object> params = new HashMap<String,Object>();
int totalCount = service.count();
Page page = PageUtil.createPage(pageSize, totalCount, currentPage);
params.put("page",page);
List<User> list = service.findbypage(params);
model.addAttribute("list", list);
model.addAttribute("page", page);
return "list.jsp";
}