Comparison of composition in MyBatis

I am having trouble displaying in MyBatis for Java and would really like to help. My class structure is as follows:

//Getters/setters omitted for clarity

class Foo 
{
  int id;
  Bar bar;
}

class Bar {
  String x;
  int y;
}

My table looks like this: that is, it is de-normalized from the class structure.

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

My operational insert can de-normalize the parameters (bar.x, bar.y).

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

So the problem is:

When I make my selection, I would like the resulting object to be an instance of Foo that has a reference to Bar.

I don’t think I can use a type handler, since this works on a single column, and the association does not make sense either, since “Bar” is not an object explicitly represented in the database through a foreign key relationship.

Can someone show me the recommended way to do this, please?

Thank!

+3
1

resultMap 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>

sql resultMap:

<select id="getFooBar" resultMap="foobar-result">
+2

Source: https://habr.com/ru/post/1787677/


All Articles