我们有一些我们试图映射的遗留数据......遗留数据包含月份日的字段...
是否可以转换
MyObject.day
MyObject.year
MyObject.month
Run Code Online (Sandbox Code Playgroud)
至
MyOtherObject.date
Run Code Online (Sandbox Code Playgroud)
我找不到关于这个主题的任何文件.任何人将不胜感激.
我知道这是一篇旧文章,但我找不到满意的答案,花了很多时间,然后发现了这个(我认为)简单的方法。您可以将 ConfigurableCustomConver 与您的mapping.xml 中的“this”引用结合使用。
例如:
public class Formatter implements ConfigurableCustomConverter
{
private String format;
private String[] fields;
@Override
public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) {
List valueList = new ArrayList();
for (String field : fields){
try {
valueList.add(sourceClass.getMethod(field).invoke(sourceFieldValue));
}
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Reflection error during mapping", e);
}
}
return MessageFormat.format(format, valueList.toArray());
}
@Override
public void setParameter(String parameter)
{
String[] parameters = parameter.split("\\|");
format = parameters[0];
fields = Arrays.copyOfRange( parameters, 1, parameters.length);
}
}
Run Code Online (Sandbox Code Playgroud)
在你的mapping.xml中:
<mapping type="one-way">
<class-a>test.model.TestFrom</class-a>
<class-b>test.model.TestTo</class-b>
<field custom-converter="nl.nxs.dozer.Formatter" custom-converter-param="{0}{1}|getFirstValue|getSecondValue">
<a>this</a>
<b>combinedValue</b>
</field>
</mapping>
Run Code Online (Sandbox Code Playgroud)
课程:
public class TestFrom
{
private String firstValue;
private String secondValue;
public String getFirstValue()
{
return firstValue;
}
public void setFirstValue(String firstValue)
{
this.firstValue = firstValue;
}
public String getSecondValue()
{
return secondValue;
}
public void setSecondValue(String secondValue)
{
this.secondValue = secondValue;
}
}
public class TestTo
{
private String combinedValue;
public String getCombinedValue(){
return combinedValue;
}
public void setCombinedValue(String combinedValue){
this.combinedValue = combinedValue;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5796 次 |
| 最近记录: |