首页 > 其他分享 >实体类对象与Vo视图对象转换举例

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

时间:2024-01-31 13:55:42浏览次数:38  
标签:实体类 String questionVo question private Vo 视图 Integer import

实体类对象:

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;
}

标签:实体类,String,questionVo,question,private,Vo,视图,Integer,import
From: https://www.cnblogs.com/techgy/p/17999107

相关文章

  • QGIS视图改为3D视图
    10.使用投影—QGISDocumentation文档(osgeo.cn)图层属性可以方便改为彩色: 图层右键点“图层CRS”选“设置CRS”,输入3785 点OK后,图层再右键选择“将图层的CRS设置成工程的CRS”这时,视图-》新建3D视图、就可以建立了。......
  • Docker Volume 的经常用法区别
    对于使用NFS的Docker数据卷,配置示例应当类似于这样:version:'3'services:my_service:image:your_imagevolumes:#挂载命名的数据卷到容器内的路径(详细模式)-type:volumesource:my_nfs_volumetarget:/path/in/containe......
  • IPQ4019: Revolutionizing Long-Range Wireless Connectivity
    UnveilingtheIPQ4019:RevolutionizingLong-RangeWirelessConnectivityIntroduction:TheIPQ4019System-on-Chip(SoC)emergesasagame-changerforPoint-to-Point(PTP)andPoint-to-Multipoint(PTMP)applications,presentingadvancedfeaturestailoredfor......
  • 苹果安卓或实现 WiFi 消息传递 ;马斯克宣布首例人类接受脑机接口植入丨 RTE 开发者日报
      开发者朋友们大家好: 这里是「RTE开发者日报」,每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享RTE(RealTimeEngagement)领域内「有话题的新闻」、「有态度的观点」、「有意思的数据」、「有思考的文章」、「有看点的会议」,但内容仅代表编辑......
  • 索引和函数视图及存储过程
    索引和函数视图及存储过程1.索引在数据库中索引最核心的作用是:加速查找1.1索引原理为什么加上索引之后速度能有这么大的提升呢?因为索引的底层是基于B+Tree的数据结构存储的很明显,如果有了索引结构的查询效率比表中逐行查询的速度要快很多且数据量越大越明显数据库的索......
  • 基于volcano实现节点真实负载感知调度
    本文分享自华为云社区《基于volcano实现节点真实负载感知调度》,作者:可以交个朋友。背景默认调度器调度器视某个节点的空闲可调度资源=节点可分配资源-SUM(节点上已调度Pod们的request),当某个Pod处于pending状态待调度时,默认调度器根据Pod中指定的request值和各个节点的空闲......
  • 美国宣布启动 NAIRR 计划打造 AI 帝国;Siri 将获大语言模型支持丨 RTE 开发者日报 Vol.
      开发者朋友们大家好: 这里是「RTE开发者日报」,每天和大家一起看新闻、聊八卦。我们的社区编辑团队会整理分享RTE(RealTimeEngagement)领域内「有话题的新闻」、「有态度的观点」、「有意思的数据」、「有思考的文章」、「有看点的会议」,但内容仅代表编辑......
  • 使用 AI 构建面向未来的 DevOps
    从去年底开始,生成式AI(AIGC) 作为热门话题,深入影响到了IT行业的各个领域和所有从业者。DevOps 是通过持续集成、持续部署、持续交付的方式,将开发和运营更好地整合在一起的流程,它的发展与架构的演进是紧密相连的。当前的架构逐渐在向微服务化的方向发展,出现了许多新的技术,如容......
  • 查询表、视图字符串字段的长度
    SELECTa.colorder字段序号,a.name字段名,b.name类型,CASEWHENCOLUMNPROPERTY(a.id,a.name,'PRECISION')=-1THEN',长度无限制'ELSE',长度限制'+CAST(COLUMNPROPERTY(a.id,......
  • Gogs,支付宝沙箱支付,DevOps&CI/CD
    1.Gogs2.支付宝沙箱支付测试3.DevOps是生么4.CI/CD是什么1.Gogs是一款极易搭建的自助Git服务。Gogs的目标是打造一个最简单、最快速和最轻松的方式搭建自助Git服务。使用Go语言开发使得Gogs能够通过独立的二进制分发,并且支持Go语言支持的所有平台,包括Linux、Ma......