2017-04-05 199 views
1

我已經定義了一個Java bean是這樣的:MyBatis的自定義映射

public class Person{ 
    private int id; 
    private String name; 
    private Map<String, Object> properties; 
    // getters and setters ... 
} 

給出以下查詢:

select id, name, age, address from users; 

我要地圖 「ID」 和 「名稱」 列到Person類的「id」和「name」屬性,但將「age」和「address」列映射到「properties」映射中作爲鍵值對。

這可能與mybatis?我正在使用最新版本的mybatis。

回答

0

以下應該做你所期望的:

<resultMap id="personResultMap" type="Person"> 
    <id property="id" column="id"/> 
    <result property="name" column="name"/> 
    <association property="properties" javaType="map"> 
    <result property="age" column="age"/> 
    <result property="address" column="address"/> 
</association> 
</resultMap> 

使用隱式映射(列 - >屬性名稱匹配),以下甚至可能是不夠的:

<resultMap id="personResultMap" type="Person"> 
    <association property="properties" javaType="map" /> 
</resultMap>