使用PageHelper插件實(shí)現(xiàn)Service層分頁(yè)
本文實(shí)例為大家分享了使用PageHelper插件實(shí)現(xiàn)Service層分頁(yè)的具體代碼,供大家參考,具體內(nèi)容如下
使用場(chǎng)景:
平時(shí)分頁(yè)我們可以直接使用mybatis-plus中內(nèi)置的IPage進(jìn)行分頁(yè),一般是在mapper中寫(xiě)好接口,在執(zhí)行sql時(shí)就將其進(jìn)行分頁(yè)操作,但是有些復(fù)雜的查詢(xún)或者是需要拼接返回格式的數(shù)據(jù)就難以操作了,所以我們使用PageHelper插件來(lái)實(shí)現(xiàn)Service分頁(yè)功能。
1.在pom.xml文件中導(dǎo)入PageHelper插件依賴(lài)
<!--pagehelper分頁(yè)插件--> <dependency> ? ? <groupId>com.github.pagehelper</groupId> ? ? <artifactId>pagehelper</artifactId> ? ? <version>4.1.6</version> </dependency>
2.編寫(xiě)PageHelper配置類(lèi)
package com.cdtye.itps.jjxt.config; import java.util.Properties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.github.pagehelper.PageHelper; /** ?* @Author Zhongks ?* @Description //TODO 分頁(yè)配置對(duì)象 ?* @Date 14:47 2021/4/23 ?* @Param ?* @return ?**/ @Configuration public class PageHelperConfiguration { ?? ?/** ?? ? * @Author Zhongks ?? ? * @Description //TODO 分頁(yè)對(duì)象實(shí)列化 ?? ? * @Date 15:49 2021/4/23 ?? ? * @Param [] ?? ? * @return com.github.pagehelper.PageHelper ?? ? **/ ?? ?@Bean ?? ?public PageHelper pageHelper() { ?? ??? ?PageHelper pageHelper = new PageHelper(); ?? ??? ?Properties p = new Properties(); ?? ??? ?p.setProperty("offsetAsPageNum", "true"); ?? ??? ?p.setProperty("rowBoundsWithCount", "true"); ?? ??? ?p.setProperty("reasonable", "true"); ?? ??? ?p.setProperty("dialect", "Oracle"); ?? ??? ?pageHelper.setProperties(p); ?? ??? ?return pageHelper; ?? ?} }
3.在Service層進(jìn)行分頁(yè)操作:
/** ? ? ?* @Author Zhongks ? ? ?* @Description //TODO 列表頁(yè)面顯示 ? ? ?* @Date 18:42 2021/4/22 ? ? ?* @Param [] ? ? ?* @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>> ? ? ?**/ ? ? public PageInfo<Map<String, Object>> getList(BureauNoticeVo vo){ ? ? ? ? if(vo.getPage()!=null&&vo.getSize()!=null){ ? ? ? ? ? ? //設(shè)置頁(yè)碼數(shù)以及一頁(yè)顯示數(shù)量 ? ? ? ? ? ? PageHelper.startPage(vo.getPage(),vo.getSize()); ? ? ? ? } ? ? ? ? //自己發(fā)布的或者下發(fā)單位中含有當(dāng)前登入人單位編碼的才能看 ? ? ? ? List<Map<String, Object>> bureauNoticeList = bureauNoticeMapper.getList(vo,AuthHelper.loginUser().getUnitDeptCode()); ? ? ? ? bureauNoticeList.forEach(map->{ ? ? ? ? ? ? //得到下發(fā)單位信息集合 ? ? ? ? ? ? List<String> deptNameList = bureauNoticeAcceptService.getBureauNoticeAcceptAndDeptByNoticeId((String) map.get("id")); ? ? ? ? ? ? map.put("deptNameList",deptNameList); ? ? ? ? ? ? //得到附件信息集合 ? ? ? ? ? ? map.put("fileList",this.getFileList((String) map.get("id"))); ? ? ? ? }); ? ? ? ? //將需要進(jìn)行分頁(yè)的list傳入Pagehelper實(shí)現(xiàn)分頁(yè) ? ? ? ? PageInfo<Map<String, Object>> pageInfo = new PageInfo(bureauNoticeList); ? ? ? ? return pageInfo; ? ? }
4.查詢(xún)類(lèi)Vo:
@ApiModel("") @Getter @Setter public class BureauNoticeVo extends BaseVo { ? ? @ApiModelProperty(value = "開(kāi)始時(shí)間") ? ? private String startDate; ? ? @ApiModelProperty(value = "開(kāi)始時(shí)間") ? ? private String endDate; ? ? @ApiModelProperty(value = "描述") ? ? private String noticeDescription; ? ? @ApiModelProperty(value = "頁(yè)碼") ? ? private Integer page; ? ? @ApiModelProperty(value = "頁(yè)顯示數(shù)") ? ? private Integer size; }
5.接口返回?cái)?shù)據(jù):
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Mybatis分頁(yè)插件PageHelper的使用詳解
- SpringBoot項(xiàng)目中分頁(yè)插件PageHelper無(wú)效的問(wèn)題及解決方法
- SpringMvc+Mybatis+Pagehelper分頁(yè)詳解
- mybatis分頁(yè)插件pageHelper詳解及簡(jiǎn)單實(shí)例
- PageHelper插件實(shí)現(xiàn)一對(duì)多查詢(xún)時(shí)的分頁(yè)問(wèn)題
- Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的方法
- Springboot整合pagehelper分頁(yè)功能
- Mybatis分頁(yè)插件PageHelper的配置和簡(jiǎn)單使用方法(推薦)
- SpringBoot集成MyBatis的分頁(yè)插件PageHelper實(shí)例代碼
- 使用mybatis插件PageHelper實(shí)現(xiàn)分頁(yè)效果
相關(guān)文章
關(guān)于idea中出現(xiàn)nbsp和zwsp的完美解決辦法
本文給大家介紹關(guān)于idea中出現(xiàn)nbsp和zwsp的解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-06-06將內(nèi)容寫(xiě)到txt文檔里面并讀取及刪除的方法
本文有個(gè)不錯(cuò)的示例,主要講解如何將內(nèi)容寫(xiě)到txt文檔里面、讀取文件里面的內(nèi)容以及清除txt文件里面的內(nèi)容2014-01-01Spring Boot中配置定時(shí)任務(wù)、線程池與多線程池執(zhí)行的方法
這篇文章主要給大家介紹了關(guān)于Spring Boot中配置定時(shí)任務(wù)、線程池與多線程池執(zhí)行的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09SpringBoot手動(dòng)使用EhCache的方法示例
本篇文章主要介紹了SpringBoot手動(dòng)使用EhCache的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Java使用POI導(dǎo)出Excel(一):?jiǎn)蝧heet
這篇文章介紹了Java使用POI導(dǎo)出Excel的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10Java簡(jiǎn)易登錄注冊(cè)功能實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Java簡(jiǎn)易登錄注冊(cè)功能實(shí)現(xiàn)代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringBoot啟動(dòng)流程SpringApplication準(zhǔn)備階段源碼分析
這篇文章主要為大家介紹了SpringBoot啟動(dòng)流程SpringApplication準(zhǔn)備階段源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04