2017-04-05 6 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; 

を私はPersonクラスのに対して、 "ID" と "名前" 列をマップしたいです"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>