美文网首页
模型之间相互转换

模型之间相互转换

作者: 尼尔君 | 来源:发表于2020-08-14 10:01 被阅读0次

使用 MapStruct

1.以maven形式添加

  <org.mapstruct.version>1.3.0.Final</org.mapstruct.version>

 <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

2.举例: UserEntity 转换成UserVO(不包含password)

创建映射Mapper

@Mapper
public interface ModelMapper {
    UserVO covertVo(UserEntity userEntity);
}


   ModelMapper mapper = Mappers.getMapper(ModelMapper.class);
   UserVO userVO = mapper.covertVo(userEntity);

不同字段映射

@Data
public class UserEntity {
    private Long id ;
    private String username;
    private String password;
    private String nickname;

}
@Data
public class UserVO {
    private Long id ;
    private String username;
    private String nick;
}

映射文件编辑

@Mapper
public interface ModelMapper {
    
    @Mapping(source = "nickname" , target = "nick")
    UserVO covertVo(UserEntity userEntity);
}

多个

@Mapper
public interface ModelMapper {

    @Mapping(source = "Id" , target = "id")
    @Mapping(source = "nickname" , target = "nick")
    UserVO covertVo(UserEntity userEntity);
}


@Mapper
public interface ModelMapper {

    @Mappings({
            @Mapping(source = "Id", target = "id"),
            @Mapping(source = "nickname", target = "nick")
    })
    UserVO covertVo(UserEntity userEntity);
}

相关文章

  • 模型之间相互转换

    使用 MapStruct 1.以maven形式添加 2.举例: UserEntity 转换成UserVO(不包含p...

  • golang进阶之路

    类型转换 go语言提供了一种在不同但是相互兼容的类型之间相互转换的方式,并且这种转换非常安全。非数值类型之间的转换...

  • 常用数据类型转换

    NSString和NSData之间的相互转换 NSString 和 NSData之间的转换频率还是挺高的,尤其是J...

  • JS时间格式转换(今天,昨天,前天)

    参考: javascript时间格式转换(今天,昨天,前天) js时间戳与日期格式之间相互转换

  • 时间转成毫秒数

    日期时间字符串和毫秒之间的相互转换

  • Flutter技术点

    1.字典(Map)、JSON、模型相互转换 打印结果 模型数据 2.网络get请求数据 1.返回数据模型 2.发起...

  • C++类型转换

    static_cast(编译阶段) 用于类型相关的指针之间相互转换 用于基本类型的数据之间进行类型转换 dynam...

  • iOS源码阅读 —— YYModel

    YYModel作为一个 iOS/OSX 模型转换框架,为JSON与数据模型之间的转换,提供了高性能的解决方案。 在...

  • iOS 使用Runtime机制将模型(对象)和字典相互转换

    在我们常见开发中往往需要将模型(对象)和字典相互转换,字典转模型(对象)相对简单,可以用系统方法快速实现,而模型(...

  • 图像基本知识【1】

    1.坐标系之间的转换 2.各种色相模型 HSI空间 HSV空间 CIE模型

网友评论

      本文标题:模型之间相互转换

      本文链接:https://www.haomeiwen.com/subject/vxtgdktx.html