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. grunt安装失败处理

    1.官网 Grunt官网 http://gruntjs.com 2.前言 前段时间一不小心升级了win10(万恶的360),各种不适应各种问题各种软件bug,最终决定回退到win7,然后悲催的发现系统 ...

  2. 学习笔记--Grunt、安装、图文详解

    学习笔记--Git安装.图文详解 安装Git成功后,现在安装Gruntjs,官网:http://gruntjs.com/ 一.安装node 参考node.js 安装.图文详解 (最新的node会自动安 ...

  3. 文件夹和文件、Path类、流、序列化

    循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...

  4. 嘿嘿,JAVA里第一次运行单元测试成功,立存

    按书上写的单元测试. 居然一次过,爽!!! package org.smart4j.chapter2.test; import java.util.HashMap; import java.util. ...

  5. httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数

    一.利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单 package com.test.httpclient; imp ...

  6. *[hackerrank]Girlfriend & Necklace

    https://www.hackerrank.com/contests/w8/challenges/gneck 有点意思.是DP,最优解包含最优子问题.F(X)=F(X-1)+F(X-3).因为F(X ...

  7. JavaWeb项目开发案例精粹-第3章在线考试系统-003Dao层

    1. package com.sanqing.dao; import java.util.List; import com.sanqing.po.Student; public interface S ...

  8. App应用与思考

    我为什么没有加入苹果的iOS APP移动大军?http://blog.csdn.net/Code_GodFather/article/details/7956858 ----------------- ...

  9. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  10. Struts2的简单案例

    第一步:首先下载struts2的架包(链接地址:http://download.csdn.net/detail/ahy1024/4626028) 第二步:新建web project项目 DemoStr ...