mybatis使用association的resultMap方式进行映射少数据问题

本文介绍了一个关于MyBatis复杂映射时出现的数据缺失问题,通过具体示例展示了如何定位并解决该问题,包括正确配置主键及确保关联字段的唯一性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在第一次使用mybatis的复杂映射的时候发现映射出来的对象的数量和那sql直接去库里面查的数量要少。


public class Blog {
    private int id;
    private String title;
    private String content;
    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 String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

}

public class BlogDetail {
    private Blog blog;
    private int commentCount;
    public Blog getBlog() {
        return blog;
    }
    public void setBlog(Blog blog) {
        this.blog = blog;
    }
    public int getCommentCount() {
        return commentCount;
    }
    public void setCommentCount(int commentCount) {
        this.commentCount = commentCount;
    }

}

我使用的方式是如下方式:

<resultMap type="BlogDetail" id="blogDetailResultMap">  
        <result property="commentCount" column="comment_count" />   
        <association property="blog" column="blog_id" javaType="Blog" resultMap="blogResultMap" />   
    </resultMap>  

    <resultMap type="Blog" id="blogResultMap">  
            <id property="id" column="blog_id" />  
            <result property="title" column="title" />  
            <result property="content" column="content" />  
    </resultMap>  

    <select id="selectByID" parameterType="int" resultMap="detailBlogResultMap">  
        select  
        B.id as blog_id ,  
        B.title as blog_title ,  
        B.content as blog_content ,  
        (select count(*) from tb_comment C where C.blog_id=B.id) as  comment_count
        from tb_blog B
        where B.id = #{id}  
    </select>  

程序跑起来,没有任何的报错。但是mybatis映射出来的对象的数量比sql直接在数据库里面查出来的要少。
对比参考的资料,发现我的第一个resultMap 中少一个主键。
按照这个思路最终发现少数据原因是blogDetailResultMap中comment_count都是一样的。

mybatis的复杂映射参照:
http://blog.csdn.net/is_zhoufeng/article/details/17208183

原文如下:

<resultMap type="Blog" id="detailBlogResultMap03">  
        <!-- 注意这里的id -->
        <id property="id" column="blog_id" />
        <result property="title" column="blog_title" />  
        <result property="content" column="blog_content" />  
        <result property="pubTime" column="blog_pub_time" />  
        <association property="author" column="blog_user_id" javaType="User" resultMap="authorResultMap03" />  
        <collection property="comments" column="comment_blog_id" ofType="Comment"  resultMap="commentResultMap03" />  
    </resultMap>  

    <resultMap type="User" id="authorResultMap03">  
            <!-- 注意这里的id -->
            <id property="id" column="user_id" />  
            <result property="title" column="title" />  
            <result property="password" column="user_password" />  
            <result property="userName" column="user_user_name" />  
            <result property="userAge" column="user_user_age" />  
            <result property="userAddress" column="user_user_address" />  
    </resultMap>  

    <resultMap type="Comment" id="commentResultMap03">  
            <id property="id" column="comment_id" />  
            <result property="content" column="comment_content" />  
    </resultMap>  


    <select id="selectByID03" parameterType="int" resultMap="detailBlogResultMap">  
        select  
        B.id as blog_id ,  
        B.title as blog_title ,  
        B.content as blog_content ,  
        B.pub_time as blog_pub_time ,  
        B.user_id as blog_user_id ,  
        U.id as user_id,  
        U.password as user_password,  
        U.user_name as user_user_name ,  
        U.user_age as user_user_age ,  
        U.user_address as user_user_address ,  
        C.id as comment_id ,  
        C.blog_id as comment_blog_id ,  
        C.content as comment_content  
        from `blog` B   
        left outer join `user` U on B.user_id = U.id  
        left outer join `comment` C on B.id = C.blog_id  
        where B.id = #{id}  
    </select>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值