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>

猜你喜欢

转载自www.cnblogs.com/zouhong/p/12976186.html