一.导入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
二.在applicationContext.xml中添加
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
到SqlSessionFactory中bean下
三.修改controller
@RequestMapping("findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "4") int size){
List<UserInfo> infos = userService.findAll(page,size);
PageInfo pageinfo = new PageInfo(infos);
ModelAndView mv=new ModelAndView();
mv.addObject("pageInfos",pageinfo);
mv.setViewName("user-list");
return mv;
}
修改参数,创建PageInfo对象,传入前端
四.前端显示
<tbody>
<c:forEach var="user" items="${pageInfos.list}">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td class="text-center">
<a href="#" class="btn bg-olive btn-xs">更新</a>
<a href="#" class="btn bg-olive btn-xs">删除</a>
<a href="#" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
</tbody>
分页
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=4" aria-label="Previous">首页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfos.pageNum-1}&size=4">上一页</a></li>
<c:forEach begin="1" end="${pageInfos.pages}" var="pageNum">
<li>
<a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageNum}&size=4">${pageNum}</a></li>
</li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfos.pageNum+1}&size=4">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfos.pages}&size=4" aria-label="Next">尾页</a></li>
</ul>
</div>