mybatis使用association的resultMap方式進行對映少資料問題

木坦坦發表於2016-04-14

在第一次使用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>  

相關文章