mybatis使用association的resultMap方式进行映射少数据问题
2016-04-14 21:19
561 查看
在第一次使用mybatis的复杂映射的时候发现映射出来的对象的数量和那sql直接去库里面查的数量要少。
我使用的方式是如下方式:
程序跑起来,没有任何的报错。但是mybatis映射出来的对象的数量比sql直接在数据库里面查出来的要少。
对比参考的资料,发现我的第一个resultMap 中少一个主键。
按照这个思路最终发现少数据原因是blogDetailResultMap中comment_count都是一样的。
mybatis的复杂映射参照:
/article/7843625.html
原文如下:
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的复杂映射参照:
/article/7843625.html
原文如下:
<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>
相关文章推荐
- LeetCode——008
- Jquery如何获得选中Radio单选按钮的值
- 一个超简单的马里奥游戏
- 日志记录
- 进程间通信(3)--信号量
- vi_ABC
- VS开发入门常识
- Linux学习笔记知识点(find命令)
- notifyDataSetChanged()
- 06 VoLTE - Signaling - Call Release and Error Handling
- BZOJ4521: [Cqoi2016]手机号码
- 关于Android SQLite3多线程并发问题
- OD三种断点原理
- 复利计算--结对1.0,做汉堡,结对2.0-复利计算再升级
- 复利计算5.1
- 【Code】JS-propertychange事件,实现实时金额计算
- WinEdt的bib参考文献管理教程
- 深入理解JVM-虚拟机字节码执行引擎
- Java内部类:局部内部类(三)
- poj 3259 Wormholes bellman