昨天給各位總結了本人學習springboot整合mybatis第一階段的一些學習心得和源碼,主要就算是敲了一下SpringBoot的門兒,希望能給各位的入門帶給一點兒捷徑,今天給各位溫習一下MyBatis的分頁插件PageHelper和SpringBoot的集成,它的使用也非常簡單,開發(fā)更為高效。因為PageHelper插件是屬于MyBatis框架的,所以相信很多哥們兒都已經用爛了,下面帶著各位吃一下回頭草。
首先說說MyBatis框架的PageHelper插件吧,它是一個非常好用的分頁插件,通常我們的項目中如果集成了MyBatis的話,幾乎都會用到它,因為分頁的業(yè)務邏輯說復雜也不復雜,但是有插件我們何樂而不為?通常引入它們只需三步驟,不管是Spring集成還是SpringBoot集成都是老套路,我就分開總結了,望各位笑納。
Spring集成PageHelper:
第一步:pom文件引入依賴
1
|
2
3
4
5
|
<!-- mybatis的分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency> |
第二步:MyBatis的核心配置文件中引入配置項
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" " http://mybatis.org/dtd/mybatis-3-config.dtd " > <configuration> <!-- 【mybatis的核心配置文件】 --> <!-- 批量設置別名(可以不配) 作用:就是在mapper.xml文件中直接寫類名,也可以不用寫全路徑名。 --> <typeAliases> < package name= "cn.e3mall.manager.po" /> </typeAliases> <!-- 配置mybatis的分頁插件PageHelper --> <plugins> <!-- com.github.pagehelper為PageHelper類所在包名 --> <plugin interceptor= "com.github.pagehelper.PageHelper" > <!-- 設置數據庫類型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫 --> <property name= "dialect" value= "mysql" /> </plugin> </plugins> </configuration> |
第三步:業(yè)務邏輯實現(xiàn)分頁功能,我們只需將當前查詢的頁數page和每頁顯示的總條數rows傳進去,然后Pagehelper已經幫我們分好數據了,只需在web層獲取數據即可。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//分頁查詢商品列表: @Override public DatagridResult itemList(Integer page, Integer rows) { //為了程序的嚴謹性,判斷非空: if (page == null ){ page = 1 ; //設置默認當前頁 } if (page <= 0 ){ page = 1 ; } if (rows == null ){ rows = 30 ; //設置默認每頁顯示的商品數(因為jsp頁面上默認寫的就是30條) } //1、設置分頁信息,包括當前頁數和每頁顯示的總計數 PageHelper.startPage(page, rows); //2、執(zhí)行查詢 TbItemExample example = new TbItemExample(); List<TbItem> list = tbItemMapper.selectByExample(example); //3、獲取分頁查詢后的數據 PageInfo<TbItem> pageInfo = new PageInfo<>(list); //4、封裝需要返回的分頁實體 DatagridResult result = new DatagridResult(); //設置獲取到的總記錄數total: result.setTotal(pageInfo.getTotal()); //設置數據集合rows: result.setRows(list); return result; } |
springboot集成PageHelper:
第一步:pom文件還是需要引入依賴
1
|
2
3
4
5
|
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version> 4.1 . 6 </version> </dependency> |
第二步:這次直接是在項目的入口類application.java中直接設置PageHelper插件即可
1
|
2
3
4
5
6
7
8
9
10
11
12
|
//配置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; } |
第三步:同理,使用插件實現(xiàn)分頁功能,方式還是一樣,只需將當前查詢的頁數和每頁顯示的條數穿進去即可,直接源碼
這是需要用到的分頁實體,各位可以直接享用。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package com.zxz.utils; /** * 分頁bean */ 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層)。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@Override public List<Item> findItemByPage( int currentPage, int pageSize) { //設置分頁信息,分別是當前頁數和每頁顯示的總記錄數【記住:必須在mapper接口中的方法執(zhí)行之前設置該分頁信息】 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實現(xiàn)) * * @param currentPage :當前頁數 * @param pageSize :每頁顯示的總記錄數 * @return */ @RequestMapping ( "/itemsPage" ) @ResponseBody public List<Item> itemsPage( int currentPage, int pageSize){ return itemService.findItemByPage(currentPage, pageSize); } |
到這兒呢,MyBatis的分頁插件PageHelper就完全和SpringBoot集成到一起了,確實沒有什么新鮮的,標題有個"回頭草"就是這個意思,重點和各位復習一下MyBatis的分頁插件的運用,好久沒用了正好一塊總結一下哈。
原文鏈接:http://www.cnblogs.com/1315925303zxz/p/7364552.html