我有同樣的問題,目前(如推土機5.5.x)沒有簡單的方法,但有複雜的。
請注意,它依賴於在JVM中未啓用安全管理器,否則您將需要在安全規則中添加少量權限。這是因爲此解決方案使用反射來訪問Dozer類的私有字段。
您需要擴展2個類:DozerBeanMapper
和MappingProcessor
。您還需要枚舉方向和界面才能從上方獲取方向。
枚舉:
public enum Direction {
TO,
FROM;
}
接口:
public interface DirectionAware {
Direction getDirection();
}
類延伸DozerBeanMapper
:
public class DirectionAwareDozerBeanMapper extends DozerBeanMapper implements DirectionAware {
private Direction direction;
public DirectionAwareDozerBeanMapper(Direction direction) {
super();
this.direction = direction;
}
public DirectionAwareDozerBeanMapper(Direction direction, List<String> mappingFiles) {
super(mappingFiles);
this.direction = direction;
}
@Override
protected Mapper getMappingProcessor() {
try {
Method m = DozerBeanMapper.class.getDeclaredMethod("initMappings");
m.setAccessible(true);
m.invoke(this);
} catch (NoSuchMethodException|SecurityException|IllegalAccessException|IllegalArgumentException|InvocationTargetException e) {
// Handle the exception as you want
}
ClassMappings arg1 = (ClassMappings)getField("customMappings");
Configuration arg2 = (Configuration)getFieldValue("globalConfiguration");
CacheManager arg3 = (CacheManager)getField("cacheManager");
StatisticsManager arg4 = (StatisticsManager)getField("statsMgr");
List<CustomConverter> arg5 = (List<CustomConverter>)getField("customConverters");
DozerEventManager arg6 = (DozerEventManager)getField("eventManager");
Map<String, CustomConverter> arg7 = (Map<String, CustomConverter>)getField("customConvertersWithId");
Mapper mapper = new DirectionAwareMappingProcessor(arg1, arg2, arg3, arg4, arg5,
arg6, getCustomFieldMapper(), arg7, direction);
return mapper;
}
private Object getField(String fieldName) {
try {
Field field = DozerBeanMapper.class.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(this);
} catch (NoSuchFieldException|SecurityException|IllegalArgumentException|IllegalAccessException e) {
// Handle the exception as you want
}
return null;
}
public Direction getDirection() {
return direction;
}
}
類延伸MappingProcessor
:
public class DirectionAwareMappingProcessor extends MappingProcessor implements DirectionAware {
private Direction direction;
protected DirectionAwareMappingProcessor(ClassMappings arg1, Configuration arg2, CacheManager arg3, StatisticsManager arg4, List<CustomConverter> arg5, DozerEventManager arg6, CustomFieldMapper arg7, Map<String, CustomConverter> arg8, Direction direction) {
super(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
this.direction = direction;
}
public Direction getDirection() {
return direction;
}
}
現在,用法。
1)每次你想映射相同的基本類型(例如String-String)時,在這個dozer映射文件中使用這個類型的DozerConverter
作爲自定義轉換器。這種轉換器的實現應擴展:DozerConverter<String,String>
並實現MapperAware
接口。這很重要,因爲您有MapperAware
可用,因爲有了映射器,您可以將它投射到DirectionAware
,然後獲取方向。
例如:
public class MyMapper extends DozerConverter<String, String> implements MapperAware {
private DirectionAware dirAware;
public MyMapper(Class<String> cls) {
super(cls, cls);
}
@Override
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<String> destinationClass, Class<String> sourceClass) {
if (dirAware.getDirection() == Direction.FROM) {
// TODO convert sourceFieldValue for "FROM" direction and return it
} else {
// TODO convert sourceFieldValue for "TO" direction and return it
}
}
@Override
public void setMapper(Mapper mapper) {
dirAware = (DirectionAware)mapper;
}
}
2)你需要創建2個全球推土機映射對象,每個映射方向之一。他們應該配置相同的映射文件,但具有不同的方向參數。例如:
DirectionAwareDozerBeanMapper mapperFrom = DirectionAwareDozerBeanMapper(mappingFiles, Direction.FROM);
DirectionAwareDozerBeanMapper mapperTo = DirectionAwareDozerBeanMapper(mappingFiles, Direction.TO);
當然,你需要使用適當的映射器(自/至)向哪個方向你映射的自定義映射器提供的信息中。
我有新的API相同的問題。我不知道映射器是否會調用convertTo()或convertFrom()。較舊的API不起作用,因爲我仍然無法區分映射的工作方向。我將從String轉換爲String。簡單的映射,我需要將數據庫代碼映射到用戶可讀的值。如果任何人有處理這一點將讚賞反饋。謝謝 – 2011-10-28 17:08:01