Ho 使用带有嵌套值和条件值的 mapstruct 的自定义映射器
Ho to use a custom mapper with mapstruct with nested values and conditional values
我正在尝试使用 mapstrut 将一个对象映射到另一个对象,目前在某些情况下如何使用它面临一些挑战。
public class TargetOrderDto {
String id;
String preferedItem;
List<Item> items;
String status;
Address address;
}
public class Item {
String id;
String name;
}
public abstract class TargetOrderMapper {
@Autowired
private StatusRepository statusRepository;
@Mappings({
@Mapping(target = "id", source = "reference"),
@Mapping(target = "preferedItem", source = ""), // Here I need to loop through these values checking for a single value with a specific tag
@Mapping(target = "items", source = "items"), // List of objects to another list of different data types.
@Mapping(target = "status", source = "remoteStatus") // may need to extract a value from a repository
})
abstract OrderDto toTargetOrderDto(RemoteOrder remoteOrder);
}
// Remote Data
public class RemoteOrder {
String reference;
List<Item> items;
String remoteStatus;
}
public class RemoteItem {
String id;
String flag;
String description;
}
这些是我无法理解的当前场景(也许我正在映射一个复杂的对象)。
- 首选项目 :
为此,我需要遍历订单中的项目并使用特定标志标识项目。 (如果它匹配,那么我取那个值,否则我使用 null)
- 项目:
我需要将其转换为 2 个不同列表的列表; List from List,都有自己不同的映射规则。
- 远程状态:
这个有点棘手,我需要从 remoteOrder 中提取状态,然后使用 statusRepository 在数据库中查找数据库中的备用映射值。
非常感谢任何帮助。
您不能使用 MapStruct 执行业务逻辑。因此,如果涉及列表中的条件映射,请保持映射简单并定义您自己的方法。注意:您可以编写自己的方法,MapStruct 会 select 它。此外,从这个自己的实现中,您可以再次参考 MapStruct 方法。
public abstract class TargetOrderMapper {
@Autowired
private StatusRepository statusRepository;
@Mappings({
@Mapping(target = "id", source = "reference"),
@Mapping(target = "preferedItem", source = ""), // Here I need to loop through these values checking for a single value with a specific tag
@Mapping(target = "items", source = "items"), // List of objects to another list of different data types.
@Mapping(target = "status", source = "remoteStatus") // may need to extract a value from a repository
})
abstract OrderDto toTargetOrderDto(RemoteOrder remoteOrder);
protected List<Item> toItemList(List<Item> items) {
// do what ever you want..
// and call toItem during iterating.
}
protected abstract Item toItem(Item item);
}
状态也是如此。前段时间加了个关于列表的FAQ entry(主要是更新,估计这里也是一样)
关于查找,您可以使用@MappingContext
传递包含访问数据库逻辑的上下文。参见 here
我正在尝试使用 mapstrut 将一个对象映射到另一个对象,目前在某些情况下如何使用它面临一些挑战。
public class TargetOrderDto {
String id;
String preferedItem;
List<Item> items;
String status;
Address address;
}
public class Item {
String id;
String name;
}
public abstract class TargetOrderMapper {
@Autowired
private StatusRepository statusRepository;
@Mappings({
@Mapping(target = "id", source = "reference"),
@Mapping(target = "preferedItem", source = ""), // Here I need to loop through these values checking for a single value with a specific tag
@Mapping(target = "items", source = "items"), // List of objects to another list of different data types.
@Mapping(target = "status", source = "remoteStatus") // may need to extract a value from a repository
})
abstract OrderDto toTargetOrderDto(RemoteOrder remoteOrder);
}
// Remote Data
public class RemoteOrder {
String reference;
List<Item> items;
String remoteStatus;
}
public class RemoteItem {
String id;
String flag;
String description;
}
这些是我无法理解的当前场景(也许我正在映射一个复杂的对象)。
- 首选项目 : 为此,我需要遍历订单中的项目并使用特定标志标识项目。 (如果它匹配,那么我取那个值,否则我使用 null)
- 项目: 我需要将其转换为 2 个不同列表的列表; List from List,都有自己不同的映射规则。
- 远程状态: 这个有点棘手,我需要从 remoteOrder 中提取状态,然后使用 statusRepository 在数据库中查找数据库中的备用映射值。
非常感谢任何帮助。
您不能使用 MapStruct 执行业务逻辑。因此,如果涉及列表中的条件映射,请保持映射简单并定义您自己的方法。注意:您可以编写自己的方法,MapStruct 会 select 它。此外,从这个自己的实现中,您可以再次参考 MapStruct 方法。
public abstract class TargetOrderMapper {
@Autowired
private StatusRepository statusRepository;
@Mappings({
@Mapping(target = "id", source = "reference"),
@Mapping(target = "preferedItem", source = ""), // Here I need to loop through these values checking for a single value with a specific tag
@Mapping(target = "items", source = "items"), // List of objects to another list of different data types.
@Mapping(target = "status", source = "remoteStatus") // may need to extract a value from a repository
})
abstract OrderDto toTargetOrderDto(RemoteOrder remoteOrder);
protected List<Item> toItemList(List<Item> items) {
// do what ever you want..
// and call toItem during iterating.
}
protected abstract Item toItem(Item item);
}
状态也是如此。前段时间加了个关于列表的FAQ entry(主要是更新,估计这里也是一样)
关于查找,您可以使用@MappingContext
传递包含访问数据库逻辑的上下文。参见 here