1. <!-- 返回HashMap结果 类型-->
  2. <!-- 如果想返回JavaBean,只需将resultType设置为JavaBean的别名或全限定名 -->
  3. <!-- TypeAliasRegistry类初始化时注册了一些常用的别名,如果忘记了别名可以在这里面查看 -->
  4. <select id="selectBlogRetHashMap" parameterType="int" resultType="map">
  5. SELECT id AS "id", title AS "title", content AS "content" FROM Blog WHERE id = #{id}
  6. </select>

测试代码:

  1. /**
  2. * 测试返回HashMap
  3. */
  4. @SuppressWarnings("unchecked")
  5. @Test
  6. public void testSelectBlogRetHashMap() {
  7. SqlSession session = sqlSessionFactory.openSession();
  8. HashMap<String,Object> blog = (HashMap<String,Object>) session.selectOne(
  9. "cn.enjoylife.BlogMapper.selectBlogRetHashMap", 15);
  10. session.close();
  11. System.out.println(blog.get("title"));
  12. }

ibatis高级映射:

表结构:

  1. create table BLOG
  2. (
  3. ID             NUMBER(20),
  4. TITLE          VARCHAR2(50),
  5. CONTENT        VARCHAR2(4000),
  6. BLOG_AUTHOR_ID NUMBER(20)
  7. )
  8. create table AUTHOR
  9. (
  10. ID          NUMBER(20),
  11. AUTHOR_NAME VARCHAR2(50)
  12. );
  13. create table POSTS
  14. (
  15. ID      NUMBER(20),
  16. SUBJECT VARCHAR2(50),
  17. BODY    VARCHAR2(4000),
  18. BLOG_ID NUMBER(20)
  19. )

bean信息:

  1. package cn.enjoylife.domain;
  2. import java.util.List;
  3. public class Blog {
  4. private Integer id;
  5. private String title;
  6. private String content;
  7. private Author author;
  8. private List<Post> posts/*=new ArrayList<Post>()*/;
  9. public List<Post> getPosts() {
  10. return posts;
  11. }
  12. public void setPosts(List<Post> posts) {
  13. this.posts = posts;
  14. }
  15. public Integer getId() {
  16. return id;
  17. }
  18. public void setId(Integer id) {
  19. this.id = id;
  20. }
  21. public String getTitle() {
  22. return title;
  23. }
  24. public void setTitle(String title) {
  25. this.title = title;
  26. }
  27. public String getContent() {
  28. return content;
  29. }
  30. public void setContent(String content) {
  31. this.content = content;
  32. }
  33. public Author getAuthor() {
  34. return author;
  35. }
  36. public void setAuthor(Author author) {
  37. this.author = author;
  38. }
  39. @Override
  40. public String toString() {
  41. return "Blog [content=" + content + ", id=" + id + ", title=" + title
  42. + "]";
  43. }
  44. }
  1. package cn.enjoylife.domain;
  2. public class Author {
  3. private Integer id;
  4. private String name;
  5. public Integer getId() {
  6. return id;
  7. }
  8. public void setId(Integer id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. @Override
  18. public String toString() {
  19. //      System.out.println("Author[id="+id+",name="+name+"]");
  20. return "Author[id="+id+",name="+name+"]";
  21. }
  22. }
  1. package cn.enjoylife.domain;
  2. public class Post {
  3. private Integer id;
  4. private String subject;
  5. private String postContent;
  6. public Integer getId() {
  7. return id;
  8. }
  9. public void setId(Integer id) {
  10. this.id = id;
  11. }
  12. public String getSubject() {
  13. return subject;
  14. }
  15. public void setSubject(String subject) {
  16. this.subject = subject;
  17. }
  18. public String getPostContent() {
  19. return postContent;
  20. }
  21. public void setPostContent(String postContent) {
  22. this.postContent = postContent;
  23. }
  24. @Override
  25. public String toString() {
  26. return "Post [postContent=" + postContent + ", id=" + id + ", subject=" + subject
  27. + "]";
  28. }
  29. }

Blog.xmll配置:

  1. <!-- 高级结果映射 -->
  2. <!--
  3. 一对一关联 嵌套查询 应用select属性
  4. 1)id="selectBlogAndRelatedAuthor" 中的 blog_author_id 对应到
  5. <association property="author" column="blog_author_id"/>中的column="blog_author_id"
  6. 2)我们执行selectBlogAndRelatedAuthor 1次之后,所产生的每条记录都要再进行一个查询来获取author信息(N),
  7. 这就是N+1问题,应该使用延迟加载的方式,否则这有可能产生致命的后果。
  8. 3)对于一对一关联,我设置了
  9. <settings>
  10. <setting name="lazyLoadingEnabled" value="true"/>
  11. </settings>
  12. 却没有对Author进行延迟加载,不知为何。。。
  13. -->
  14. <resultMap id="blogResult" type="Blog" >
  15. <association property="author" column="blog_author_id"
  16. javaType="Author" select="selectAuthor"  />
  17. </resultMap>
  18. <select id="selectBlogAndRelatedAuthor" parameterType="int" resultMap="blogResult" >
  19. SELECT id,title,content,blog_author_id FROM blog WHERE id = #{id}
  20. </select>
  21. <select id="selectAuthor" parameterType="int" resultType="Author">
  22. SELECT id,author_name as "name" FROM author WHERE id = #{id}
  23. </select>
  24. <!--
  25. 一对一关联 嵌套结果
  26. -->
  27. <resultMap id="blogResult2" type="Blog">
  28. <id property="id" column="id" />
  29. <result property="title" column="title"/>
  30. <association property="author" column="blog_author_id"
  31. javaType="Author">
  32. <id property="id" column="author_id"/>
  33. <result property="name" column="author_name"/>
  34. </association>
  35. </resultMap>
  36. <select id="selectBlogAndRelatedAuthor2" parameterType="int" resultMap="blogResult2" >
  37. SELECT t.ID, t.TITLE, t.CONTENT, a.id as "author_id", a.author_name
  38. FROM blog t
  39. INNER JOIN author a ON t.BLOG_AUTHOR_ID = a.ID
  40. AND t.ID = #{id}
  41. </select>
  42. <!--
  43. 一对多关联  嵌套查询,应用select属性
  44. <collection property="posts" column="id" javaType="ArrayList" ofType="Post"
  45. select="selectPosts"/>中的column指得是Post所对应表中的引用的主表中的主键id,
  46. 注意:这个column指的是主表(Blog)中的id,我在这犯了个错,写为Post所对应表中的外键id,这是不对的,
  47. 应为所引用主表的主键id。
  48. property="posts" javaType="ArrayList" ofType="Post" 指属性posts为元素为Post的ArrayList类型
  49. 同样没有进行延迟加载,不知为何。。。
  50. -->
  51. <resultMap type="Blog" id="blogResult3" >
  52. <id property="id" column="id"/>
  53. <result property="title" column="title"/>
  54. <result property="content" column="content"/>
  55. <collection property="posts" column="id" javaType="ArrayList" ofType="Post"
  56. select="selectPosts"/>
  57. </resultMap>
  58. <select id="selectBlogAndRelatedPosts" parameterType="int" resultMap="blogResult3" >
  59. SELECT id, title, content FROM blog WHERE id = #{id}
  60. </select>
  61. <select id="selectPosts" parameterType="int" resultType="Post" >
  62. SELECT p.id,p.subject,p.body as "postContent" FROM posts p WHERE p.blog_id =#{id}
  63. </select>
  64. <!--
  65. 一对多关联  嵌套结果,在使用这种方式时,sql语句应该使用别名以保证不重复,如果不这样,可能
  66. 出现结果不正确的现象,比如以下的post_id别名
  67. -->
  68. <resultMap type="Blog" id="blogResultVersion" >
  69. <id property="id" column="id" />
  70. <result property="title" column="title"/>
  71. <result property="content" column="content"/>
  72. <collection property="posts" javaType="ArrayList" ofType="Post" column="id">
  73. <id property="id" column="post_id"/>
  74. <result property="subject" column="subject"/>
  75. <result property="postContent" column="postContent"/>
  76. </collection>
  77. </resultMap>
  78. <select id="selectBlogAndRelatedPosts2" parameterType="int"  resultMap="blogResultVersion">
  79. SELECT t.id, t.title, t.content, p.id as "post_id", p.subject, p.BODY as "postContent"
  80. FROM blog t
  81. left outer JOIN posts p ON t.id = p.blog_id
  82. WHERE t.ID = #{id}
  83. </select>

测试代码:

  1. /**
  2. * 测试一对一关联 嵌套查询
  3. */
  4. @Test
  5. public void testSelectBlogAndRelatedAuthor() {
  6. SqlSession session = sqlSessionFactory.openSession();
  7. Blog blog = (Blog) session.selectOne(
  8. "cn.enjoylife.BlogMapper.selectBlogAndRelatedAuthor", 15);
  9. System.out.println(blog.toString());
  10. session.close();
  11. System.out.println(blog.getAuthor());
  12. }
  13. /**
  14. * 测试一对一关联 嵌套结果
  15. */
  16. @Test
  17. public void testSelectBlogAndRelatedAuthor2() {
  18. SqlSession session = sqlSessionFactory.openSession();
  19. Blog blog = (Blog) session.selectOne(
  20. "cn.enjoylife.BlogMapper.selectBlogAndRelatedAuthor2", 15);
  21. session.close();
  22. System.out.println(blog.toString());
  23. System.out.println(blog.getAuthor());
  24. }
  25. /**
  26. * 测试一对多关联 嵌套查询
  27. */
  28. @Test
  29. public void testSelectBlogAndRelatedPosts() throws Exception {
  30. SqlSession session = sqlSessionFactory.openSession();
  31. Blog blog = (Blog) session.selectOne(
  32. "cn.enjoylife.BlogMapper.selectBlogAndRelatedPosts", 15);
  33. System.out.println(blog.toString());
  34. session.close();
  35. System.out.println(blog.getPosts());
  36. }
  37. /**
  38. * 测试一对多关联 嵌套结果
  39. */
  40. @Test
  41. public void testSelectBlogAndRelatedPosts2() throws Exception {
  42. SqlSession session = sqlSessionFactory.openSession();
  43. Blog blog = (Blog) session.selectOne(
  44. "cn.enjoylife.BlogMapper.selectBlogAndRelatedPosts2", 15);
  45. session.close();
  46. System.out.println(blog.toString());
  47. System.out.println(blog.getPosts());
  48. }

mybatis返回HashMap结果类型与映射的更多相关文章

  1. 云笔记项目-MyBatis返回自增类型&堆栈对象补充理解

    在云笔记项目中,讲到了MySql的自增,MyBatis查询到自增类型数据后可以设置返回到参数属性,其中学习了MySql的自增写法,堆栈对象等知识. MySql数据类型自增 建立一张Person表,其中 ...

  2. Mybatis返回HashMap时,某个字段值为null时,不会保存key

    转载: http://blog.csdn.net/little2z/article/details/38525327 mybatis 的 callSettersOnNulls 问题项目用到mybati ...

  3. mybatis返回map类型数据空值字段不显示的解决方法

    在日常开发中,查询数据返回类型为map,数据库中有些自动值为null,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢? Spring boot + MyBatis返回map中null值默认不 ...

  4. MyBatis 返回类型resultType为map时的null值不返回问题

    问题一:    查询结果集中 某字段 的值为null,在map中不包含该字段的key-value对 解决:在mybatis.xml中添加setting参数 <!-- 在null时也调用 sett ...

  5. 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap

    上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...

  6. 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap good

    上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...

  7. 笔记:MyBatis Mapper XML文件详解 - 映射和参数

    MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% ...

  8. Mybatis,返回Map的时候,将Map内的Key转换为驼峰的命名

    每次使用mybatis的时候,简单的连表查询,用Map接收的时候,都是像DB定义的字段一样,类似以下 student_name,student_id,没有转换为驼峰,但是又不能因为这一个定义一个jav ...

  9. 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap[转]

    上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...

随机推荐

  1. APT工作原理

    两篇好的文章:http://blog.csdn.net/newjueqi/article/details/6679857 http://blog.csdn.net/buguyiqie/article/ ...

  2. PhotoSwipe简介(PhotoSwipe是一个适合在触摸屏手机上使用的相册展示包)

    官方介绍PhotoSwipe 是专为移动触摸设备设计的相册/画廊.兼容所有iPhone.iPad.黑莓6+,以及桌面浏览器.底层实现基于HTML/CSS/JavaScript,是一款免费开源的相册产品 ...

  3. poj 3150 Cellular Automaton

    首先来看一下Sample里的第一组数据.1 2 2 1 2经过一次变换之后就成了5 5 5 5 4它的原理就是a0 a1 a2 a3 a4->(a4+a0+a1) (a0+a1+a2) (a1+ ...

  4. [YY题]HDOJ5288 OO’s Sequence

    题意:求这个式子 $\sum \limits_{i=1}^{n} \sum \limits_{j=1}^{m} f(i, j) mod (10^9 + 7)$ 的值 就是对每个区间[i, j]枚举区间 ...

  5. cogs 自己出的题目 题解报告

    第一题很简单嘛,就是裸的动态树分治嘛 对于每一层的重心维护子树路径的信息和子树到上一层重心的点的信息 空间复杂度O(nlogn) 对于每一层我们按dis排序,之后记录军队数量的前缀和 查询的时候我们只 ...

  6. 心情记录&考试总结 3.30

    并不知道现在要干什么,本人像是一只大颓狗 Em..怎么说呢,今天考完了一场奇怪的试 准确的说,画风很不正常的试 第一题集体爆零 第二题暴力20分 第三题暴力40分,乱搞有加成 改题的话, 第一题有奇怪 ...

  7. 李洪强漫谈iOS开发[C语言-037]-if else 语句

    李洪强漫谈iOS开发[C语言-037]-if else 语句

  8. HorseCome

    紫气东来,祝福也随之而来,喜鹊登梅,福禄也登上眉梢,马年将至,喜庆将萦绕身旁,在这个美好的日子送上我最真挚的祝福,祝身体安康. 春晓,春晓,处处绿杨芳草.山山水水,欢欢笑笑,共祝六合同春,步步登高!

  9. java io流缓冲理解

    bufferedinputstream和bufferedoutputstream:这两个类是在inputstream和outputstream的基础上增加了一个buffer的缓冲区,从而使数据不直接写 ...

  10. 2014-9-17二班----11 web project

    http://localhost:8080/rwkj1/indexServlet?name=zhagnsan&pwd=1234 跳  转  http://localhost:8080/rwkj ...