gpt4 book ai didi

java - MyBatis 中的映射组合

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:02:38 26 4
gpt4 key购买 nike

我在 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”不是通过外键关系在数据库中显式表示的实体.

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

谢谢!

最佳答案

您是否尝试过在您的 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">

关于java - MyBatis 中的映射组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4793600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com