题
我在Mybatis的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">
不隶属于 StackOverflow