首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Mybatis关联查询方式

Mybatis关联查询方式

作者头像
名字是乱打的
发布2021-12-22 15:56:15
发布2021-12-22 15:56:15
33700
代码可运行
举报
文章被收录于专栏:软件工程软件工程
运行总次数:0
代码可运行
1.嵌套结果

比如我们想通过查询一个文章,返回这个文章和这个文章的作者的信息

代码语言:javascript
代码运行次数:0
运行
复制
 <!--pojo  BlogAndAuthor.java-->
public class BlogAndAuthor implements Serializable {
    Integer bid; // 文章ID
    String name; // 文章标题
    Author author; // 作者
  <!--省略set get tostring,不在这展示了-->
}
代码语言:javascript
代码运行次数:0
运行
复制
 <!--pojo  Author.java-->
public class Author implements Serializable {
    Integer authorId; // 作者ID
    String authorName; // 作者名称

    public Integer getAuthorId() {
        return authorId;
    }

    public void setAuthorId(Integer authorId) {
        this.authorId = authorId;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }
}

封装一个结果集,内部对对象属性对一个结果映射

代码语言:javascript
代码运行次数:0
运行
复制
<!-- 根据文章查询作者,一对一查询的结果,嵌套结果 -->
    <resultMap id="BlogWithAuthorResultMap" type="BlogAndAuthor">
        <id column="bid" property="bid" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <!-- 联合查询,将author的属性映射到ResultMap -->
        <association property="author" javaType="Author">
            <id column="author_id" property="authorId"/>
            <result column="author_name" property="authorName"/>
        </association>
    </resultMap>
查询语句--结果集映射为我们上面自己定义的
代码语言:javascript
代码运行次数:0
运行
复制
 <!-- 根据文章查询作者,一对一,嵌套结果,无N+1问题 -->
    <select id="selectBlogWithAuthorResult" resultMap="BlogWithAuthorResultMap" >
        select b.bid, b.name, b.author_id, a.author_id , a.author_name
        from blog b
        left join author a
        on b.author_id=a.author_id
        where b.bid = #{bid, jdbcType=INTEGER}
    </select>
2.嵌套查询
代码语言:javascript
代码运行次数:0
运行
复制
    <!-- 另一种联合查询(一对一)的实现,但是这种方式有“N+1”的问题 -->
    <resultMap id="BlogWithAuthorQueryMap" type="BlogAndAuthor">
        <id column="bid" property="bid" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <association property="author" javaType="Author"
                     column="author_id" select="selectAuthor"/> 
    </resultMap>
代码语言:javascript
代码运行次数:0
运行
复制
 <!-- 嵌套查询 -->
    <select id="selectAuthor" parameterType="int" resultType="Author">
        select author_id authorId, author_name authorName
        from author where author_id = #{authorId}
    </select>
代码语言:javascript
代码运行次数:0
运行
复制
<select id="selectBlogWithAuthorQuery" resultMap="BlogWithAuthorQueryMap" >
       select b.bid, b.name, b.author_id, a.author_id , a.author_name
       from blog b
       left join author a
       on b.author_id=a.author_id
       where b.bid = #{bid, jdbcType=INTEGER}
   </select>

这种情况会有N+1的问题,我们可以开启来加载来解决(原理动态代理)

代码语言:javascript
代码运行次数:0
运行
复制
    <!-- 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。默认 false  -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 当开启时,任何方法的调用都会加载该对象的所有属性。默认 false,可通过select标签的 fetchType来覆盖-->
        <setting name="aggressiveLazyLoading" value="false"/>
        <!--  Mybatis 创建具有延迟加载能力的对象所用到的代理工具,默认JAVASSIST -->
        <!--<setting name="proxyFactory" value="CGLIB" />-->
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/10/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.嵌套结果
    • 查询语句--结果集映射为我们上面自己定义的
  • 2.嵌套查询
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档