0

我正在尝试使用 JMapper 从 StaticS 映射到 StaticD 类。以下是配置。

<jmapper>
  <class name="com.StaticD">
    <global>
       <value name="map"/>
    </global>
  </class>
  <class name="com.StaticS">
    <conversion name="toDate" from="map" to="startDate, endDate">
        return new java.text.SimpleDateFormat("dd/MM/yyyy").parse((String)${source}.get("${destination.name}"));
    </conversion>
    <conversion name="toString" from="map" to="securityName, id">
        return (${destination.type}) ${source}.get("${destination.name}");
    </conversion>
  </class>
</jmapper>

课程如下:

public class StaticS {

    private Map<String, String> map;

    public Map<String, String> getMap()
    {
        return map;
    }

    public StaticS(Map<String, String> map) {
        super();
        this.map = map;
    }
}


public class StaticD {

    private Date startDate;

    private Date endDate;

    private String id;

    private String securityName;

    public Date getStartDate() {
        return startDate;
    }
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    public Date getEndDate() {
        return endDate;
    }
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    public String getId()
    {
        return id;
    }
    public void setId(String id)
    {
        this.id = id;
    }
    public String getSecurityName() {
        return securityName;
    }
    public void setSecurityName(String securityName) {
        this.securityName = securityName;
    }
    public StaticD() {}
}

我调用映射器框架来映射这两个文件的方式是:

Map<String, String> map = new HashMap<>();
        map.put("id", "JMapper Framework v.1.2.0");
        map.put("endDate", "22/08/1988");
        map.put("startDate", "22/08/1987");
        map.put("securityName", "IBM");

        // ANNOTATION
        JMapper<StaticD, StaticS> mapper = new JMapper<StaticD, StaticS>(StaticD.class, StaticS.class, "staticConversion.xml");
        StaticD destination = mapper.getDestination(new StaticS(map));
        System.out.println(destination);

实际输出如下:

StaticD{id=JMapper Framework v.1.2.0, startDate=Sat Aug 22 00:00:00 CDT 1987, endDate=Sat Aug 22 00:00:00 CDT 1987, securityName=JMapper Framework v.1.2.0}

预期输出如下:

StaticD{id=JMapper Framework v.1.2.0, startDate=Sat Aug 22 00:00:00 CDT 1987, endDate=Sat Aug 22 00:00:00 CDT 1988, securityName=IBM}

如您所见,startDate 将在实际输出中同时指向 StaticD 类中的 start 和 endDate 属性。

如果我对每个属性使用单独的转换,它就可以工作,但是当我对两个不同的属性使用相同的转换时,映射不起作用。

例如,以下工作,但代码是多余的

<conversion name="toString" from="map" to="securityName">
            return (${destination.type}) ${source}.get("${destination.name}");
        </conversion>
<conversion name="toString" from="map" to="id">
            return (${destination.type}) ${source}.get("${destination.name}");
        </conversion>

如果我在 to 参数中使用多个属性,您能否建议为什么上述用于映射这两个类的配置不起作用?

4

1 回答 1

1

在进行转换时,我必须在 xml 文件中指定 type="DYNAMIC"。

<conversion name="toDate" from="map" to="startDate, endDate" type="DYNAMIC">
        return new java.text.SimpleDateFormat("dd/MM/yyyy").parse((String)${source}.get("${destination.name}"));
    </conversion> 
于 2013-11-04T16:25:23.347 回答