如何使用PageHelper外掛進行分頁?
阿新 • • 發佈:2020-12-28
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>