开发者

Mapping Composition in MyBatis

开发者 https://www.devze.com 2023-02-06 09:54 出处:网络
I\'m having some trouble with a mapping in MyBatis for Java and would appreciate some help. My class structure is as below:

I'm having some trouble with a mapping in MyBatis for Java and would appreciate some help. My class structure is as below:

//Getters/setters omitted for clarity

class Foo 
{
  int id;
  Bar bar;
}

class Bar {
  String x;
  int y;
}

My table looks like this - i.e. it is de-normalized from the class structure.

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

My working insert statement is able to de-normalize using (bar.x, bar.y) parameters.

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

So, the problem:

When I execute my select, I would like the resultant object to be an instance of Foo that has a reference to Bar.

I don't think I can use a typ开发者_Python百科e handler since this works over a single column, and and association doesn't seem to make sense since 'Bar' is not an entity explicitly represented in the database via a foreign key relationship.

Can anyone show me the recommended way of doing this please?

Thanks!


Have you tried using resultMap definition in your ibatis sqlmap?

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

and then in you sql just reference the resultMap:

<select id="getFooBar" resultMap="foobar-result">
0

精彩评论

暂无评论...
验证码 换一张
取 消