为什么全局懒加载没有生效呢?

2020-12-19 12:18:06 +08:00
 IvanBlade

article

public class Article {

    private Integer id;

    private String title;

    private String content;

    // 表示当前文章代表的评论列表
    private List<Comment> commentList;


    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", commentList=" + commentList +
                '}';
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<Comment> getCommentList() {
        return commentList;
    }

    public void setCommentList(List<Comment> commentList) {
        this.commentList = commentList;
    }

    public Integer getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

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

}

mapper

<?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="com.Ivan.mapper.ArticleMapper">


    <!--一对多关联查询:查询所有的用户,同时还要查询出每个用户所关联的订单信息-->

    <resultMap id="articleMap" type="com.Ivan.domain.Article">
        <id property="id" column="id"></id>
        <result property="title" column="title"></result>
        <result property="content" column="content"></result>


        <!--
            collection : 一对多使用 collection 标签进行关联
        -->
        <collection property="commentList" ofType="com.Ivan.domain.Comment" fetchType="lazy">
            <id property="id" column="cid"></id>
            <result property="content" column="content"/>
            <result property="author" column="author"/>
            <result property="a_id" column="a_id" />
        </collection>
    </resultMap>


    <select id="displayAllArticle"  resultMap="articleMap">
        SELECT a.*,c.id cid,c.content, c.author,c.a_id FROM t_comment c RIGHT JOIN t_article a ON c.a_id = a.id
    </select>

</mapper>

部分 sqlmapConfig

<settings>
        <!--开启全局延迟加载功能--> 
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--所有方法都会延迟加载-->
        <setting name="lazyLoadTriggerMethods" value="toString()"/>

        <!--
       因为 cacheEnabled 的取值默认就为 true,所以这一步可以省略不配置。
       为 true 代表开启二级缓存;为 false 代表不开启二级缓存。
   -->
        <setting name="cacheEnabled" value="false"/>

    </settings>

结果中的 commentList 还是照常显示。。。是哪里出了问题?

1296 次点击
所在节点    Java
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/737012

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX