美文网首页
Mybatis关联查询方式

Mybatis关联查询方式

作者: 名字是乱打的 | 来源:发表于2020-03-10 00:08 被阅读0次

1.嵌套结果

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

 <!--pojo  BlogAndAuthor.java-->
public class BlogAndAuthor implements Serializable {
    Integer bid; // 文章ID
    String name; // 文章标题
    Author author; // 作者
  <!--省略set get tostring,不在这展示了-->
}
 <!--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;
    }
}

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

<!-- 根据文章查询作者,一对一查询的结果,嵌套结果 -->
    <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>
查询语句--结果集映射为我们上面自己定义的
 <!-- 根据文章查询作者,一对一,嵌套结果,无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.嵌套查询

    <!-- 另一种联合查询(一对一)的实现,但是这种方式有“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>
 <!-- 嵌套查询 -->
    <select id="selectAuthor" parameterType="int" resultType="Author">
        select author_id authorId, author_name authorName
        from author where author_id = #{authorId}
    </select>
<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的问题,我们可以开启来加载来解决(原理动态代理)

    <!-- 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。默认 false  -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 当开启时,任何方法的调用都会加载该对象的所有属性。默认 false,可通过select标签的 fetchType来覆盖-->
        <setting name="aggressiveLazyLoading" value="false"/>
        <!--  Mybatis 创建具有延迟加载能力的对象所用到的代理工具,默认JAVASSIST -->
        <!--<setting name="proxyFactory" value="CGLIB" />-->

相关文章

  • MyBatis 最佳实践篇 3:关联查询懒加载

    MyBatis 中联合查询可分为关联查询和关联结果两种方式(具体查看 Mybatis 文档篇 3.5:Mapper...

  • Mybatis关联查询方式

    1.嵌套结果 比如我们想通过查询一个文章,返回这个文章和这个文章的作者的信息 封装一个结果集,内部对对象属性对一个...

  • 关于关联查询的处理

    mybatis关联查询配置有2种:关联嵌套结果,关联嵌套查询关联的查询过程中如果关联的表可能查到很多数据,应该考虑...

  • mybatis进阶2——关联查询

    关联查询代码参考mybatis-demo测试代码AssociationQueryTest.java 0.关联查询的...

  • Maven项目集成Mybatis 一对多、多对一 查询嵌套&结

    MyBatis在映射文件中加载关联对象的关系主要通过两种方式:查询嵌套、结果嵌套查询嵌套:类似于子查询,它需要执行...

  • Mybatis关联查询

    1. mysql中的LEFT JOIN 使用left join是以左表为主,即使右表没有数据,也会出来左表的所有数...

  • Mybatis | 关联查询

    使用Mybatis进行关联查询,之前发在我的CSDN博客中,现在搬到简书上来。 数据库关系图 如图是一个博客系统的...

  • MyBatis关联查询

    【目录】1 一对一2 一对多 在项目开始之前准备好数据库。数据库database: 1 一对一 相关代码:主配置文...

  • python面试题01

    1、什么是多表关联查询,有几种多表关联的查询方式,分别是什么? 多表关联查询概念: 多表关联查询分类:1.1内连接...

  • MyBatis学习:MyBatis中的延迟加载

    MyBatis中的延迟加载。 1. 什么是延迟加载 例子:如果查询订单并且关联查询用户信息。如果先查询订单信息...

网友评论

      本文标题:Mybatis关联查询方式

      本文链接:https://www.haomeiwen.com/subject/gtzmdhtx.html