Mybatis源码学习五嵌套查询及循环依赖问题的解决

ResultMap结果集映射

Mybatis源码学习五嵌套查询及循环依赖问题的解决

Mybatis源码学习五嵌套查询及循环依赖问题的解决

association和collection代码示例

association作用于1对1

collection作用于1对多

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.BlogMapper">

    <resultMap id="bolgMap" type="MetaObject.Blog" autoMapping="true">
        <result column="title" property="title"/>
        <association property="author" column="author_id" select="selectUserByUserId"/>
        <collection property="comments" column="id" select="selectCommentByBlogId"/>
    </resultMap>

    <select id="selectBlogById" resultMap="bolgMap">
        select * from blog where id = #{id}
    </select>

    <select id="selectUserByUserId" resultType="model.User">
        select * from user where id = #{userId}
    </select>

    <select id="selectCommentByBlogId" resultType="model.Comment">
        select * from comment where blog_id = #{blogId}
    </select>
</mapper>
package MetaObject;

import model.Comment;
import model.User;

import java.util.List;
import java.util.Map;

/**
 * @author lingyujia
 * @version v1
 * @description Blog
 * @since 2022/3/14 19:53
 */

public class Blog {

    private int id;

    private String title;

    private User author;

    private String body;

    private List<Comment> comments;

    Map<String, String> labels;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        System.out.println("调用setAuthor");
        this.author = author;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Map<String, String> getLabels() {
        return labels;
    }

    public void setLabels(Map<String, String> labels) {
        this.labels = labels;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @Override
    public String toString() {
        return "Blog{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author=" + author +
                ", body='" + body + '\'' +
                ", comments=" + comments +
                ", labels=" + labels +
                '}';
    }
}

Mybatis源码学习五嵌套查询及循环依赖问题的解决

嵌套查询

嵌套查询是指当一个主查询中含有一个子查询,而子查询又会调用主查询

类似下图中查询blog表语句时会触发查询comment表,而查询comment表时又会调用查询blog表语句

Mybatis源码学习五嵌套查询及循环依赖问题的解决

代码示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.BlogMapper">

    <resultMap id="bolgMap" type="MetaObject.Blog" autoMapping="true">
        <result column="title" property="title"/>
        <collection property="comments" column="id" select="selectCommentByBlogId" fetchType="eager"/>
    </resultMap>

    <resultMap id="commentMap" type="model.Comment" >
        <association column="blog_id" property="blog" select="selectBlogById" fetchType="eager"/>
    </resultMap>

    <select id="selectBlogById" resultMap="bolgMap">
        select * from blog where id = #{id}
    </select>

    <select id="selectCommentByBlogId" resultMap="commentMap">
        select * from comment where blog_id = #{blogId}
    </select>
</mapper>
public class Comment {

    private int id;
    private int blogId;
    private String body;

    private Blog blog;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getBlogId() {
        return blogId;
    }

    public void setBlogId(int blogId) {
        this.blogId = blogId;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Blog getBlog() {
        return blog;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", blogId=" + blogId +
                ", body='" + body + '\'' +
                ", blog=" + blog +
                '}';
    }

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 能正确查询出结果,不会出现死循环。注:不能打印结果,否则会造成栈内存溢出。

循环依赖解析

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 

源码分析循环依赖的解析原理

sql可以看出第一次是主查询,查询blog。queryStack会从0加1 

Mybatis源码学习五嵌套查询及循环依赖问题的解决

第一次一级缓存为空,会查询数据库操作

Mybatis源码学习五嵌套查询及循环依赖问题的解决

存储到一级缓存,设置个固定值,为占位符

Mybatis源码学习五嵌套查询及循环依赖问题的解决

填充结果:key可以看出是子查询comment,第一次查询一级缓存肯定为空。这里设置的懒加载为false(fetchType=”eager”)

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 Mybatis源码学习五嵌套查询及循环依赖问题的解决

不涉及二级缓存,二级缓存为空,可以不考虑

Mybatis源码学习五嵌套查询及循环依赖问题的解决

sql可以看出触发子查询

Mybatis源码学习五嵌套查询及循环依赖问题的解决

queryStack从1加到2, 一级缓存为空,查询数据库操作

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 存储到一级缓存,设置个固定值,为占位符

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 填充子查询的结果,通过key可以看出是将blog的数据填充到子查询中,此时一级缓存一级设置blog的数据,会进入下一步:延迟加载

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 canLoad的方法结果为false,因为此时一级缓存中设置的还只是占位符Mybatis源码学习五嵌套查询及循环依赖问题的解决

 延迟加载就是将缓存等存到ConcurrentLinkedQueue<BaseExecutor.DeferredLoad>

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 子查询填充后,会先将一级缓存中的占位符删除,再将结果list存到缓存中,此时list中的blog还是为null

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 此时子查询还未结束,不会从延迟加载的ConcurrentLinkedQueue中获取数据

Mybatis源码学习五嵌套查询及循环依赖问题的解决

 子查询结束,主查询也要先删除一级缓存中的占位符,然后再将结果设置到缓存中,此时可以看到一级缓存中的子查询和主查询都不是占位符了,不过blog中还是为null

Mybatis源码学习五嵌套查询及循环依赖问题的解决

queryStack已经为0,从延迟加载的ConcurrentLinkedQueue 迭代获取数据

Mybatis源码学习五嵌套查询及循环依赖问题的解决 此时list中的blog还是为空,接着从一级缓存中获取blog的值填充到list中的blog(具体原理是MetaObject反射原理,后期再写MetaObject底层知识)

Mybatis源码学习五嵌套查询及循环依赖问题的解决

此时list中blog已经迭代为 真实数据

Mybatis源码学习五嵌套查询及循环依赖问题的解决

总结:Mybatis使用了一级缓存,延迟装载,占位符和queryStack来解决了嵌套查询的循环依赖问题。这也解释了为什么在之前讲一级缓存时必须要在queryStack为0时才能被清除。同时一级缓存也是不能被关闭的原因。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容