一.配置mybaits的PageHelper插件有兩種方式:
先引入pageHelper插件
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
1.SSM整合中 mybaits配置已經集成到了spring的配置文件中,所以配置SpringSessionFactory時候可以指定插件:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--數據源--><property name="dataSource" ref="dataSource"></property><!--mybatis的其他配置 --><property name="plugins"><array><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><props><!-- 分頁的相關配置參數 詳細參數解析見附錄 --><prop key="helperDialect">oracle</prop></props></property></bean></array></property></bean>
2.配置一個mybaits的SqlMapConfig.xml
<configuration><plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"/></plugins></configuration>
好處是:不需要指定數據庫方言了,mybatis會自動選擇;
二.PageHelper的API解析
public PageInfo<Product> findByPageHelper(Integer currPage, Integer pageSize) {//開始分頁PageHelper.startPage(currPage, pageSize);//查詢全部List<Product> productList = productDao.findAll();//創建PageInfo對象PageInfo<Product> pageInfo = new PageInfo<>(productList, 5);return pageInfo;
}
三.
pageInfo是PageHelper定義的實體類,成員屬性:
pageInfo.getPageNum():當前頁
pageInfo.getPageSize():每頁條數
pageInfo.getPages():總頁數
pageInfo.getTotal():總條數
pageInfo.getList():數據
pageInfo.getPrePage():上一頁
pageInfo.getNextPage():下一頁
pageInfo.isIsFirstPage():是否第一頁
pageInfo.isIsLastPage():是否最后一頁
pageInfo.getNavigateFirstPage():展示的第一個頁面
pageInfo..getNavigateLastPage():展示的最后一個頁碼
需要注意的是關與select中option子標簽的設置
option有一個屬性:selected:
selected屬性:規定在頁面加載時預先選定該選項,被預選的選項會顯示在下拉列表的最前面的位置,
也可以在頁面加載后通過Js設置selected;
當selected="selected"或者selected="true"當前的option就會默認在刷新后顯示最前面稱為select的value值;
實例:
<%--默認選中每頁條數--%>
$("#pageSize option[value=${pageInfo.pageSize}]").prop("selected","selected");
<!--secript 代碼-->
<script type="text/javascript"> // 分頁 $("#pageSize option[value=${pageInfo.pageSize}]").prop("selected","selected"); function gotoPage(currPage) { // 獲取每頁顯示條數 var pageSize = $("#pageSize").val(); if(currPage<1){ return; } if(currPage>${pageInfo.pages}){ return; } location.href="${pageContext.request.contextPath}/product/findAll?currPage="+currPage+"&pageSize="+pageSize; }
</script>
<div class="box-tools pull-right">
<ul class="pagination">
<%--在超鏈接中訪問js函數 必須添加前綴 javascript--%>
<li><a href="javascript:gotoPage(1)" aria-label="Previous">首頁</a></li>
<li><a href="javascript:gotoPage(${pageInfo.prePage})">上一頁</a></li>
<c:forEach begin="${pageInfo.navigateFirstPage}" end="${pageInfo.navigateLastPage}" var="i">
<li><a href="javascript:gotoPage(${i})">${i}</a></li>
</c:forEach>
<li><a href="javascript:gotoPage(${pageInfo.nextPage})">下一頁</a></li>
<li><a href="javascript:gotoPage(${pageInfo.pages})" aria-label="Next">尾頁</a></li>
</ul>
</div>
Controller 層
// PageHelper 分頁助手查詢 //查詢全部 RequestParam 請求參數 // value 指定頁面參數的名稱 // required 是否必要有參數 @RequestMapping("/findAll") public ModelAndView findAll(@RequestParam(value = "currPage",defaultValue = "2") Integer currPage, @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize){ // 准備數據 PageInfo<Product> pageInfo= productService.findByPageHelper(currPage,pageSize); // 創建ModelAndView 對象 ModelAndView modelAndView = new ModelAndView(); // 添加數據 modelAndView.addObject("pageInfo",pageInfo); // 指定頁面 modelAndView.setViewName("product-list"); return modelAndView; }
servrice 層和serviceImp層
//根據分頁助手查詢
PageInfo<Product> findByPageHelper(Integer currPage,Integer pageSize);
public PageInfo<Product> findByPageHelper(Integer currPage, Integer pageSize) { //指定分頁的參數 PageHelper.startPage(currPage, pageSize); //查詢全部 List<Product> productList = productDao.findAll(); //創建PageInfo對象 PageInfo<Product> productPageInfo = new PageInfo<>(productList,3); return productPageInfo; }