MyBatis:高级结果映射

时间:2017-10-12 07:35:05

标签: sql mybatis

我有以下结构的实体:

id: 123,
key1: bla1
properties: {
  key2: bla2
}

我需要从DB中选择这样的实体并将其映射到:

id: 123,
properties: {
  key1: bla1
  key2: bla2
}

无论是在SELECT级别还是在resultMap(MyBatis)级别上都无关紧要。

1 个答案:

答案 0 :(得分:2)

所以SQL是:

SELECT t1.id, t1.key1, p.key2 FROM t1 JOIN properties p ON [join clause] 

结果图:

<resultMap id="rm">
  <id column="id", property="id">
  <association property="properties" type="">
    <result column="key1" property="key1" />
    <result column="key2" property="key2" />
  </association>
</resultMap>

虽然第一个实体映射的结果映射如下所示,但列/属性在主对象中,不关联:

<resultMap id="rm">
  <id column="id", property="id">
  <result column="key1" property="key1" />
  <association property="properties" type="">
    <result column="key2" property="key2" />
  </association>
</resultMap>