使用sa-token和mybatis-plus分页时swagger中显示返回结果内容配置

我们直接用sa-token的SaResult里面是没有@Api注解的,swagger是无法识别的,所以我们自定义一个SaResult,同理mybatis-plus里的Page, 我们也学着自定义一个。如下:

package com.chhuang.core.vo;

import com.chhuang.core.enums.SaCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @ClassName ChResult
 * @Description 封装前端请求返回结果
 * @Author Darren Huang
 * @Date 2022/11/19 13:03
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "封装前端请求返回结果对象", description = "封装前端请求返回结果,包括了code,msg和data")
public class SaResult<T> implements Serializable {
    private static final long serialVersionUID = -5258833270567888760L;

    @ApiModelProperty(value = "编码")
    private int code;
    @ApiModelProperty(value = "信息")
    private String msg;
    @ApiModelProperty(value = "数据")
    private T data;

    public static<T> SaResult<T> ok() {
        return new SaResult<T>(SaCode.SUCCESS.getCode(), SaCode.SUCCESS.getMessage(), (T)null);
    }

    public static<T> SaResult<T> ok(String msg) {
        return new SaResult<T>(SaCode.SUCCESS.getCode(), msg, (T)null);
    }

    public static<T> SaResult<T> code(int code) {
        return new SaResult<T>(code, (String)null, (T)null);
    }

    public static<T> SaResult<T> data(T data) {
        return new SaResult<T>(SaCode.SUCCESS.getCode(), SaCode.SUCCESS.getMessage(), data);
    }

    public static<T> SaResult<T> data(String msg, T data) {
        return new SaResult<T>(SaCode.SUCCESS.getCode(), msg, data);
    }

    public static<T> SaResult<T> error() {
        return new SaResult<T>(SaCode.INTERNAL_SERVER_ERROR.getCode(), SaCode.INTERNAL_SERVER_ERROR.getMessage(), (T)null);
    }

    public static<T> SaResult<T> error(String msg) {
        return new SaResult<T>(SaCode.INTERNAL_SERVER_ERROR.getCode(), msg, (T)null);
    }

    public static<T> SaResult<T> get(int code, String msg, T data) {
        return new SaResult<T>(code, msg, data);
    }

    public static<T> SaResult<T> get(int code, String msg) {
        return SaResult.get(code, msg, (T)null);
    }

    public String toString() {
        return "{\"code\": " + this.getCode() + ", \"msg\": " + this.getMsg() + ", \"data\": " + this.getData() + "}";
    }
}
package com.chhuang.core.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

/**
 * @ClassName SaPage
 * @Description 分页
 * @Author Darren Huang
 * @Date 2022/11/19 13:39
 * @Version 1.0
 */
@ApiModel(value = "分页对象", description = "用户分页查询是传递分页参数,并返回查询结果")
public class Page<T> implements IPage<T> {
    private static final long serialVersionUID = 3489660490059839625L;

    @ApiModelProperty(value = "查询结果集合")
    protected List<T> records;
    @ApiModelProperty(value = "数据总个数")
    protected long total;
    @ApiModelProperty(value = "每页个数")
    protected long size;
    @ApiModelProperty(value = "当前页")
    protected long current;
    @ApiModelProperty(value = "排序集合[{column:排序字段名, asc:是否正序默认为true}]")
    protected List<OrderItem> orders;
    @ApiModelProperty(value = "优化计数SQL")
    protected boolean optimizeCountSql;
    @ApiModelProperty(value = "查询计数")
    protected boolean searchCount;
    @ApiModelProperty(value = "优化联接计数SQL")
    protected boolean optimizeJoinOfCountSql;
    @ApiModelProperty(value = "计数编号")
    protected String countId;
    @ApiModelProperty(value = "每页个数")
    protected Long maxLimit;
    
    public Page() {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        this.orders = new ArrayList();
        this.optimizeCountSql = true;
        this.searchCount = true;
        this.optimizeJoinOfCountSql = true;
    }

    public Page(long current, long size) {
        this(current, size, 0L);
    }

    public Page(long current, long size, long total) {
        this(current, size, total, true);
    }

    public Page(long current, long size, boolean searchCount) {
        this(current, size, 0L, searchCount);
    }

    public Page(long current, long size, long total, boolean searchCount) {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        this.orders = new ArrayList();
        this.optimizeCountSql = true;
        this.searchCount = true;
        this.optimizeJoinOfCountSql = true;
        if (current > 1L) {
            this.current = current;
        }

        this.size = size;
        this.total = total;
        this.searchCount = searchCount;
    }

    public boolean hasPrevious() {
        return this.current > 1L;
    }

    public boolean hasNext() {
        return this.current < this.getPages();
    }

    public List<T> getRecords() {
        return this.records;
    }

    public IPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public long getTotal() {
        return this.total;
    }

    public IPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    public long getSize() {
        return this.size;
    }

    public IPage<T> setSize(long size) {
        this.size = size;
        return this;
    }

    public long getCurrent() {
        return this.current;
    }

    public IPage<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    public String countId() {
        return this.countId;
    }

    public Long maxLimit() {
        return this.maxLimit;
    }

    private String[] mapOrderToArray(Predicate<OrderItem> filter) {
        List<String> columns = new ArrayList(this.orders.size());
        this.orders.forEach((i) -> {
            if (filter.test(i)) {
                columns.add(i.getColumn());
            }

        });
        return (String[])columns.toArray(new String[0]);
    }

    private void removeOrder(Predicate<OrderItem> filter) {
        for(int i = this.orders.size() - 1; i >= 0; --i) {
            if (filter.test((OrderItem)this.orders.get(i))) {
                this.orders.remove(i);
            }
        }

    }

    public Page<T> addOrder(OrderItem... items) {
        this.orders.addAll(Arrays.asList(items));
        return this;
    }

    public Page<T> addOrder(List<OrderItem> items) {
        this.orders.addAll(items);
        return this;
    }

    public List<OrderItem> orders(){
        return this.orders;
    }

    public boolean optimizeCountSql() {
        return this.optimizeCountSql;
    }

    public static <T> Page<T> of(long current, long size, long total, boolean searchCount) {
        return new Page(current, size, total, searchCount);
    }

    public boolean optimizeJoinOfCountSql() {
        return this.optimizeJoinOfCountSql;
    }

    public Page<T> setSearchCount(boolean searchCount) {
        this.searchCount = searchCount;
        return this;
    }

    public Page<T> setOptimizeCountSql(boolean optimizeCountSql) {
        this.optimizeCountSql = optimizeCountSql;
        return this;
    }

    @ApiModelProperty(value = "总页数")
    public long getPages() {
        return IPage.super.getPages();
    }

    public static <T> Page<T> of(long current, long size) {
        return of(current, size, 0L);
    }

    public static <T> Page<T> of(long current, long size, long total) {
        return of(current, size, total, true);
    }

    public static <T> Page<T> of(long current, long size, boolean searchCount) {
        return of(current, size, 0L, searchCount);
    }

    public boolean searchCount() {
        return this.total < 0L ? false : this.searchCount;
    }

    /** @deprecated */
    @Deprecated
    public String getCountId() {
        return this.countId;
    }

    /** @deprecated */
    @Deprecated
    public Long getMaxLimit() {
        return this.maxLimit;
    }

    /** @deprecated */
    @Deprecated
    public List<OrderItem> getOrders() {
        return this.orders;
    }

    /** @deprecated */
    @Deprecated
    public boolean isOptimizeCountSql() {
        return this.optimizeCountSql;
    }

    /** @deprecated */
    @Deprecated
    public boolean isSearchCount() {
        return this.searchCount;
    }

    public void setOrders(final List<OrderItem> orders) {
        this.orders = orders;
    }

    public void setOptimizeJoinOfCountSql(final boolean optimizeJoinOfCountSql) {
        this.optimizeJoinOfCountSql = optimizeJoinOfCountSql;
    }

    public void setCountId(final String countId) {
        this.countId = countId;
    }

    public void setMaxLimit(final Long maxLimit) {
        this.maxLimit = maxLimit;
    }
}

Page这里有点小小的不完善,要用mybatis-plus的分页方法一定要实现IPage,而这个OrderItem是在IPage里写的,没办法自定义用到自己写的。只能先这样注释一下,

@ApiModelProperty(value = "排序集合[{column:排序字段名, asc:是否正序默认为true}]")
protected List<OrderItem> orders;

使用一下看看,

    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param chUser 查询条件实体
     * @return 所有数据
     */
    @GetMapping("/query")
    //说明是什么方法(可以理解为方法注释)
    @ApiOperation(value = "ChUser查询", notes = "根据查询条件和分页条件查询数据的接口")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "返回ChUser信息列表")
    })
    public SaResult<Page<ChUser>> query(@RequestBody Page<ChUser> page, @RequestBody ChUser chUser) {
        return SaResult.data(i18nMessage.get(SaCode.SUCCESS.getCode()), this.chUserService.page(page, new QueryWrapper<>(chUser)));
    }

 这样,平台的接口返回内容就完整了,可以交给前端写用了,不过前端也是我自己。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值