1. PapeHelper简介
PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持众多常用的数据库,例如mysql、oracle、mariaDB、DB2、SQLite、Hsqldb等。
2. PageHelper使用
2.1 在maven的pom.xml 中引入pageHelper的坐标
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
2.2 配置
第一种方式是 在 MyBatis 的配置文件SqlMapConfig.xml 中配置拦截器插件
<plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="oracle"/> <property name="reasonable" value="true"/> </plugin> </plugins>
第二种方式是在 Spring 配置文件applicationContext.xml中配置拦截器插件
<bean id="sqlSessionFactory" 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> <!--当该参数设置为 true 时, pageNum<=0 时会查询第一页, pageNum>pages (超过总数时),会查询最后一页。--> <prop key="reasonable">true</prop> </props> </property> </bean> </array> </property> </bean>
2.3 在service层中,调用dao层之前调用PageHelper.startPage() 方法
@Override public List<Orders> findAll(int pageNum,int pageSize) { PageHelper.startPage(pageNum,pageSize); return ordersDao.findAll(); }
2.4 在controller层,使用PageInfo 对查询到的list 集合数据进行包装
@Controller @RequestMapping("/orders") public class OrdersController { @Autowired private OrdersService ordersService; @RequestMapping("/findAll") public ModelAndView findAll(@RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,@RequestParam(name = "pageSize",required = true,defaultValue = "5")int pageSize){ ModelAndView modelAndView = new ModelAndView(); List<Orders> ordersList = ordersService.findAll(pageNum,pageSize); PageInfo pageInfo = new PageInfo(ordersList); modelAndView.addObject("pageInfo",pageInfo); modelAndView.setViewName("orders-list"); return modelAndView; } }
2.5 在orders-list.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_desc">订单编号</th> <th class="sorting_asc sorting_asc_disabled">产品名称</th> <th class="sorting_desc sorting_desc_disabled">金额</th> <th class="sorting">下单时间</th> <th class="text-center sorting">订单状态</th> <th class="text-center">操作</th> </tr> </thead> <tbody> <c:forEach items="${pageInfo.list}" var="orders"> <tr> <td><input name="ids" type="checkbox"></td> <td>${orders.id }</td> <td>${orders.orderNum }</td> <td>${orders.product.productName }</td> <td>${orders.product.productPrice }</td> <td>${orders.orderTimeStr }</td> <td class="text-center">${orders.orderStatusStr }</td> <td class="text-center"> <button type="button" class="btn bg-olive btn-xs">订单</button> <button type="button" class="btn bg-olive btn-xs" onclick="location.href='${pageContext.request.contextPath}/orders/findById.do?id=${orders.id}'"> 详情 </button> <button type="button" class="btn bg-olive btn-xs">编辑</button> </td> </tr> </c:forEach> </tbody> </table>
<!--分页条--> <div class="box-tools pull-right"> <ul class="pagination"> <li> <a href="${pageContext.request.contextPath}/orders/findAll?pageNum=1&pageSize=${pageInfo.pageSize}" aria-label="Previous">首页</a> </li> <li> <a href="${pageContext.request.contextPath}/orders/findAll?pageNum=${pageInfo.pageNum-1}&pageSize=${pageInfo.pageSize}">上一页</a> </li> <c:forEach begin="1" end="${pageInfo.pages}" var="pageNum"> <c:if test="${pageNum==pageInfo.pageNum}"> <li class="active"> <a href="${pageContext.request.contextPath}/orders/findAll?pageNum=${pageNum}&pageSize=${pageInfo.pageSize}">${pageNum}</a> </li> </c:if> <c:if test="${pageNum!=pageInfo.pageNum}"> <li> <a href="${pageContext.request.contextPath}/orders/findAll?pageNum=${pageNum}&pageSize=${pageInfo.pageSize}">${pageNum}</a> </li> </c:if> </c:forEach> <li> <a href="${pageContext.request.contextPath}/orders/findAll?pageNum=${pageInfo.pageNum+1}&pageSize=${pageInfo.pageSize}">下一页</a> </li> <li> <a href="${pageContext.request.contextPath}/orders/findAll?pageNum=${pageInfo.pages}&pageSize=${pageInfo.pageSize}" aria-label="Next">尾页</a> </li> </ul> </div>