一.导入MyBatis分页插件相关jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.7</version>
</dependency>
二.在service层写入对象
需要传入当前页面和页面大小
PageHelper.startPage(currPage,pageSize);
@Override
public List<Shopp> selectShoppByPage(int currPage,int pageSize) {
// MyBatis分页插件
PageHelper.startPage(currPage,pageSize);
List<Shopp> shopps = shoppDao.selectShoppByPage();
return shopps;
}
三.在Controller层上加入对象
PageInfo pageInfo=new PageInfo(shopps,5);//5代表的是页面数
@RequestMapping("query")
public String query(HttpServletRequest request){
String curr=request.getParameter("curr");
String pageSize=request.getParameter("pageSize");
if (curr==null||curr==""){
curr="1";
}
if (pageSize==null||pageSize==""){
pageSize="8";
}
List<Shopp> shopps = shopSercice.selectShoppByPage(Integer.parseInt(curr), Integer.parseInt(pageSize));
PageInfo pageInfo=new PageInfo(shopps,5);//页面数
request.setAttribute("shopp",pageInfo);
return "index";
}
四.在SqlMapConfig.xml里配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--显示Sql语句-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--MyBatis分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"></property>
</plugin>
</plugins>
</configuration>
五.前端用BootStrap分页样式
<%--商品展示--%>
<div style="width: 1150px;height: 500px;margin-left: 250px;margin-top: 10px;border: white 1px solid" >
<c:forEach items="${shopp.list}" var="shop">
<div style="width: 250px;height: 250px;margin-left: 10px" id="show">
<%--<span>${shopp.id}</span>--%>
<span><img src="${path}${shop.img}" style="width: 250px;height: 150px"></span>
<span>商品名:${shop.name}</span><br>
<span>价格:${shop.price}</span><br>
<span>数量:${shop.p_small}</span><br>
<input type="button" value="添加购物车" onclick="addCar(${shop.id})"class="btn btn-success">
</div>
</c:forEach>
<%--分页信息--%>
<div style="border: red 1px solid;margin-top: 500px">
当前第 ${shopp.pageNum} 页.总共 ${shopp.pages} 页.一共 ${shopp.total} 条记录
</div>
<div style="margin-top: 500px;margin-left: 200px">
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="${path}/user/query?curr=1">首页</a></li>
<!--上一页-->
<li>
<%--hasPreviousPage 如果有上一页--%>
<c:if test="${shopp.hasPreviousPage}">
<a href="${path}/user/query?curr=${shopp.pageNum-1}"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</c:if>
</li>
<!--循环遍历连续显示的页面,若是当前页就高亮显示,并且没有链接-->
<%--navigatepageNums 导航页码数 --%>
<c:forEach items="${shopp.navigatepageNums}" var="page_num">
<c:if test="${page_num == shopp.pageNum}">
<li class="active"><a href="#">${page_num}</a></li>
</c:if>
<c:if test="${page_num != shopp.pageNum}">
<li>
<a href="${path}/user/query?curr=${page_num}">${page_num}</a>
</li>
</c:if>
</c:forEach>
<!--下一页-->
<li>
<c:if test="${shopp.hasNextPage}">
<a href="${path}/user/query?curr=${shopp.pageNum+1}"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</c:if>
</li>
<li><a href="${path}/user/query?curr=${shopp.pages}">尾页</a></li>
</ul>
</nav>
</div>