在具有相同属性名称的不同数据类型上使用ModelMapper [英] Using ModelMapper on different data types with same attribute name
问题描述
我有两个类,分别是 Animal
& AnimalDto
我想使用 ModelMapper
将Entity转换为DTO,反之亦然.
但是对于一些名称相似的属性,这些类应该具有不同的数据类型.
我该如何实现?
I have two classes say Animal
& AnimalDto
I want to use ModelMapper
to convert Entity to DTO and vice verca.
But the classes are supposed to have different data types for a few attributes having similar name.
How do I achieve this?
Animal.java
public class Animal {
int category;
String color;
int age;
}
AnimalDto.java
public class AnimalDto {
String category;
int color;
int age;
}
目前,我正在手动进行这样的转换:
currently I'm manually transforming as such:
class AnimalTransformer {
private static Category[] categories = Category.values();
private static Color[] colors = Color.values();
animalEntityToDto(Animal animal) {
AnimalDto animalDto = new AnimalDto();
animalDto.setAge(animal.getAge());
animalDto.setCategory(categories[animal.getCategory()].toString());
animalDto.setColor(Color.valueOf(animal.getColor()).ordinal());
}
animalDtoToEntity(AnimalDto animalDto) {
Animal animal = new Animal();
animal.setAge(animalDto.getAge());
animal.setCategory(Category.valueOf(animalDto.getCategory()).ordinal());
animal.setColor(colors[animalDto.getColor()].toString());
}
}
推荐答案
您提供的示例在流畅度方面可能不是模型映射器的最佳部分,特别是因为转换具有某些特殊困难的 enum
谈到使用泛型.
The example you presented might not be the best part of model mapper in fluency especially because converting enum
s that have some special difficulties what comes to using generics.
无论如何,使用 Converter
或通常使用 AbstractConverter
都是可能的.
Anyway this is possible with Converter
or usually AbstractConverter
.
您没有提供枚举示例,所以我创建了最简单的枚举示例:
You did not provide examples of your enums so I create most simple example enums:
enum Color {
PINK;
}
和
enum Category {
MAMMAL;
}
要将整数 Animal.category
转换为 String AnimalDto.category
,转换器应类似于:
To convert Integer Animal.category
to String AnimalDto.category
, a converter could be like:
public class CategoryToStringConverter extends AbstractConverter<Integer, String> {
@Override
protected String convert(Integer source) {
return Category.values()[source].toString();
}
}
并要将 String Animal.color
转换为 Integer AnimalDto.category
,转换器应类似于:
And to convert String Animal.color
to Integer AnimalDto.category
, a converter could be like:
public class ColorToOrdinalConverter extends AbstractConverter<String, Integer> {
@Override
protected Integer convert(String source) {
return Color.valueOf(source).ordinal();
}
}
用法将是:
mm.createTypeMap(Animal.class, AnimalDto.class).addMappings(mapper -> {
mapper.using(new CategoryToStringConverter()).map(Animal::getCategory,
AnimalDto::setCategory);
mapper.using(new ColorToOrdinalConverter()).map(Animal::getColor,
AnimalDto::setColor);
});
这是从 Animal
转换为 AnimalDto
的一部分.反之亦然,转换当然需要它自己的映射,在这里我不介绍它,因为我认为要点很清楚.
This is the part of converting from Animal
to AnimalDto
. Converting vice versa of course needs mappings of its own which I do not present here because I think the point came clear.
对于一个类,您现在的做法可能会更好,但是如果您需要转换 Category
&在这样的许多地方 Color
,那么您应该考虑使用可重复使用的转换器.
For one class the way you do it now might be better but if you need to convert Category
& Color
in many places like this then you should consider using converters that are reusable.
这篇关于在具有相同属性名称的不同数据类型上使用ModelMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!