1.项目使用了springboot + mybatis-plus,在开发时需要指定封装需要的数据,但是由于使用了自定义的结果封装类,输出的结果为空
- 在项目中统一抽取了一个BaseEntity,这里封装应用所有实体共同的属性,因此在这里抽取了一个BaseEntityView
@Getter
@Setter
public class BaseEntity implements Serializable {
/**
* 主键id
*/
@TableId(value = "ID", type = IdType.ASSIGN_UUID)
@ApiModelProperty(value = "主键id")
@JsonView(BaseEntityView.class)
private String id;
/**
* 创建时间
*/
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT, insertStrategy = FieldStrategy.NOT_EMPTY)
@ApiModelProperty(value = "创建时间,格式:yyyy-MM-dd HH:mm:ss", dataType = "Date", example = "2020-05-05 10:10:10")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonView(BaseEntityView.class)
private Date createTime;
/**
* 修改时间
*/
@TableField(value = "MODIFY_TIME", fill = FieldFill.INSERT_UPDATE, updateStrategy = FieldStrategy.NOT_EMPTY)
@ApiModelProperty(value = "修改时间,格式:yyyy-MM-dd HH:mm:ss", dataType = "Date", example = "2020-05-05 10:10:10")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonView(BaseEntityView.class)
private Date modifyTime;
/**
* 软删除标志:1已删除,0未删除
*/
@TableLogic
@ApiModelProperty(value = "删除标志(软删除:1已删除,0未删除)", dataType = "Integer")
@TableField(value = "DELETED")
@JsonView(BaseEntityView.class)
private DeletedEnum deleted = DeletedEnum.UNDELETED;
/**
* 基础的视图展示,都提供id,展示都需要继承这个接口
*/
public interface BaseEntityView {
}
}
- 当每个entity需要实现展示自定义的字段时
@Data
public class Entity extends BaseEntity {
private static final long serialVersionUID = 1L;
@JsonView(PlatformAppSimpleView.class)
private AppStatusEnum status;
@JsonView(PlatformAppSimpleView.class)
private String name;
@JsonView(PlatformAppSimpleView.class)
private String key;
@JsonView(PlatformAppSimpleView.class)
private String secret;
/**
* 定义页面上视图展示的数据,用于列表展示
*/
public interface PlatformAppSimpleView extends BaseEntityView {
}
/**
* 视图的详细定义,用于编辑
*/
public interface PlatformAppDetailsView extends PlatformAppSimpleView {
}
}
4.controller层数据展示
@PostMapping(
produces = "application/json;charset=utf-8",
consumes = "application/json;charset=utf-8"
)
@JsonView(PlatformApp.PlatformAppSimpleView.class)
public ResultWrapper<PageDto<?>> page(@RequestBody PageDto<?> page) {
return new ResultWrapper<>(platformAppService.page(page));
}
5.重点,由于我将数据封装了两层,所有每层需要展示的数据都要使用注解,这个时候统一抽取出来的BaseEntityView就有用了
@Data
@AllArgsConstructor
public class ResultWrapper<T> {
/**
* 错误码,其中 {@code 0} 代表没有错误
*
* <p>详见 {@link ErrorCode}</p>
*/
@JsonView(BaseEntity.BaseEntityView.class)
private int errorCode;
/**
* 错误或成功的描述信息
*/
@JsonView(BaseEntity.BaseEntityView.class)
private String message;
/**
* 具体业务的返回结果
*/
@JsonView(BaseEntity.BaseEntityView.class)
private T result;
/**
* 操作成功(设置默认成功的错误码和描述信息),只需设置具体业务的返回结果
*
* @param result 具体业务的返回结果
*/
public ResultWrapper(T result) {
errorCode = ErrorCode.SUCCESS.getErrorCode();
message = ErrorCode.SUCCESS.getMessage();
this.result = result;
}
/**
* 只设置错误码和描述信息,不需要具体业务的返回结果
*
* @param errorCode 错误码,详见 {@link ErrorCode}
* @param message 错误或成功的描述信息
*/
public ResultWrapper(ErrorCode errorCode, String message) {
this.errorCode = errorCode.getErrorCode();
this.message = message;
}
}
6.同时,还有一层封装的数据
@Data
public class PageDto<T> implements IPage<T> {
/**
* 查询数据列表,实际数据封装的位置,这里很重要
*/
@JsonView(BaseEntity.BaseEntityView.class)
private List<T> records = Collections.emptyList();
private long total = 0;
private long size = 10;
private long current = 1;
private long pages;
private boolean isSearchCount = true;
private boolean isPageable = true;
private List<OrderItem> orders = new ArrayList<>();
@Override
public List<OrderItem> orders() {
return this.getOrders();
}
}