导入jar包
在pom中导入pagehelper包
<!--导入分页jar包-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
配置applicationContext
设置数据库类型mysql,reasonable为true
<!--分页插件-->
<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>
userService
@Override
public List<UserInfo> findAll(int page, int size) {
PageHelper.startPage(page, size);
return userInfoDao.findAll();
}
UserController
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "5") int size) {
ModelAndView mv = new ModelAndView();
List<UserInfo> userInfos = userInfoService.findAll(page, size);
PageInfo pageInfo = new PageInfo(userInfos);
mv.addObject("pageInfo", pageInfo);
mv.setViewName("user-list");
return mv;
}
user-list
<div class="box-body">
<!-- 数据表格 -->
<div class="table-box">
<!--数据列表-->
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px"><input
id="selall" type="checkbox" class="icheckbox_square-blue">
</th>
<th class="sorting_asc">ID</th>
<th class="sorting_desc">用户名</th>
<th class="sorting_asc sorting_asc_disabled">密码</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${pageInfo.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="${pageContext.request.contextPath}/user/toUpdate.do?id=${user.id}"
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>
</table>
<!--数据列表/-->
</div>
<!-- 数据表格 /-->
</div>
<!-- /.box-body -->
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=5"
aria-label="Previous">首页</a></li>
<li>
<a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageNum-1}&size=5">上一页</a>
</li>
<c:forEach begin="1" end="${pageInfo.pages}" var="pageNumber">
<li>
<a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageNumber}&size=5">${pageNumber}</a>
</li>
</c:forEach>
<li>
<a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageNum+1}&size=5">下一页</a>
</li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pages}&size=5"
aria-label="Next">尾页</a></li>
</ul>
</div>