美文网首页
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关联查询方式

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