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

SpringBoot整合MyBatis的分頁外掛PageHelper

來源:http://www.cnblogs.com/1315925303zxz/p/7364552.html

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

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

Spring整合PageHelper:

第一步:pom檔案引入依賴

1 <!-- mybatis的分頁外掛 -->
2 <dependency>
3     <groupId>com.github.pagehelper</groupId>
4     <artifactId>pagehelper</artifactId>
5 </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檔案還是需要引入依賴

1 <dependency>
2      <groupId>com.github.pagehelper</groupId>
3      <artifactId>pagehelper</artifactId>
4      <version>4.1.6</version>
5 </dependency>

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

複製程式碼
 1     //配置mybatis的分頁外掛pageHelper
 2     @Bean
 3     public PageHelper pageHelper(){
 4         PageHelper pageHelper = new PageHelper();
 5         Properties properties = new Properties();
 6         properties.setProperty("offsetAsPageNum","true");
 7         properties.setProperty("rowBoundsWithCount","true");
 8         properties.setProperty("reasonable","true");
 9         properties.setProperty("dialect","mysql");    //配置mysql資料庫的方言
10         pageHelper.setProperties(properties);
11         return pageHelper;
12     }
複製程式碼

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

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

 View Code

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

複製程式碼
 1     @Override
 2     public List<Item> findItemByPage(int currentPage,int pageSize) {
 3         //設定分頁資訊,分別是當前頁數和每頁顯示的總記錄數【記住:必須在mapper介面中的方法執行之前設定該分頁資訊】
 4         PageHelper.startPage(currentPage, pageSize);
 5         
 6         List<Item> allItems = itemMapper.findAll();        //全部商品
 7         int countNums = itemMapper.countItem();            //總記錄數
 8         PageBean<Item> pageData = new PageBean<>(currentPage, pageSize, countNums);
 9         pageData.setItems(allItems);
10         return pageData.getItems();
11     }
複製程式碼  View Code

       到這兒呢,MyBatis的分頁外掛PageHelper就完全和SpringBoot整合到一起了,確實沒有什麼新鮮的,標題有個"回頭草"就是這個意思,重點和各位複習一下MyBatis的分頁外掛的運用,好久沒用了正好一塊總結一下哈。