SpringBoot集成MyBatis的分頁插件PageHelper實(shí)例代碼
昨天給各位總結(jié)了本人學(xué)習(xí)springboot整合mybatis第一階段的一些學(xué)習(xí)心得和源碼,主要就算是敲了一下SpringBoot的門兒,希望能給各位的入門帶給一點(diǎn)兒捷徑,今天給各位溫習(xí)一下MyBatis的分頁插件PageHelper和SpringBoot的集成,它的使用也非常簡單,開發(fā)更為高效。因?yàn)镻ageHelper插件是屬于MyBatis框架的,所以相信很多哥們兒都已經(jīng)用爛了,下面帶著各位吃一下回頭草。
首先說說MyBatis框架的PageHelper插件吧,它是一個(gè)非常好用的分頁插件,通常我們的項(xiàng)目中如果集成了MyBatis的話,幾乎都會用到它,因?yàn)榉猪摰臉I(yè)務(wù)邏輯說復(fù)雜也不復(fù)雜,但是有插件我們何樂而不為?通常引入它們只需三步驟,不管是Spring集成還是SpringBoot集成都是老套路,我就分開總結(jié)了,望各位笑納。
Spring集成PageHelper:
第一步:pom文件引入依賴
<!-- mybatis的分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency>
第二步:MyBatis的核心配置文件中引入配置項(xiàng)
<?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的核心配置文件】 --> <!-- 批量設(shè)置別名(可以不配) 作用:就是在mapper.xml文件中直接寫類名,也可以不用寫全路徑名。 --> <typeAliases> <package name="cn.e3mall.manager.po" /> </typeAliases> <!-- 配置mybatis的分頁插件PageHelper --> <plugins> <!-- com.github.pagehelper為PageHelper類所在包名 --> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 設(shè)置數(shù)據(jù)庫類型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數(shù)據(jù)庫 --> <property name="dialect" value="mysql"/> </plugin> </plugins> </configuration>
第三步:業(yè)務(wù)邏輯實(shí)現(xiàn)分頁功能,我們只需將當(dāng)前查詢的頁數(shù)page和每頁顯示的總條數(shù)rows傳進(jìn)去,然后Pagehelper已經(jīng)幫我們分好數(shù)據(jù)了,只需在web層獲取數(shù)據(jù)即可。
//分頁查詢商品列表: @Override public DatagridResult itemList(Integer page, Integer rows) { //為了程序的嚴(yán)謹(jǐn)性,判斷非空: if(page == null){ page = 1; //設(shè)置默認(rèn)當(dāng)前頁 } if(page <= 0){ page = 1; } if(rows == null){ rows = 30; //設(shè)置默認(rèn)每頁顯示的商品數(shù)(因?yàn)閖sp頁面上默認(rèn)寫的就是30條) } //1、設(shè)置分頁信息,包括當(dāng)前頁數(shù)和每頁顯示的總計(jì)數(shù) PageHelper.startPage(page, rows); //2、執(zhí)行查詢 TbItemExample example = new TbItemExample(); List<TbItem> list = tbItemMapper.selectByExample(example); //3、獲取分頁查詢后的數(shù)據(jù) PageInfo<TbItem> pageInfo = new PageInfo<>(list); //4、封裝需要返回的分頁實(shí)體 DatagridResult result = new DatagridResult(); //設(shè)置獲取到的總記錄數(shù)total: result.setTotal(pageInfo.getTotal()); //設(shè)置數(shù)據(jù)集合rows: result.setRows(list); return result; }
springboot集成PageHelper:
第一步:pom文件還是需要引入依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
第二步:這次直接是在項(xiàng)目的入口類application.java中直接設(shè)置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數(shù)據(jù)庫的方言 pageHelper.setProperties(properties); return pageHelper; }
第三步:同理,使用插件實(shí)現(xiàn)分頁功能,方式還是一樣,只需將當(dāng)前查詢的頁數(shù)和每頁顯示的條數(shù)穿進(jìn)去即可,直接源碼
這是需要用到的分頁實(shí)體,各位可以直接享用。
package com.zxz.utils; /** * 分頁bean */ import java.util.List; public class PageBean<T> { // 當(dāng)前頁 private Integer currentPage = 1; // 每頁顯示的總條數(shù) private Integer pageSize = 10; // 總條數(shù) private Integer totalNum; // 是否有下一頁 private Integer isMore; // 總頁數(shù) private Integer totalPage; // 開始索引 private Integer startIndex; // 分頁結(jié)果 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) { //設(shè)置分頁信息,分別是當(dāng)前頁數(shù)和每頁顯示的總記錄數(shù)【記?。罕仨氃趍apper接口中的方法執(zhí)行之前設(shè)置該分頁信息】 PageHelper.startPage(currentPage, pageSize); List<Item> allItems = itemMapper.findAll(); //全部商品 int countNums = itemMapper.countItem(); //總記錄數(shù) PageBean<Item> pageData = new PageBean<>(currentPage, pageSize, countNums); pageData.setItems(allItems); return pageData.getItems(); } /** * 商品分頁功能(集成mybatis的分頁插件pageHelper實(shí)現(xiàn)) * * @param currentPage :當(dāng)前頁數(shù) * @param pageSize :每頁顯示的總記錄數(shù) * @return */ @RequestMapping("/itemsPage") @ResponseBody public List<Item> itemsPage(int currentPage,int pageSize){ return itemService.findItemByPage(currentPage, pageSize); }
到這兒呢,MyBatis的分頁插件PageHelper就完全和SpringBoot集成到一起了,確實(shí)沒有什么新鮮的,標(biāo)題有個(gè)"回頭草"就是這個(gè)意思,重點(diǎn)和各位復(fù)習(xí)一下MyBatis的分頁插件的運(yùn)用,好久沒用了正好一塊總結(jié)一下哈。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)
線性表(linear?list)是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列。順序表是常見的線性表之一,本文將詳細(xì)講講順序表的原理與實(shí)現(xiàn),需要的可以參考一下2022-08-08RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報(bào)錯(cuò)404的解決
這篇文章主要介紹了RestFul風(fēng)格 — 使用@PathVariable傳遞參數(shù)報(bào)錯(cuò)404的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot使用Jasypt對配置文件和數(shù)據(jù)庫密碼加密
在做數(shù)據(jù)庫敏感信息保護(hù)時(shí),應(yīng)加密存儲,本文就來介紹一下SpringBoot使用Jasypt對配置文件和數(shù)據(jù)庫密碼加密,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02Java web.xml之contextConfigLocation作用案例詳解
這篇文章主要介紹了Java web.xml之contextConfigLocation作用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法
這篇文章主要介紹了SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06