3

我在 MyBatis for Java 中的映射遇到了一些问题,希望能得到一些帮助。我的班级结构如下:

//Getters/setters omitted for clarity

class Foo 
{
  int id;
  Bar bar;
}

class Bar {
  String x;
  int y;
}

我的表看起来像这样 - 即它是从类结构中去规范化的。

create table foo_bar (
  id int,
  x varchar,
  y int
);

我的工作插入语句能够使用 (bar.x, bar.y) 参数进行反规范化。

<insert id="insert" parameterType="foo">
  <![CDATA[
    insert into foo_bar
    (id, x, y) values
    (#{x}, #{bar.x}, #{bar.y})
  ]]>
</insert>

所以,问题:

当我执行我的选择时,我希望结果对象是一个引用 Bar 的 Foo 实例。

我认为我不能使用类型处理程序,因为它适用于单个列,并且关联似乎没有意义,因为“Bar”不是通过外键关系在数据库中显式表示的实体。

谁能告诉我推荐的方法吗?

谢谢!

4

1 回答 1

3

您是否尝试过在您的 ibatis sqlmap 中使用 resultMap 定义?

<resultMap id="foobar-result" class="Foo">
    <result property="id" column="id"/>
    <result property="bar.x" column="x"/>
    <result property="bar.y" column="y"/>
</resultMap>

然后在你的sql中只引用resultMap:

<select id="getFooBar" resultMap="foobar-result">
于 2011-02-01T01:25:06.650 回答