Spring通过springframework.data的@PageableDefault注解分页
在使用Spring+mybatis
框架时,看到很多人用的pageHelper插件进行分页,如果不用的话,使用spring.data
下的@PageableDefault
也是可以完成分页功能的。
@PageableDefault
接口
package org.springframework.data.web; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.data.domain.Sort.Direction; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER}) public @interface PageableDefault { int value() default 10; int size() default 10; int page() default 0; String[] sort() default {}; Direction direction() default Direction.ASC; }
Pageable
定义了很多方法,但其核心的信息只有两个:一是分页的信息(page、size),二是排序的信息。使用@PageableDefault
的时候可以自定义分页信息@PageableDefault(value = 15, sort = { "update_time" }, direction = Sort.Direction.DESC) Pageable pageable)
。
controller层:
package me.cf81.onestep.cms.controller; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import me.cf81.commons.web.bind.annotation.FormModel; import me.cf81.commons.web.bind.util.MapWrapper; import me.cf81.onestep.cms.model.CMSPage; import me.cf81.onestep.cms.service.CMSPageService; import me.cf81.onestep.epc.Exceptions; import me.cf81.onestep.util.Util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.web.PageableDefault; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by yh on 2018/08/07. */ @RestController @RefreshScope public class CMSPageController { @Autowired private CMSPageService cmsPageService; @ApiOperation(value = "页面分页") @ApiImplicitParams(value = { @ApiImplicitParam(name = "x-access-token", value = "令牌", paramType = "header", required = true), @ApiImplicitParam(name = "page", value = "页码,从0开始", paramType = "query"), @ApiImplicitParam(name = "search.name", value = "页面名称", paramType = "query") }) @GetMapping("/cms/page/page_list:search") public Page<CMSPage> getPageByMap(@FormModel MapWrapper<String, Object> mapWrappers, @PageableDefault(value = 15, sort = { "update_time" }, direction = Sort.Direction.DESC) Pageable pageable) { try { Long companyId = Util.getCompanyId(); return cmsPageService.findPageByMap(mapWrappers.toMap(), pageable, companyId); } catch (Exception e) { e.printStackTrace(); throw Exceptions.ERROR.buildException(); } } }
sql语句:
<!--新闻分页--> <select id="selectPageByMap" resultType="me.cf81.onestep.cms.model.CMSPage"> SELECT id,`name`,create_time,update_time,`key`,is_delete,is_release FROM temp_cms_page cp <where> cp.is_delete=0 AND company_id = #{companyId} <include refid="conditions"/> </where> </select>
当不对@PageableDefault
设置属性时,采用的是默认属性(0,10,不排序),这个时候就需要将分页信息写进sql语句。
controller:
@RestController @RefreshScope public class CMSPageController { @Autowired private CMSPageService cmsPageService; @ApiOperation(value = "页面分页") @ApiImplicitParams(value = { @ApiImplicitParam(name = "x-access-token", value = "令牌", paramType = "header", required = true), @ApiImplicitParam(name = "page", value = "页码,从0开始", paramType = "query"), @ApiImplicitParam(name = "search.name", value = "页面名称", paramType = "query") }) @GetMapping("/cms/page/page_list:search") public Page<CMSPage> getPageByMap(@FormModel MapWrapper<String, Object> mapWrappers, @PageableDefault Pageable pageable) { try { Long companyId = Util.getCompanyId(); return cmsPageService.findPageByMap(mapWrappers.toMap(), pageable, companyId); } catch (Exception e) { e.printStackTrace(); throw Exceptions.ERROR.buildException(); } } }
sql语句:
<!--新闻分页--> <select id="selectPageByMap" resultType="me.cf81.onestep.cms.model.CMSPage"> SELECT id,`name`,create_time,update_time,`key`,is_delete,is_release FROM temp_cms_page cp <where> cp.is_delete=0 AND company_id = #{companyId} <include refid="conditions"/> </where> ORDER BY cp.update_time DESC limit #{pageable.offset}, #{pageable.pageSize} </select>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!