VO映射DO对象

1. 添加MapStruct依赖

<!--mapStruct依赖 自动生成VO-DO的模型映射-->
<dependency>
  <groupId>org.mapstruct</groupId>
  <artifactId>mapstruct-jdk8</artifactId>
  <version>1.2.0.CR1</version>
</dependency>
<dependency>
  <groupId>org.mapstruct</groupId>
  <artifactId>mapstruct-processor</artifactId>
  <version>1.2.0.CR1</version>
  <scope>provided</scope>
</dependency>

 2. 定于DO对象及VO对象

// DO对象,表示持久化,DO对象的每个属性都对应表的每个字段

/**
 * 用户实体对应表 mgr_user
 */
@Data
@TableName("mgr_user")
public class User {

    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;
    private String username;
    private String password;
    private String name;
    private String userDesc;
    private String employeeNo;
    private String email;
    @TableLogic
    private Byte isDeleted;
    private Long createBy;
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
    private Long updateBy;
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
}

// VO对象,表示视图对象,前端传过来数据封装成一个对象,用于接收前端数据的对象

@Data
@ApiModel(value = "用户信息")
public class RequestUser {

    @TableId(value = "user_id", type = IdType.AUTO)
    @ApiModelProperty(value = "用户ID", example = "0", hidden = true)
    private Long userId;
    @ApiModelProperty(value = "登录名", example = "admin")
    private String username;
    @ApiModelProperty(value = "登录密码", example = "123456")
    private String password;
    @ApiModelProperty(value = "用户名", example = "张三")
    private String name;
    @ApiModelProperty(value = "备注", example = "个性鲜明")
    private String userDesc;
    @ApiModelProperty(value = "员工工号", example = "10001")
    private String employeeNo;
    @ApiModelProperty(value = "邮箱", example = "admin@hundsun.com")
    private String email;
    @TableLogic
    @ApiModelProperty(value = "状态: 0:正常 1:删除", hidden = true)
    private Byte isDeleted;
    @ApiModelProperty(value = "创建者", example = "系统管理员", hidden = true)
    private Long createBy;
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    @ApiModelProperty(value = "创建时间", hidden = true)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
    @ApiModelProperty(value = "更新者", example = "系统管理员", hidden = true)
    private Long updateBy;
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    @ApiModelProperty(value = "更新时间", hidden = true)
    private Date updateTime;
}

3. 创建VO-DO对象映射接口

import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface UserMapping {

    /**
     * VO对象转DO对象
     * @param requestUser
     * @return
     */
    User toUser(RequestUser requestUser);
}

4. 编译后自动生成映射接口的实现类

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2020-12-20T19:29:30+0800",
    comments = "version: 1.2.0.CR1, compiler: javac, environment: Java 1.8.0_201 (Oracle Corporation)"
)
@Component
public class UserMappingImpl implements UserMapping {

    @Override
    public User toUser(RequestUser requestUser) {
        if ( requestUser == null ) {
            return null;
        }

        User user = new User();

        user.setUserId( requestUser.getUserId() );
        user.setUsername( requestUser.getUsername() );
        user.setPassword( requestUser.getPassword() );
        user.setName( requestUser.getName() );
        user.setUserDesc( requestUser.getUserDesc() );
        user.setEmployeeNo( requestUser.getEmployeeNo() );
        user.setEmail( requestUser.getEmail() );
        user.setIsDeleted( requestUser.getIsDeleted() );
        user.setCreateBy( requestUser.getCreateBy() );
        user.setCreateTime( requestUser.getCreateTime() );
        user.setUpdateBy( requestUser.getUpdateBy() );
        user.setUpdateTime( requestUser.getUpdateTime() );

        return user;
    }
}

VO映射DO对象

上一篇:设置时间段 拆分


下一篇:Leetcode - 14. 最长公共前缀