package com.neuedu.java7.baen;
public class Page {
private int currentPage = 1; //当前页数
private int totalPages; //总页数
private int total; //记录总行数
private int pageSize = 5; //每页记录行数
private int nextPage; //下一页
private int prefPage; //前一页
public Page(){
}
public Page(int currentPage, int pageSize) {
this.currentPage = currentPage;
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPages() {
totalPages = total%pageSize == 0?total/pageSize:total/pageSize+1;
return totalPages;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getNextPage() {
if(currentPage<totalPages){
nextPage = currentPage+1;
}else{
nextPage = currentPage;
}
return nextPage;
}
public int getPrefPage() {
if(currentPage>1){
prefPage = currentPage-1;
}else{
prefPage = currentPage;
}
return prefPage;
}
}
//写方法
/**
* 分页查询
*
* @param pagger
* @return
*/
public List<Category> findList(Page page);
/**
* 查询记录总数
*
* @return
*/
public int findTotal();
//写Mapper方法
<select id="findTotal" resultType="int">
select count(t.id) t from
category t
</select>
<select id="findList" parameterType="com.neuedu.java7.baen.Page"
resultMap="CategoryMap">
select * from (select rownum rn, id,cname from category
where rownum <= #{currentPage}*#{pageSize}) where rn >(#{currentPage}-1)*#{pageSize}
</select>
//写Action控制
@RequestMapping("/doAll")
public String findAlls(HttpServletRequest request) {
Page page = new Page();
int count = categoryService.findTotal();
page.setTotal(count);
request.getSession().setAttribute("page", page);
List<Category> category = categoryService.findList(page);
request.getSession().setAttribute("category", category);
return "list";
}
@RequestMapping("/page")
public String page(Integer pageSize,Integer currentPage,HttpServletRequest request){
Page page = null;
if (request.getSession().getAttribute("page") !=null) {
page = (Page) request.getSession().getAttribute("page");
}else {
page = new Page();
}
if (pageSize!=null) {
page.setPageSize(pageSize);
}
if (currentPage!=null) {
page.setCurrentPage(currentPage);
}
List<Category> category = categoryService.findList(page);
request.setAttribute("category", category);
return "list";
}
//写页面编码
<tr>
<td align="left" colspan="2">每页显示<select name="pageSize" id="pageSize" style="background-color: #FFFF33" onchange="toPage()">
<option value="5" ${page.pageSize==5?"selected='selected'":"" }>5</option>
<option value="10" ${page.pageSize==10?"selected='selected'":"" }>10</option>
<option value="20" ${page.pageSize==20?"selected='selected'":"" }>20</option>
</select>条</td>
<td align="right" colspan="3"><a href="../category/page.do?currentPage=1">[首页]</a>
<a href="../category/page.do?currentPage=${page.prefPage }">前一页</a>
<a href="../category/page.do?currentPage=${page.nextPage }">后一页</a>
<a href="../category/page.do?currentPage=${page.totalPages }">[尾页]</a></td>
</td>
</tr>