实体类对象与Vo视图对象转换举例

实体类对象:

package com.yupi.ojSystem.model.entity;

import com.baomidou.mybatisplus.annotation.*;

import java.io.Serializable;
import java.util.Date;
import lombok.Data;

/**
 * 题目
 * @TableName question
 */
@TableName(value ="question")
@Data
public class Question implements Serializable {
    /**
     * id
     */
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表(json 数组)
     */
    private String tags;

    /**
     * 题目答案
     */
    private String answer;

    /**
     * 题目提交数
     */
    private Integer submitNum;

    /**
     * 题目通过数
     */
    private Integer acceptedNum;

    /**
     * 判题用例(json 数组)
     */
    private String judgeCase;

    /**
     * 判题配置(json 对象)
     */
    private String judgeConfig;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 是否删除
     */
    @TableLogic
    private Integer isDelete;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}

视图Vo对象:

package com.yupi.ojSystem.model.vo;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.yupi.ojSystem.model.dto.question.JudgeConfig;
import com.yupi.ojSystem.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 问题Vo
 */
@Data
public class QuestionVo implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表
     */
    private List<String> tags;

    /**
     * 题目提交数
     */
    private Integer submitNum;

    /**
     * 题目通过数
     */
    private Integer acceptedNum;

    /**
     * 判题配置(json 对象)
     */
    private JudgeConfig judgeConfig;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 创建题目人信息
     */
    private UserVO userVO;

    /**
     * Vo转实体对象
     * @param questionVo
     * @return
     */
    public static Question voToObj(QuestionVo questionVo) {
        if (ObjectUtils.isEmpty(questionVo)) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVo, question);
        List<String> tagList = questionVo.getTags();
        if (!CollectionUtils.isEmpty(tagList)) {
            question.setTags(JSONUtil.toJsonStr(tagList));
        }
        JudgeConfig judgeConfigVo = questionVo.getJudgeConfig();
        if (!ObjectUtils.isEmpty(judgeConfigVo)) {
            question.setJudgeConfig(JSONUtil.toJsonStr(judgeConfigVo));
        }
        return question;
    }

    /**
     * 实体对象转Vo
     * @param question
     * @return
     */
    public static QuestionVo objToVo(Question question) {
        if (ObjectUtils.isEmpty(question)) {
            return null;
        }
        QuestionVo questionVo = new QuestionVo();
        BeanUtils.copyProperties(question, questionVo);
        String tags = question.getTags();
        if (StrUtil.isNotBlank(tags)) {
            List<String> tagList = JSONUtil.toList(tags, String.class);
            questionVo.setTags(tagList);
        }
        if (StrUtil.isNotBlank(question.getJudgeConfig())) {
            questionVo.setJudgeConfig(JSONUtil.toBean(question.getJudgeConfig(), JudgeConfig.class));
        }
        return questionVo;
    }

    private static final long serialVersionUID = 1L;
}
posted @ 2024-01-31 13:50  AI未来10Y  阅读(54)  评论(0编辑  收藏  举报