服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实体映射工具MapStruct使用方法详解

Java实体映射工具MapStruct使用方法详解

2022-07-13 09:54AnakinSky Java教程

MapStruct是用于代码中JavaBean对象之间的转换,例如DO转换为DTO,DTO转换为VO,或Entity转换为VO等场景,这篇文章主要给大家介绍了关于Java实体映射工具MapStruct使用的相关资料,需要的朋友可以参考下

1.序

通常在后端开发中经常不直接返回实体Entity类,经过处理转换返回前端,前端提交过来的对象也需要经过转换Entity实体才做存储;通常使用的BeanUtils.copyProperties方法也比较粗暴,不仅效率低下(使用反射)而且仅映射相同名的属性,多数情况下还需要手动编写对应的转换方法实现。

插件MapStruct以接口方法结合注解优雅实现对象转换,MapStruct生成器生成代码以更贴近原生的Setter、Getter方法处理属性映射更为高效。

https://github.com/mapstruct/mapstruct/

https://github.com/mapstruct/mapstruct-examples

2.简单用例

实体对象User

?
1
2
3
4
5
6
7
8
@Data
@AllArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
    private String address;
}

转换对象UserVO

?
1
2
3
4
5
6
@Mapper
public interface UserConvert {
    UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
    @Mapping(source = "name", target = "userName")
    UserVO toVO(User entity);
}

转换接口

?
1
2
3
4
5
@Test
public void contextLoads() {
    User user = new User(0, "Tester", 1, "上海市徐汇区");
    UserVO userVO = UserConvert.INSTANCE.toVO(user);
}

使用示例

?
1
2
3
4
5
@Test
public void contextLoads() {
    User user = new User(0, "Tester", 1, "上海市徐汇区");
    UserVO userVO = UserConvert.INSTANCE.toVO(user);
}

3.使用详解

1)关于接口注解@Mapper几种属性用法详解

uses 使用其他手动编写的或者其他Mapper接口覆写相关的转换方法,不能循环引用

?
1
@Mapper(uses=DateMapper.class)

imports 引用相关类,允许通过mapping.expression()、mapping.defaultExpression()直接使用这些类型

?
1
2
3
4
5
6
7
8
9
10
@Mapper(imports = DateUtil.class)
public interface UserConvert {
    UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
    @Mappings({
            @Mapping(source = "name", target = "userName"),
            // 以指定方法转换属性,这里如果不使用imports,则需要写全引用类包路径
            @Mapping(target = "birthday", expression = "java(DateUtil.formatDate(entity.getBirthday()))")
    })
    UserVO toVO(User entity);
}

unmappedSourcePolicy、unmappedTargetPolicy 针对源类型/目标类型中未映射属性的反馈策略

typeConversionPolicy 针对有损转换的反馈策略,例如Long转Integer

反馈策略主要有三种:IGNORE,默认值,忽略未映射的字段。WARN,警告。ERROR,报错

?
1
@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR)

componentModel 指定生成映射器实例的模式,主要有四种:

default,不主动生成实例,通常以Mappers.getMapper(Class)实例化。

cdi,以CDI标准实例化映射器,使用@Inject注入相关实例,

spring,Spring Bean方式实例化,

jsr330,jsr330标准实例化

?
1
2
3
4
5
@Mapper(componentModel = "spring")
public interface UserConvert {
    @Mapping(source = "name", target = "userName")
    UserVO toVO(User entity);
}
?
1
2
@Autowired
private UserConvert userConvert;

implementationName 指定实现类名称,映射生成器接口会自动生成实现类<CLASS_NAME>Impl,使用此属性可避免类冲突

implementationPackage 指定实现类包路径

config 指定配置类,由指定的@MapperConfig配置类,config导入相关配置

配置类

?
1
2
3
4
5
6
@MapperConfig(
        uses = DateUtil.class,
        unmappedSourcePolicy = ReportingPolicy.WARN
)
public interface UserConfig {
}

导入配置类

?
1
@Mapper(componentModel = "spring", config = UserConfig.class)

collectionMappingStrategy 集合映射策略,这里注意集合映射时,如果集合中的类型已有对应转换方法,集合转换时会优先使用

?
1
2
3
4
5
6
@Mappings({
        @Mapping(source = "name", target = "userName"),
        @Mapping(target = "birthday", expression = "java(DateUtil.formatDate(entity.getBirthday()))")
})
UserVO toVO(User entity);
List<UserVO> collectionCvt(List<User> entities);

GENERATED CODE 生成器生成代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public List<UserVO> collectionCvt(List<User> entities) {
    if (entities == null) {
        return null;
    } else {
        List<UserVO> list = new ArrayList(entities.size());
        Iterator var3 = entities.iterator();
 
        while(var3.hasNext()) {
            User user = (User)var3.next();
            // 集合转换时优先使用了已定义的toVO方法
            list.add(this.toVO(user));
        }
 
        return list;
    }
}

nullValueMappingStrategy null作为源值映射策略;RETURN_NULL默认返回null, RETURN_DEFAULT返回默认值,对于对象会通过构造器自动构造对象返回,集合会返回空集合

当值为RETURN_DEFAULT时,如果映射规则中包含Mapping.expression、Mapping.constant必须手动判空处理,否则NPE

?
1
@MapperConfig(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)

NullValuePropertyMappingStrategy null作为源属性映射策略;SET_TO_NULL默认返回null,SET_TO_DEFAULT返回默认值,IGNORE 忽略该值,以目标对象已存在的值为准

MappingInheritanceStrategy 继承方法级映射配置策略:EXPLICIT 显示使用InheritConfiguration生效。

AUTO_INHERIT_FROM_CONFIG 自动继承正向转换的配置。AUTO_INHERIT_REVERSE_FROM_CONFIG 自动继承反向转换的配置。AUTO_INHERIT_ALL_FROM_CONFIG 都继承

?
1
2
3
@MapperConfig(
        mappingInheritanceStrategy = MappingInheritanceStrategy.EXPLICIT
)
?
1
2
@InheritConfiguration
void cvtVO(User entity, @MappingTarget UserVO vo);

nullValueCheckStrategy 空值监测策略

2) 其他方法级别注解

@InheritInverseConfiguration 反向转换时继承映射规则

@Mapping 配置类型属性的映射规则;

dateFormat 格式化日期

?
1
@Mapping(target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")

numberFormat 数字格式化

?
1
@Mapping(target = "price", numberFormat = "$#.00")

constant 常量

?
1
@Mapping(target = "age", constant = "0")

总结

到此这篇关于Java实体映射工具MapStruct使用的文章就介绍到这了,更多相关Java实体映射工具MapStruct内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/anakinsky008/p/15513375.html

延伸 · 阅读

精彩推荐
  • Java教程Java数组扩容实例代码

    Java数组扩容实例代码

    这篇文章主要介绍了Java数组扩容实例代码,具有一定借鉴价值,需要的朋友可以参考下...

    Engineer-MrYang8452021-02-22
  • Java教程SpringBoot系列教程之dubbo和Zookeeper集成方法

    SpringBoot系列教程之dubbo和Zookeeper集成方法

    这篇文章主要介绍了SpringBoot系列教程之dubbo和Zookeeper集成方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需...

    小小白菜拱大猪2662020-09-06
  • Java教程Java中的FilterOutputStream 简介_动力节点Java学院整理

    Java中的FilterOutputStream 简介_动力节点Java学院整理

    FilterOutputStream 的作用是用来“封装其它的输出流,并为它们提供额外的功能”。它主要包括BufferedOutputStream, DataOutputStream和PrintStream。接下来通过本文给大...

    动力节点3082020-10-06
  • Java教程Mybatis-Plus 多表联查分页的实现代码

    Mybatis-Plus 多表联查分页的实现代码

    本篇文章主要介绍了Mybatis-Plus 多表联查分页的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    殷天文11692021-05-07
  • Java教程Java单链表反转图文教程

    Java单链表反转图文教程

    这篇文章主要给大家介绍了关于Java单链表反转的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    Dreamer-19692021-09-01
  • Java教程Spring Boot Excel文件导出下载实现代码

    Spring Boot Excel文件导出下载实现代码

    这篇文章带领我们直接实现Excel文件的直接导出下载,后续开发不需要开发很多代码,直接继承已经写好的代码,增加一个Xml配置就可以直接导出。具体实...

    从今天起做个幸福的人6872021-06-15
  • Java教程详解Java适配器模式

    详解Java适配器模式

    这篇文章主要介绍了Java适配器模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一...

    键盘上的麒麟臂5312021-07-24
  • Java教程通过Java来测试JSON和Protocol Buffer的传输文件大小

    通过Java来测试JSON和Protocol Buffer的传输文件大小

    这篇文章主要介绍了通过Java来测试JSON和Protocol Buffer的传输文件大小,Protocol Buffer(文中简称Protobuffer)是谷歌开发的新的文件传输格式,需要的朋友可以参考下...

    cxshun4242020-03-14