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>
所以,问题是:
当我执行选择时,我希望生成的对象是引用 Bar 的 Foo 实例。
我认为我不能使用类型处理程序,因为这在单个列上工作,并且关联似乎没有意义,因为“Bar”不是通过外键关系在数据库中显式表示的实体。
谁能告诉我推荐的方法吗?
谢谢!
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 type 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在 ibatis sqlmap 中使用 resultMap 定义?
然后在你的sql中只需引用resultMap:
Have you tried using resultMap definition in your ibatis sqlmap?
and then in you sql just reference the resultMap: