在MyBatis中映射组合

时间:2011-01-25 12:39:21

标签: java sql ibatis mybatis

我在使用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>

所以,问题是:

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

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

有人能告诉我这样做的推荐方法吗?

谢谢!

1 个答案:

答案 0 :(得分: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">