1. 程式人生 > >Spring,SpringBoot 整合 MyBatis 的分頁外掛 PageHelper

Spring,SpringBoot 整合 MyBatis 的分頁外掛 PageHelper

SPRING BOOT, Spring整合 MyBatis 的分頁外掛 PageHelper

原創   2018-04-03  宗野  

   昨天給各位總結了本人學習springboot整合mybatis第一階段的一些學習心得和原始碼,主要就算是敲了一下SpringBoot的門兒,希望能給各位的入門帶給一點兒捷徑,今天給各位溫習一下MyBatis的分頁外掛PageHelper和SpringBoot的整合,它的使用也非常簡單,開發更為高效。因為PageHelper外掛是屬於MyBatis框架的,所以相信很多哥們兒都已經用爛了,下面帶著各位吃一下回頭草。

       首先說說mybatis框架的pageHelper的外掛吧,它是一個非常好用的分頁外掛,通常我們的專案中如果集成了MyBatis的話,幾乎都會用到它,因為分頁的業務邏輯說複雜也不復雜,但是有外掛我們何樂而不為?通常引入它們只需三步驟,不管是Spring整合還是SpringBoot整合都是老套路,總結如下

Spring整合PageHelper:

第一步:pom檔案引入依賴

 <!-- mybatis的分頁外掛 -->
         <dependency>
         	
<groupId>com.github.pagehelper</groupId>           <artifactId>pagehelper</artifactId>           <version>4.1.4</version>          </dependency>

第二步:MyBatis的核心配置檔案中引入配置項

複製程式碼 複製程式碼
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration  3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  4 "http://mybatis.org/dtd/mybatis-3-config.dtd">  5  6 <configuration>  7 <!-- 【mybatis的核心配置檔案】 -->  8  9 <!-- 批量設定別名(可以不配) 作用:就是在mapper.xml檔案中直接寫類名,也可以不用寫全路徑名。 --> 10 <typeAliases> 11 <package name="cn.e3mall.manager.po" /> 12 </typeAliases> 13 14 <!-- 配置mybatis的分頁外掛PageHelper --> 15 <plugins> 16 <!-- com.github.pagehelper為PageHelper類所在包名 --> 17 <plugin interceptor="com.github.pagehelper.PageHelper"> 18 <!-- 設定資料庫型別Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫 --> 19 <property name="dialect" value="mysql"/> 20 </plugin> 21 </plugins> 22 23 </configuration>
複製程式碼 複製程式碼

第三步:業務邏輯實現分頁功能,我們只需將當前查詢的頁數page和每頁顯示的總條數rows傳進去,然後Pagehelper已經幫我們分好資料了,只需在web層獲取資料即可。

複製程式碼 複製程式碼
 1 //分頁查詢商品列表:
 2     @Override
 3 public DatagridResult itemList(Integer page, Integer rows) {  4 //為了程式的嚴謹性,判斷非空:  5 if(page == null){  6 page = 1; //設定預設當前頁  7  }  8 if(page <= 0){  9 page = 1; 10  } 11 if(rows == null){ 12 rows = 30; //設定預設每頁顯示的商品數(因為jsp頁面上預設寫的就是30條) 13  } 14 15 //1、設定分頁資訊,包括當前頁數和每頁顯示的總計數 16  PageHelper.startPage(page, rows); 17 //2、執行查詢 18 TbItemExample example = new TbItemExample(); 19 List<TbItem> list = tbItemMapper.selectByExample(example); 20 //3、獲取分頁查詢後的資料 21 PageInfo<TbItem> pageInfo = new PageInfo<>(list); 22 //4、封裝需要返回的分頁實體 23 DatagridResult result = new DatagridResult(); 24 //設定獲取到的總記錄數total: 25  result.setTotal(pageInfo.getTotal()); 26 //設定資料集合rows: 27  result.setRows(list); 28 29 return result; 30 }

springboot整合PageHelper:

第一步:pom檔案還是需要引入依賴

 <dependency>
      <groupId>com.github.pagehelper</groupId>  <artifactId>pagehelper</artifactId>  <version>4.1.6</version>  </dependency>

第二步:這次直接是在專案的入口類application.java中直接設定PageHelper外掛即可

複製程式碼
      //配置mybatis的分頁外掛pageHelper
      @Bean
 public PageHelper pageHelper(){  PageHelper pageHelper = new PageHelper();  Properties properties = new Properties();  properties.setProperty("offsetAsPageNum","true");  properties.setProperty("rowBoundsWithCount","true");  properties.setProperty("reasonable","true");  properties.setProperty("dialect","mysql"); //配置mysql資料庫的方言   pageHelper.setProperties(properties);  return pageHelper;  }

第三步:同理,使用外掛實現分頁功能,方式還是一樣,只需將當前查詢的頁數和每頁顯示的條數穿進去即可,直接原始碼

這是需要用到的分頁實體,各位可以直接享用。

 package com.zy.adhere.cn.entity;
import java.util.List;
public class PageBean<T> {
	 // 當前頁
	     private Integer currentPage = 1;
	     // 每頁顯示的總條數
	     private Integer pageSize = 10;
	     // 總條數
	     private Integer totalNum;
	     // 是否有下一頁
	     private Integer isMore;
	     // 總頁數
	     private Integer totalPage;
	     // 開始索引
	     private Integer startIndex;
	     // 分頁結果
	     private List<T> items;
	 
	     public PageBean() {
	         super();
	     }
	 
	     public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
	         super();
	         this.currentPage = currentPage;
	         this.pageSize = pageSize;
	         this.totalNum = totalNum;
	         this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize;
	         this.startIndex = (this.currentPage-1)*this.pageSize;
	         this.isMore = this.currentPage >= this.totalPage?0:1;
	     }
	 
	     public Integer getCurrentPage() {
	         return currentPage;
	     }
	 
	     public void setCurrentPage(Integer currentPage) {
	         this.currentPage = currentPage;
	     }
	 
	     public Integer getPageSize() {
	         return pageSize;
	     }
	 
	     public void setPageSize(Integer pageSize) {
	         this.pageSize = pageSize;
	     }
	 
	     public Integer getTotalNum() {
	         return totalNum;
	     }
	 
	     public void setTotalNum(Integer totalNum) {
	         this.totalNum = totalNum;
	     }
	 
	     public Integer getIsMore() {
	         return isMore;
	     }
	 
	     public void setIsMore(Integer isMore) {
	         this.isMore = isMore;
	     }
	 
	     public Integer getTotalPage() {
	         return totalPage;
	     }
	 
	     public void setTotalPage(Integer totalPage) {
	         this.totalPage = totalPage;
	     }
	 
	     public Integer getStartIndex() {
	         return startIndex;
	     }
	 
	     public void setStartIndex(Integer startIndex) {
	         this.startIndex = startIndex;
	     }
	 
	     public List<T> getItems() {
	         return items;
	     }
	 
	     public void setItems(List<T> items) {
	         this.items = items;
	     }
}

分頁功能原始碼(web層和service層)。

    @Override
    public List<Item> findItemByPage(int currentPage,int pageSize) {  //設定分頁資訊,分別是當前頁數和每頁顯示的總記錄數【記住:必須在mapper介面中的方法執行之前設定該分頁資訊】   PageHelper.startPage(currentPage, pageSize);   List<Item> allItems = itemMapper.findAll(); //全部商品  int countNums = itemMapper.countItem(); //總記錄數  PageBean<Item> pageData = new PageBean<>(currentPage, pageSize, countNums);   pageData.setItems(allItems);  return pageData.getItems();  }
  /**
       * 商品分頁功能(整合mybatis的分頁外掛pageHelper實現)
   *   * @param currentPage :當前頁數   * @param pageSize :每頁顯示的總記錄數   * @return  */  @RequestMapping("/itemsPage")   @ResponseBody  public List<Item> itemsPage(int currentPage,int pageSize){  return itemService.findItemByPage(currentPage, pageSize);  }
 

DAO[基於註解式開發]:

Service:


ServiceImpl: