随笔 - 832  文章 - 2  评论 - 31  阅读 - 167万

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>
复制代码

 

posted on   小破孩楼主  阅读(4904)  评论(0编辑  收藏  举报
编辑推荐:
· 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代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示