简体   繁体   English

如何使用 mapstruct 将 id 属性的值更改为 DTO class 中的等效描述

[英]How can I change a value of an id attribute to its equivalent description in DTO class using mapstruct

I have a 1 entity class PersonEntity and corresponding to that I have Dto class我有 1 个实体 class PersonEntity 并且对应于我有 Dto class

@Entity(value="person")

class PersonEntity{

String name;

String id;

}

**DTO** 

class PersonDto{
String name;

String id;

String desc; (This is a lookup from a map with id attribute)
}

In my mapper class, I am able to change my entity list to Dto list with below code.

public interface MyMapper{

List<PersonDto> entityListToDtoList(<List<PersonEntity>)

}

How can I use my lookup map to get the description and set in my DTO class.如何使用我的查找 map 来获取描述并在我的 DTO class 中设置。 I am not able to figure out how to determine the value with below code.我无法弄清楚如何使用以下代码确定值。

List<PersonDto> entityListToDtoList(<List<PersonEntity>,@Context Map<String,String> lookupMap)

You have to define the PersonEntity to PersonDto method and add a @Mapping with an expression for that.您必须将PersonEntity定义为PersonDto方法,并为此添加一个带有表达式的@Mapping See the following example:请参见以下示例:

    @Mapping( target = "desc", expression = "java(lookupMap.get(person.getId()))" )
    PersonDto entityToDto(PersonEntity person, @Context Map<String, String> lookupMap);

    List<PersonDto> entityListToDtoList(List<PersonEntity> persons, @Context Map<String, String> lookupMap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM