1.pom中导入
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
2.applicationConfig.xml中添加插件
<!--整合myBatis--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--插件--> <property name="plugins"> <array> <!--分页插件--> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置参数,一行配置一个 --> <props> <prop key="helperDialect">oracle</prop> <prop key="reasonable">true</prop> </props> </property> </bean> </array> </property> </bean>
3.web
@RequestMapping("/findPage.do") /* * 分页,默认传第一页,每页显示5条 * */ public ModelAndView finPage(@RequestParam(defaultValue = "1") Integer pageNumber,@RequestParam(defaultValue = "5") Integer pageSize){ System.out.println(pageNumber+"##"+pageSize); PageInfo<Order> pageInfo= orderService.findPage(pageNumber,pageSize); ModelAndView mv=new ModelAndView(); mv.addObject("pageInfo",pageInfo); mv.setViewName("order-list"); return mv; }
4.service
@Service public class OrderServiceImpl implements OrderService { @Autowired private OrderDao orderDao; @Override public List<Order> findAll() { return orderDao.findAll(); } @Override public PageInfo<Order> findPage(Integer pageNumber, Integer pageSize) { PageHelper.startPage(pageNumber,pageSize); List<Order> list = orderDao.findAll();
//导航条长度7 PageInfo<Order> pageInfo=new PageInfo<>(list,7); return pageInfo; } }
5.dao
public interface OrderDao { @Select("select * from orders") @Results(value = { @Result(column = "productId",property = "product",one = @One(select = "lz.dao.ProductDao.findProductByid")) }) public List<Order> findAll(); }
前台jsp页面
<!--数据列表-->
<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">订单号</th>
<th class="sorting">时间</th>
<th class="sorting">出行人数</th>
<th class="sorting">订单描述</th>
<th class="sorting">支付方式</th>
<th class="sorting">状态</th>
<th class="sorting">产品名称</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pageInfo.list}" var="o">
<tr>
<td><input name="ids" type="checkbox"></td>
<%--<td>${o.id}</td>--%>
<td>${o.orderNum}</td>
<td><fmt:formatDate value="${o.orderTime}" pattern="yyyy-MM-dd HH:mm"></fmt:formatDate></td>
<td>${o.peopleCount}</td>
<td>${o.orderDesc}</td>
<td>${o.payType}</td>
<td>${o.orderStatus}</td>
<td>${o.product.productName}</td>
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs"
οnclick='location.href="${pageContext.request.contextPath}/pages/order-show.jsp"'>订单</button>
<button type="button" class="btn bg-olive btn-xs"
οnclick='location.href="${pageContext.request.contextPath}/pages/order-show.jsp"'>查看</button>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<!-- /.box-body -->
<!-- .box-footer-->
<div class="box-footer">
<div class="pull-left">
<div class="form-group form-inline">
总共${pageInfo.pages}页,共${pageInfo.total}条数据。 每页 <select οnchange="sizeChange(this)" class="form-control">
<
<option value="1" <c:if test="${pageInfo.pageSize==1}">selected </c:if> >1</option>
<option value="5" <c:if test="${pageInfo.pageSize==5}">selected </c:if> >5</option>
<option value="10" <c:if test="${pageInfo.pageSize==10}">selected </c:if> >10</option>
</select> 条.
</div>
</div>
<div class="box-tools pull-right">
<ul class="pagination">
<li><a href="javascript:changePage(${pageInfo.navigateFirstPage})" aria-label="Previous">首页</a></li>
<%--第一页时隐藏上一页--%>
<
<script >
alert('${pageInfo.firstPage}');
</script>
<c:if test="${pageInfo.pageNum!=1}">
<li><a href="javascript:changePage(${pageInfo.prePage})" >上一页</a></li>
</c:if>
<c:forEach begin="${pageInfo.navigateFirstPage}" end="${pageInfo.navigateLastPage}" var="a">
<%--当前页加样式--%>
<li><a href="javascript:changePage('${a}')" <c:if test="${pageInfo.pageNum==a}"> style="background-color: #D2E4E8"</c:if> >${a}</a></li>
</c:forEach>
<%--尾页时隐藏最后一页--%>
<c:if test="${pageInfo.pages!=pageInfo.pageNum}">
<li><a href="javascript:changePage(${pageInfo.nextPage})">下一页</a></li>
</c:if>
<li><a href="javascript:changePage(${pageInfo.pages})" aria-label="Next">尾页</a></li>
</ul>
</div>
</div>
<!-- /.box-footer-->
</div>
</section>
<!-- 正文区域 /-->
</div>
<script >
function changePage(pageNumber) {
location.href="${pageContext.request.contextPath}/order/findPage.do?pageSize="+${pageInfo.pageSize} +"&pageNumber="+pageNumber;
}
function sizeChange(size) {
location.href="${pageContext.request.contextPath}/order/findPage.do?pageSize="+size.value;
}
</script>
<!--数据列表--> <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">订单号</th> <th class="sorting">时间</th> <th class="sorting">出行人数</th> <th class="sorting">订单描述</th> <th class="sorting">支付方式</th> <th class="sorting">状态</th> <th class="sorting">产品名称</th> </tr> </thead> <tbody> <c:forEach items="${pageInfo.list}" var="o"> <tr> <td><input name="ids" type="checkbox"></td> <%--<td>${o.id}</td>--%> <td>${o.orderNum}</td> <td><fmt:formatDate value="${o.orderTime}" pattern="yyyy-MM-dd HH:mm"></fmt:formatDate></td> <td>${o.peopleCount}</td> <td>${o.orderDesc}</td> <td>${o.payType}</td> <td>${o.orderStatus}</td> <td>${o.product.productName}</td> <td class="text-center"> <button type="button" class="btn bg-olive btn-xs" οnclick='location.href="${pageContext.request.contextPath}/pages/order-show.jsp"'>订单</button> <button type="button" class="btn bg-olive btn-xs" οnclick='location.href="${pageContext.request.contextPath}/pages/order-show.jsp"'>查看</button> </td> </tr> </c:forEach> </tbody> </table> </div> </div> <!-- /.box-body --> <!-- .box-footer--> <div class="box-footer"> <div class="pull-left"> <div class="form-group form-inline"> 总共${pageInfo.pages}页,共${pageInfo.total}条数据。 每页 <select οnchange="sizeChange(this)" class="form-control"> < <option value="1" <c:if test="${pageInfo.pageSize==1}">selected </c:if> >1</option> <option value="5" <c:if test="${pageInfo.pageSize==5}">selected </c:if> >5</option> <option value="10" <c:if test="${pageInfo.pageSize==10}">selected </c:if> >10</option> </select> 条. </div> </div> <div class="box-tools pull-right"> <ul class="pagination"> <li><a href="javascript:changePage(${pageInfo.navigateFirstPage})" aria-label="Previous">首页</a></li> <%--第一页时隐藏上一页--%> < <script > alert('${pageInfo.firstPage}'); </script> <c:if test="${pageInfo.pageNum!=1}"> <li><a href="javascript:changePage(${pageInfo.prePage})" >上一页</a></li> </c:if> <c:forEach begin="${pageInfo.navigateFirstPage}" end="${pageInfo.navigateLastPage}" var="a"> <%--当前页加样式--%> <li><a href="javascript:changePage('${a}')" <c:if test="${pageInfo.pageNum==a}"> style="background-color: #D2E4E8"</c:if> >${a}</a></li> </c:forEach> <%--尾页时隐藏最后一页--%> <c:if test="${pageInfo.pages!=pageInfo.pageNum}"> <li><a href="javascript:changePage(${pageInfo.nextPage})">下一页</a></li> </c:if> <li><a href="javascript:changePage(${pageInfo.pages})" aria-label="Next">尾页</a></li> </ul> </div> </div> <!-- /.box-footer--> </div> </section> <!-- 正文区域 /--> </div> <script > function changePage(pageNumber) { location.href="${pageContext.request.contextPath}/order/findPage.do?pageSize="+${pageInfo.pageSize} +"&pageNumber="+pageNumber; } function sizeChange(size) { location.href="${pageContext.request.contextPath}/order/findPage.do?pageSize="+size.value; } </script>