如何使用mapStruct仅映射指定的字段? [英] How to map only specified fields using mapStruct?

查看:3291
本文介绍了如何使用mapStruct仅映射指定的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果MapStruct具有相同的名称,则默认情况下映射源和目标的所有属性。虽然我们可以在@Mapping中使用ignore属性来省略任何字段映射。但那不是我想要的。我想控制映射策略。我要指定类似

MapStruct is mapping all the properties of source and destination by default if they have same name. Although we can use ignore property in @Mapping for omitting any field mapping. But that's not I want. I want control over the mapping strategy. I've to specify something like

@Mapper(STRATEGY=MAPPING_STRATEGY.SPECIFIED)
public interface EmployeeToEmployeeDTOMapper {
        @Mappings({
                   @Mapping(target="id", source="id"),
                   @Mapping(target="name", source="name")
                 })
        public EmployeeDTO employeeToEmployeeDTO (Employee emp);
}

现在,此映射仅用于将源和名称从源映射到目标。除非在映射注释中指定,否则不应映射其他字段。

Now this mapping is only meant to map id and name from source to destination.No other fields should be mapped unless specified in the mappings annotation.

推荐答案

您要查找的是#1392 。有一个待处理的PR,因此它可以在下一个版本(1.3.0)中使用。最终的API尚未定义。按照问题和PR完成后通知

What you are looking for is a feature request in #1392. There is a pending PR so it would be available to use in the next version (1.3.0). The final API is not yet defined. Follow the issue and the PR to be notified when it gets done

这篇关于如何使用mapStruct仅映射指定的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆