简体   繁体   中英

Mybatis2 to MyBatis3 Conversion - Results with Same Property

I am working on converting a large MyBatis2 map to a MyBatis3 map, and have run into a bit of an issue where I have a resultMap with multiple result elements using the same property attribute (and the class is generated from a WSDL outside of my control):

<resultMap id="blah" class="someWsdlGeneratedClass">
    <result property="addressLine" 
            resultMap="addressLineOneListMap" 
            javaType="java.util.List" />
    <result property="addressLine" 
            resultMap="addressLineTwoListMap" 
            javaType="java.util.List" />
</resultMap>
<resultMap id="addressLineXListMap" class="string">
    <!-- Result maps are the same except for column -->
    <result property="addressLine" column="COLUMN_X" />
</resultMap>

Notice that both properties are "addressLine".

This works fine for Mybatis2. However, if I try to use the same pattern for MyBatis3, I get an IllegalArgumentException : Result Maps collection already contains value for Mapper.mapper_resultMap[blah]_collection[addressLine]

<resultMap id="blah" type="someWsdlGeneratedClass">
    <collection property="addressLine" 
                resultMap="addressLineOneListMap" 
                javaType="java.util.List" />
    <collection property="addressLine" 
                resultMap="addressLineTwoListMap" 
                javaType="java.util.List" />
</resultMap>

I'd like to avoid writing a wrapper around the generated class in a Dto object, if possible, as that would result in a major refactoring effort in the project. Is there something I can add in the map itself?

You can add a 2nd setter (setAddressLine2) to your generated dto.
In which your code for it can just add to addressLine. ex:

void setAddressLine2(final List<Address> addressLine2) {
    address.addAll(addressLine2);
}

If that's not possible you can try changing your query to return a union of the 2 columns Without knowing your exact query it would look something like:

SELECT foo, addressLine1 as Address
FROM bar
UNION
SELECT foo, addressLine2 as Address
FROM bar

If that isn't possible then you need to create a test project and create an issue on https://github.com/mybatis/mybatis-3 and request a feature.

That option is probably best anyways as I'm not sure you are using it correctly. It seems that your 2nd example (using collection) is correct (conceptually at least. you still can't map to the same property using it) but the first won't behave as you explained it?

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