简体   繁体   中英

Mybatis resultmap to map values to hash map field of a model

I am trying to map create a result map that will populate vehicleVO. I want to map few columns to vehicleDocuments HashMap. I am having the data to be populated also present in the same table.

public class VehicleVO implements Serializable {
    public String vehicleId;
    public String vehicleNumber;
    public String model;
    public Map<String, Date> vehicleDocuments;
    public TransportVO transport;
    public String distanceTraveled;

}

I am trying to use the following xml for mapping. But it don't seem to work. I a gettign this error

"The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".

<resultMap id="BaseResultMap" type="com.svms.service.vo.VehicleVO">
        <id column="vehicle_id" jdbcType="BIGINT" property="vehicleId" />
        <result column="vehicle_no" jdbcType="VARCHAR" property="vehicleNumber" />
        <result column="Model" jdbcType="VARCHAR" property="model" />
        <association property="vehicleDocuments" javaType="java.util.HashMap">
            <result column="FC" jdbcType="DATE" property="FC_TD" />
            <result column="TAX" jdbcType="DATE" property="TAX_TD" />
            <result column="Insureance" jdbcType="DATE" property="INSURANCE_TD" />
            <result column="Form47" jdbcType="DATE" property="FORM47_TD" />
            <result column="NC" jdbcType="DATE" property="NC_TD" />
        </association>
        <result column="total_distance" jdbcType="INTEGER" property="distanceTraveled" />
        <result column="transport_id" jdbcType="BIGINT" property="transportId" />
</resultMap>

If my understanding is correct, Trying to map to an hashMap can also be considered as association mapping. But this is a one to one mapping only. I also tried using the <collection> tag for mapping. Still I get the same error.

The order of elements under 'resultMap' should be maintained as dictated by the DTD . Move all the 'result' tags to appear before the 'association' tag.

You'll need to implement a ResultHandler to build a Hashmap unfortunately.

Also, the DTD error you mention is because the result elements must be before the association elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM