MyBatis的自定义映射
问题描述:
我已经定义了一个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。
答
以下应该做你所期望的:
<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>