java Mybatis resultmap 将值映射到模型的哈希映射字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13548349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:10:56  来源:igfitidea点击:

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

javamybatis

提问by om39a

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.

我正在尝试映射创建一个将填充 VehicleVO 的结果映射。我想将几列映射到 VehicleDocuments HashMap。我要填充的数据也存在于同一个表中。

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

我正在尝试使用以下 xml 进行映射。但它似乎不起作用。我得到这个错误

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

“元素类型“resultMap”的内容必须匹配“(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.

如果我的理解是正确的,尝试映射到 hashMap 也可以视为关联映射。但这只是一对一的映射。我也尝试使用<collection>标签进行映射。我仍然得到同样的错误。

采纳答案by Andy

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

不幸的是,您需要实现一个ResultHandler来构建一个 Hashmap。

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

此外,您提到的 DTD 错误是因为结果元素必须在关联元素之前。

回答by Seeta Somagani

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.

'resultMap' 下元素的顺序应按照DTD 的规定进行维护。将所有“结果”标签移动到“关联”标签之前。