Mybatis的关联查询

1:一对一的处理:作者表和博客表示一对一的关系

<select id="selectBlogWithAuthor" resultMap="blogResultWithAuthor"> 
        SELECT
          b.id        as blog_id, 
          b.name      as blog_title, 
          b.author_id as blog_author_id, 
          a.id        as author_id, 
          a.username  as author_username, 
          a.password  as author_password, 
          a.email     as author_email, 
          a.bio       as author_bio 
        FROM blog b LEFT OUTER JOIN author a ON b.author_id=a.id 
        WHERE b.id = #{id} 
    </select> 
    <resultMap id="blogResultWithAuthor" type="blog"> 
        <id property="id" column="blog_id"/> 
        <result property="name" column="blog_title"/> 
        <association property="author" column="blog_author_id" javaType="author" resultMap="authorResult"/> 
    </resultMap> 
    <resultMap id="authorResult" type="author"> 
        <id property="id" column="author_id"/> 
        <result property="username" column="author_username"/> 
        <result property="password" column="author_password"/> 
        <result property="email" column="author_email"/> 
        <result property="bio" column="author_bio"/> 
    </resultMap> 

1> 首先看到的是select标签,其中id表示对应的接口的方法名。

        resultMap的值是一个resultMap节点的id,这个表示select查询结果的映射方式。

2> 然后就是resultMap最后所指向的节点blogResultWithAuthor。

  • resultMap节点的id就是唯一标识这个节点的,type表示这个resultMap最终映射成的实体类Blog。
  • id是主键映射,这个和mybatis缓存有关。



 

对于resultMap id=authorResult 也是一样的。


2: 对于一对多的处理: 生命博客表和博文表是一对多的关系


 <select id="selectBlogWithPost" resultMap="blogResultWithPost">
        SELECT
        b.id    AS   blog_id,
        b.name  AS   blog_name,
        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="blogResultWithPost" type="Blog">
        <id property="id" column="blog_id"/>
        <result property="name" column="blog_name"/>
        <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>

 集合和关联几乎完全一样,只不过一个是association一个是collection,一个是一对一,一个是一对多,  对于collection中的一些参数。


注意:

这个对你所映射的内容没有深度,广度或关联和集合相联合的限制。当映射它们 时你应该在大脑中保留它们的表现。 你的应用在找到最佳方法前要一直进行的单元测试和性 能测试。 只是一个简单的了解, 还需深入学习。

 对于复用的部分可以参考:http://www.cnblogs.com/woshimrf/p/5697617.html#association


MyBatis关联查询是指在查询数据库时,通过配置映射文件,将多个表之间的关联数据查询出来并进行组合。可以使用嵌套结果映射或嵌套查询来处理关联查询。 在嵌套结果映射中,可以使用\<resultMap>来定义映射关系,并使用\<association>来设置关联对象。例如,可以定义一个resultMap来映射Classes和Teacher表之间的关联关系,然后通过联表查询将两个表的数据关联起来。 另一种方式是使用嵌套查询,即在映射文件中通过\<select>标签设置嵌套查询语句来获取关联数据。通过联表查询获取到的数据可以作为嵌套查询的参数,从而获取到关联数据。 以上就是MyBatis关联查询的基本概念和使用方式。可以根据具体的需求选择合适的方式进行关联查询操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SSM框架的学习与应用-Java EE企业级应用开发学习记录(第四天)Mybatis关联映射和缓存机制](https://download.youkuaiyun.com/download/m0_53659738/88253127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mybatis关联查询](https://blog.youkuaiyun.com/faramita_of_mine/article/details/122202515)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值