使用PageHelper插件實(shí)現(xiàn)Service層分頁
本文實(shí)例為大家分享了使用PageHelper插件實(shí)現(xiàn)Service層分頁的具體代碼,供大家參考,具體內(nèi)容如下
使用場景:
平時(shí)分頁我們可以直接使用mybatis-plus中內(nèi)置的IPage進(jìn)行分頁,一般是在mapper中寫好接口,在執(zhí)行sql時(shí)就將其進(jìn)行分頁操作,但是有些復(fù)雜的查詢或者是需要拼接返回格式的數(shù)據(jù)就難以操作了,所以我們使用PageHelper插件來實(shí)現(xiàn)Service分頁功能。
1.在pom.xml文件中導(dǎo)入PageHelper插件依賴
<!--pagehelper分頁插件--> <dependency> ? ? <groupId>com.github.pagehelper</groupId> ? ? <artifactId>pagehelper</artifactId> ? ? <version>4.1.6</version> </dependency>
2.編寫PageHelper配置類
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 分頁配置對象 ?* @Date 14:47 2021/4/23 ?* @Param ?* @return ?**/ @Configuration public class PageHelperConfiguration { ?? ?/** ?? ? * @Author Zhongks ?? ? * @Description //TODO 分頁對象實(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)行分頁操作:
/** ? ? ?* @Author Zhongks ? ? ?* @Description //TODO 列表頁面顯示 ? ? ?* @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è)置頁碼數(shù)以及一頁顯示數(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)行分頁的list傳入Pagehelper實(shí)現(xiàn)分頁 ? ? ? ? PageInfo<Map<String, Object>> pageInfo = new PageInfo(bureauNoticeList); ? ? ? ? return pageInfo; ? ? }
4.查詢類Vo:
@ApiModel("") @Getter @Setter public class BureauNoticeVo extends BaseVo { ? ? @ApiModelProperty(value = "開始時(shí)間") ? ? private String startDate; ? ? @ApiModelProperty(value = "開始時(shí)間") ? ? private String endDate; ? ? @ApiModelProperty(value = "描述") ? ? private String noticeDescription; ? ? @ApiModelProperty(value = "頁碼") ? ? private Integer page; ? ? @ApiModelProperty(value = "頁顯示數(shù)") ? ? private Integer size; }
5.接口返回?cái)?shù)據(jù):
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Mybatis分頁插件PageHelper的使用詳解
- SpringBoot項(xiàng)目中分頁插件PageHelper無效的問題及解決方法
- SpringMvc+Mybatis+Pagehelper分頁詳解
- mybatis分頁插件pageHelper詳解及簡單實(shí)例
- PageHelper插件實(shí)現(xiàn)一對多查詢時(shí)的分頁問題
- Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁的方法
- Springboot整合pagehelper分頁功能
- Mybatis分頁插件PageHelper的配置和簡單使用方法(推薦)
- SpringBoot集成MyBatis的分頁插件PageHelper實(shí)例代碼
- 使用mybatis插件PageHelper實(shí)現(xiàn)分頁效果
相關(guān)文章
關(guān)于idea中出現(xiàn)nbsp和zwsp的完美解決辦法
本文給大家介紹關(guān)于idea中出現(xiàn)nbsp和zwsp的解決辦法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-06-06Spring Boot中配置定時(shí)任務(wù)、線程池與多線程池執(zhí)行的方法
這篇文章主要給大家介紹了關(guān)于Spring Boot中配置定時(shí)任務(wù)、線程池與多線程池執(zhí)行的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Java使用POI導(dǎo)出Excel(一):單sheet
這篇文章介紹了Java使用POI導(dǎo)出Excel的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10SpringBoot啟動流程SpringApplication準(zhǔn)備階段源碼分析
這篇文章主要為大家介紹了SpringBoot啟動流程SpringApplication準(zhǔn)備階段源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04