2

(使用 MyBatis v3.0.4。)我有一个不知道如何解决的问题。我的对象模型是:

位置.java


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

地址.java


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

我的 SQL 是:


SELECT 
    // ... other content 
    postal_address_line_1, 
    postal_address_line_2, 
    postal_address_city, 
    cleansed_address_line_1, 
    cleansed_address_line_2, 
    cleansed_address_city, 
    // ... other content

我将如何构造一个resultMap将适当的列插入到正确类型的地址实例中并添加到 Location.java 中的同一列表中?我想避免为了保存不同类型的地址而必须向 Location.java 添加另一个实例变量。

4

2 回答 2

3

在结果图中使用鉴别器标签。

查看mybatis 用户指南。搜索“鉴别器”您会看到更多信息。

<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>

加法1:

您需要将地址选择为不同的行。

IE

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

……

联盟

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

......

那么内置的枚举类型处理程序应该正确设置类型。

于 2011-06-11T00:14:44.510 回答
1

按照 Andy Pryor 的建议,我能够通过将我的 SQL 语句更新为如下内容来解决问题:

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

然后将我resultMap的更新为以下内容:

<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>

最后,在我的Address班级中,我能够拥有setType(Type)并且内置的枚举类型处理程序具有魔力。在Location类中,我只能有一个实例列表,Address并且各种 setXXXAddress() 方法可以适当地添加到该列表中。

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.

于 2011-07-05T10:38:59.563 回答