开发者

Constructing resultMap to create different instances for the same collection

(Using MyBatis v3.0.4.) I have a problem that I do not know how to solve. My object model is:

Location.java


public class Location {
    // ... other content
    private List addresses; 
    // ... other content
}

Address.java


public class Address { 
    public enum Type { POSTAL, POBOX, INPUT, CLEANSED } 
    private Type type; 
    private String line1; 
    // ... other content
}

My SQL is:


SELECT 
    // ... other content 
    postal_address_line_1, 
    postal_address_line_2, 
    po开发者_Python百科stal_address_city, 
    cleansed_address_line_1, 
    cleansed_address_line_2, 
    cleansed_address_city, 
    // ... other content

How would I construct a resultMap that would plug the appropriate columns into an address instance of the correct type and added to the same list in Location.java? I would like to avoid having to add another instance variable to Location.java just to hold a different type of address.


Use a discriminator tag in your result map.

Look at the mybatis user guide. Search for "discriminator" you see more informations.

<resultMap id="vehicleResult" type="Vehicle">
   <id property=”id” column="id" />
   <result property="sharedPropA" column="shared_column"/>
   <discriminator javaType="int" column="address_type">
     <case value="1" resultMap="postalResultMap"/>
     <case value="2" resultMap="inputResultMap"/>
     <case value="3" resultMap="cleanResultMap"/>
     <case value="4" resultMap="whatIsaCleansedAddressResultMap"/>
   </discriminator>
</resultMap>

Addition 1:

You need to select the addresses as different rows.

i.e

select
    postal_address_line_1 as line1, 
    postal_address_line_2 as line2, 
    postal_address_city as city,
    type as 'POSTAL'

....

union

select
    postal_address_line_1 as line1, 
    postal_address_line_2 as line2, 
    postal_address_city as city,
    type as 'CLEANSED'

.....

then the built in enum type handler should set the type correctly.


Along the lines of Andy Pryor's suggestion, I was able to solve the problem by updating my SQL statement to something like the following:

SELECT 
// ... other content 
'POSTAL' as Postal_Address_Type,
postal_address_line_1, 
postal_address_line_2, 
postal_address_city,
'CLEANSED' as Cleansed_Address_Type,
cleansed_address_line_1, 
cleansed_address_line_2, 
cleansed_address_city, 
// ... other content

Then update my resultMap to the following:

<resultMap ...>
    //... other content
    <association property="postalAddress" javaType="com.x.y.z.Address">
        <result property="type" column="Postal_Address_Type"/>
        <result property="line1" column="Address_Part_1_Name"/>
        <result property="line2" column="Address_Part_2_Name"/>
        //...other content
    </association>
    <association property="cleansedAddress" javaType="com.x.y.z.Address">
        <result property="type" column="Cleansed_Address_Type"/>
        <result property="line1" column="Address_Part_1_Name"/>
        <result property="line2" column="Address_Part_2_Name"/>
        //...other content
    </association>
</resultMap>

Finally, within my Address class I am able to have setType(Type) and the inbuilt enumerated type handler does the magic. Within the Location class I can just have one list of instances of Address and the various setXXXAddress() methods can add to this list appropriately.

It is unfortunate that I cannot plug the columns into some sort of factory class but putting hard-coded types into the SQL statement isn't too dirty, in my opinion. The disadvantage is that I have introduced coupling between the domain model's Address.Type values and the SQL statement but this is kind of already there given that the resultMap SQL XML needs to hold the names of instance variables in the Address class anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜