1.创建pagebean
package com.testonline.controller.bean;
import java.util.List;
public class PageBean<T> {
private int pageNumber; //总记录
private int pageCount; //总页数
private Integer pageIndex; //当前页
private int pageSize; //每页几条数据
private List<T> list; //当前页的数据
public int getPageNumber() { return pageNumber; }
public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; }
public int getPageCount() { return pageCount; }
public void setPageCount(int pageCount) { this.pageCount = pageCount; }
public Integer getPageIndex() { return pageIndex; }
public void setPageIndex(Integer pageIndex) { this.pageIndex = pageIndex; }
public int getPageSize() { return pageSize; }
public void setPageSize(int pageSize) { this.pageSize = pageSize; }
public List<T> getList() { return list; }
public void setList(List<T> list) { this.list = list; }
}
2.在UserMapper.xml中加入sql语句
<!--查一共有多少条数据-->
<select id="countIndex" resultType="int">
select count(*) from user where 1=1
</select>
<!--查用户记录-->
<select id="showlist" parameterType="int" resultMap="BaseResultMap">
select * from user limit #{index},#{pageSize}
</select>
3.在UserMapper.java中加入
//分页显示所有用户信息
List<User> showlist(@Param("index")int index,@Param("pageSize")int pageSize);
//计算数据总数
int countIndex();
注意:若要传两个参数或以上,则要在参数前面要加上@Param(“xxx”)以区分,不然会报错:nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘XXX’ not found
4.在UserService加入
public List<User> showlist(int index,int pageSize);
public int countIndex();
5.在UserServiceImpl加入
//查询用户列表
@Override
public List<User> showlist(int index,int pageSize) {
return usermapper.showlist(index,pageSize);
}
//查询用户数据的总条数
@Override
public int countIndex() {
return usermapper.countIndex();
}
6.在AdminController加入
//分页查询
@RequestMapping("/admin_user")
public String page(HttpServletRequest request,Model model){
//当前页
Integer pageIndex = 1;
//每页记录条数
int pageSize = 3;
//总记录数
int countIndex = userService.countIndex();
//总页数(向上取整)
int pageCount = (int)Math.ceil((double)countIndex/pageSize);
if(request.getParameter("pageIndex")!=null){
pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
}
//index代表偏移量
int index = (pageIndex-1)*pageSize;
//当前页的数据
List<User> list = userService.showlist(index,pageSize);
PageBean<User> pageUtil = new PageBean<User>();
pageUtil.setPageIndex(pageIndex);
pageUtil.setPageCount(pageCount);
pageUtil.setPageNumber(countIndex);
pageUtil.setPageSize(pageSize);
pageUtil.setList(list);
model.addAttribute("pageUtil",pageUtil);
return "/admin_user";
}
7.在JSP中加入
<!--分页-->
<nav aria-label="Page navigation">
<ul class="pagination">
<!--第一页-->
<li>
<c:choose>
<c:when test="${pageUtil.pageIndex - 1 > 0}">
<a href="admin_user?pageIndex=1" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</c:when>
<c:otherwise>
<li class="disabled">
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:otherwise>
</c:choose>
</li>
<!--页数-->
<c:forEach var = "i" begin="1" end="${pageUtil.pageCount}" step="1">
<c:choose>
<c:when test="${pageUtil.pageIndex==i}">
<li class="active">
<a href="admin_user?pageIndex=${i}">${i}</a>
</li>
</c:when>
<c:otherwise>
<li><a href="admin_user?pageIndex=${i}">${i}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
<!--尾页-->
<li>
<c:choose>
<c:when test="${pageUtil.pageIndex < pageUtil.pageCount}">
<a href="admin_user?pageIndex=${pageUtil.pageCount}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</c:when>
<c:otherwise>
<li class="disabled">
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:otherwise>
</c:choose>
</li>
</ul>
</nav>