使用VO类对实体类进行拓展

我们常常会使用额外的vo类对实体类进行拓展。

package com.example.demo.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

/**
 * 
 * @TableName user
 */
@TableName(value ="user")
@Data
@Getter
public class User implements Serializable {
    /**
     * 
     */
    @TableId(type = IdType.AUTO)
    private Integer userid;

    /**
     * 
     */
    private String userdate;

    /**
     * 
     */
    private String username;

    /**
     * 
     */
    private String useraddress;

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

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        User other = (User) that;
        return (this.getUserid() == null ? other.getUserid() == null : this.getUserid().equals(other.getUserid()))
            && (this.getUserdate() == null ? other.getUserdate() == null : this.getUserdate().equals(other.getUserdate()))
            && (this.getUsername() == null ? other.getUsername() == null : this.getUsername().equals(other.getUsername()))
            && (this.getUseraddress() == null ? other.getUseraddress() == null : this.getUseraddress().equals(other.getUseraddress()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getUserid() == null) ? 0 : getUserid().hashCode());
        result = prime * result + ((getUserdate() == null) ? 0 : getUserdate().hashCode());
        result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode());
        result = prime * result + ((getUseraddress() == null) ? 0 : getUseraddress().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", userid=").append(userid);
        sb.append(", userdate=").append(userdate);
        sb.append(", username=").append(username);
        sb.append(", useraddress=").append(useraddress);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

vo类

package com.example.demo.vo;

import com.example.demo.pojo.User;
import lombok.Data;
import org.springframework.cglib.core.Local;

import java.time.LocalDateTime;

/**
 * @author:xxxxx
 * @create: 2023-01-05 16:32
 * @Description:
 */
@Data
public class UserVo extends User {
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
}
package com.example.demo.vo;

/**
 * @author:xxxxx
 * @create: 2023-01-05 16:39
 * @Description:
 */
public abstract  class BaseEntityWrapper<E,V> {

    public BaseEntityWrapper(){
    }
    /**
     * 定义一个规范,子类各自实现自己的业务逻辑,
     * @return
     */
    public abstract  V entityVO(E entity);

}
package com.example.demo.vo;


import com.example.demo.Util.BeanUtil;
import com.example.demo.pojo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;


/**
 * @author:xxxxx
 * @create: 2023-01-05 16:40
 * @Description:
 */
@Component
public class UserWrapper extends BaseEntityWrapper<User, UserVo> {
    public static UserWrapper userWrapper;

    //流式编程
    public static UserWrapper build() {
        return userWrapper;
    }

    /**
     * 当Spring注入到管理到此类的时候,就会立马执行带有此注解的方法。
     */
    @PostConstruct
    public void init() {
        userWrapper = this;
    }

    @Override
    public UserVo entityVO(User user) {
        UserVo userVo = new UserVo();
        if (user != null) {
            try {
                BeanUtils.copyProperties(user,userVo);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        userVo.setCreateTime(LocalDateTime.now());
        userVo.setUpdateTime(LocalDateTime.now());
        return userVo;
    }

}
package com.example.demo;

import com.example.demo.pojo.User;
import com.example.demo.vo.UserVo;
import com.example.demo.vo.UserWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
        User user = new User();
        user.setUserid(1);
        user.setUserdate("2001.7.29");
        user.setUseraddress("陝西西安");
        user.setUsername("admin");
        UserWrapper userWrapper = new UserWrapper();
        UserVo userVo = userWrapper.entityVO(user);
        System.out.println(userVo);
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值