Ibatis中如何实现一对多关系?

发布于 2024-07-12 09:35:56 字数 697 浏览 6 评论 0原文

假设我有这个类:


    Class A {
        int id;
        int[] b;
        // Other properties
    }

    Class B {
        int id;
        // Other properties
    }

A 类与 B 类具有一对多关系。我已经有一个缓存 B 对象并在 id 上返回它们的服务。

表模式看起来像这样


    Table a:
    -------
      int id,
      prop1,
      etc

    Table a_to_b_map
    ----------------
      int a_id,
      int b_id

现在,我如何在 iBatis 中映射它?

由于B对象已经被缓存,我想将id列表获取到A对象中,然后使用该服务来丰富A。

有人可以建议如何去做吗?

我能想到的两种可能的替代方案是:

  1. 在 A (AtoB 映射)中创建一个内部类,并在 iBatis 配置中使用选择查询来填充它
  2. 在 iBatis resultMap/select 内部使用另一个选择来获取 B id 列表(不太确定)关于如何在配置中执行此操作)

Let's say I have this Class:


    Class A {
        int id;
        int[] b;
        // Other properties
    }

    Class B {
        int id;
        // Other properties
    }

The Class A has one-to-many relation with class B. I already have a service which caches B objects and return them on id.

Table schema looks something like this


    Table a:
    -------
      int id,
      prop1,
      etc

    Table a_to_b_map
    ----------------
      int a_id,
      int b_id

Now, how do I map this in iBatis?

Since, B objects are already cached, I want to get the list of ids into A objects and then use the service to enrich As.

Can someone suggest how to go about it?

Two possible alternative I can think of are:

  1. Create an inner class in A (AtoB map) and use a select query in iBatis config to populate this
  2. Inside the iBatis resultMap/select use another select to get the list of B ids (not too sure on how to do this in config)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌生 2024-07-19 09:35:56

在 mybatis 3 中,情况略有不同。 您可以通过指定两个 select 语句来完成此操作,也可以使用 join 然后使用集合标记创建 resultMap。

<resultMap id=”blogResult” type=”Blog”>
   <collection property="posts" javaType=”ArrayList” column="blog_id"
      ofType="Post" select=”selectPostsForBlog”/>
</resultMap>

<select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>
    SELECT * FROM BLOG WHERE ID = #{id}
    </select>
<select id=”selectPostsForBlog” parameterType=”int” resultType="Author">
    SELECT * FROM POST WHERE BLOG_ID = #{id}
    </select>

或者您可以使用 join

<select id="selectBlog" parameterType="int" resultMap="blogResult">
select
    B.id as blog_id,
    B.title as blog_title,
    B.author_id as blog_author_id,
    P.id as post_id,
    P.subject as post_subject,
    P.body as post_body,
from Blog B
    left outer join Post P on B.id = P.blog_id
where B.id = #{id}
</select>

并执行结果映射,

<resultMap id="blogResult" type="Blog">
  <id property=”id” column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <result property="body" column="post_body"/>
  </collection>
</resultMap>

您可以从此处的 ibatis 用户指南获取完整的教程:

http://svn.apache.org/repos/asf/ibatis/java/ibatis-3/trunk/doc/en/iBATIS- 3-用户指南.pdf

in mybatis 3 it's little bit different. You can do it by specify two select statement or you can use join then create resultMap with collection tag.

<resultMap id=”blogResult” type=”Blog”>
   <collection property="posts" javaType=”ArrayList” column="blog_id"
      ofType="Post" select=”selectPostsForBlog”/>
</resultMap>

<select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>
    SELECT * FROM BLOG WHERE ID = #{id}
    </select>
<select id=”selectPostsForBlog” parameterType=”int” resultType="Author">
    SELECT * FROM POST WHERE BLOG_ID = #{id}
    </select>

or you can use join

<select id="selectBlog" parameterType="int" resultMap="blogResult">
select
    B.id as blog_id,
    B.title as blog_title,
    B.author_id as blog_author_id,
    P.id as post_id,
    P.subject as post_subject,
    P.body as post_body,
from Blog B
    left outer join Post P on B.id = P.blog_id
where B.id = #{id}
</select>

and do result map

<resultMap id="blogResult" type="Blog">
  <id property=”id” column="blog_id" />
  <result property="title" column="blog_title"/>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <result property="body" column="post_body"/>
  </collection>
</resultMap>

you can get complete totorial from ibatis user guide here :

http://svn.apache.org/repos/asf/ibatis/java/ibatis-3/trunk/doc/en/iBATIS-3-User-Guide.pdf

永不分离 2024-07-19 09:35:56

不确定我是否正确理解了你的问题。

假设你要根据A的id进行查询,那么在ibatis中编写一个连接两个表的查询怎么样?

select * 
from a, a_to_b_map 
where a.id = #id# and a.id = a_to_b_map.a_id

然后,您可以使用“queryForMap”返回 a_id 与(查询中的记录集合)的哈希图。 使用自定义方法将此数据结构转换为“A”的对象

Not sure if I have understood your question correctly.

Assuming you will query based on A's id, how about writing a query in ibatis that joins the two tables?

select * 
from a, a_to_b_map 
where a.id = #id# and a.id = a_to_b_map.a_id

You can then use a 'queryForMap' to return a hashmap of a_id vs (collection of records from the query). Use a custom method to convert this data structure into an object of 'A'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文