发表于: 2017-03-15 23:36:42
2 1257
今天完成的事情:
1.今天阅读了mybatis 3.0 中文文档,主要是针对任务四 t11 页面需要进行的多表查询的相关知识进行了解。
我忍不住要贴一些代码实在是太精彩了
文章出处:http://www.cnblogs.com/rollenholt/p/3365866.html (其实这种文章是直接从mybatis 的中文文档复制过来的)
高级结果映射
MyBatis 创建的一个想法:数据库不用永远是你想要的或需要它们是什么样的。而我们 最喜欢的数据库最好是第三范式或 BCNF 模式,但它们有时不是。如果可能有一个单独的 数据库映射,所有应用程序都可以使用它,这是非常好的,但有时也不是。结果映射就是 MyBatis 提供处理这个问题的答案。
比如,我们如何映射下面这个语句?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!-- Very Complex Statement --> <select id= "selectBlogDetails" parameterType= "int" resultMap= "detailedBlogResultMap" > select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio, A.favourite_section as author_favourite_section, P.id as post_id, P.blog_id as post_blog_id, P.author_id as post_author_id, P.created_on as post_created_on, P.section as post_section, P.subject as post_subject, P.draft as draft, P.body as post_body, C.id as comment_id, C.post_id as comment_post_id, C.name as comment_name, C.comment as comment_text, T.id as tag_id, T.name as tag_name from Blog B left outer join Author A on B.author_id = A.id left outer join Post P on B.id = P.blog_id left outer join Comment C on P.id = C.post_id left outer join Post_Tag PT on PT.post_id = P.id left outer join Tag T on PT.tag_id = T.id where B.id = #{id} </select> |
你可能想把它映射到一个智能的对象模型,包含一个作者写的博客,有很多的博文,每 篇博文有零条或多条的评论和标签。 下面是一个完整的复杂结果映射例子 (假设作者, 博客, 博文, 评论和标签都是类型的别名) 我们来看看 。 但是不用紧张, 我们会一步一步来说明。 当天最初它看起来令人生畏,但实际上非常简单。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <!-- Very Complex Result Map --> <resultMap id= "detailedBlogResultMap" type= "Blog" > <constructor> <idArg column= "blog_id" javaType= "int" /> </constructor> <result property= "title" column= "blog_title" /> <association property= "author" javaType= "Author" > <id property= "id" column= "author_id" /> <result property= "username" column= "author_username" /> <result property= "password" column= "author_password" /> <result property= "email" column= "author_email" /> <result property= "bio" column= "author_bio" /> <result property= "favouriteSection" column= "author_favourite_section" /> </association> <collection property= "posts" ofType= "Post" > <id property= "id" column= "post_id" /> <result property= "subject" column= "post_subject" /> <association property= "author" javaType= "Author" /> <collection property= "comments" ofType= "Comment" > <id property= "id" column= "comment_id" /> </collection> <collection property= "tags" ofType= "Tag" > <id property= "id" column= "tag_id" /> </collection> <discriminator javaType= "int" column= "draft" > < case value= "1" resultType= "DraftPost" /> </discriminator> </collection> </resultMap> |
最佳实践 通常逐步建立结果映射。单元测试的真正帮助在这里。如果你尝试创建 一次创建一个向上面示例那样的巨大的结果映射, 那么可能会有错误而且很难去控制它 来工作。开始简单一些,一步一步的发展。而且要进行单元测试!使用该框架的缺点是 它们有时是黑盒(是否可见源代码) 。你确定你实现想要的行为的最好选择是编写单元 测试。它也可以你帮助得到提交时的错误。
关联的嵌套查询
这种方式很简单, 但是对于大型数据集合和列表将不会表现很好。 问题就是我们熟知的 “N+1 查询问题”。概括地讲,N+1 查询问题可以是这样引起的:
- 你执行了一个单独的 SQL 语句来获取结果列表(就是“+1”)。
- 对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。
这个问题会导致成百上千的 SQL 语句被执行。这通常不是期望的。
MyBatis 能延迟加载这样的查询就是一个好处,因此你可以分散这些语句同时运行的消 耗。然而,如果你加载一个列表,之后迅速迭代来访问嵌套的数据,你会调用所有的延迟加 载,这样的行为可能是很糟糕的。
关联的嵌套结果
1 2 3 4 5 6 7 8 9 10 11 | <resultMap id= "blogResult" type= "Blog" > <id property= "id" column= "blog_id" /> <result property= "title" column= "blog_title" /> <association property= "author" javaType= "Author" > <id property= "id" column= "author_id" /> <result property= "username" column= "author_username" /> <result property= "password" column= "author_password" /> <result property= "email" column= "author_email" /> <result property= "bio" column= "author_bio" /> </association> </resultMap> |
2.还可以参考这篇文章写得也很明白:http://www.cnblogs.com/dongying/p/4073259.html
明天计划的事情:
1.任务四 t11 进行动态页面的展示
遇到的问题:
1.我今天的问题就是没搞懂association和collection里面的column和property的具体含义,终于在查了一些文章终于明白。
比如一个博客对应一个作者,则两者是一对一的关系,column是在数据库里对应作者的字段,property是博客的javabean类对应的作者的属性。
只有在他们的bean里面有关系才能用resultMap查询出结果来。
收获:
mybatis的进一步认识
评论