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. }

最新文章

  1. typedef使用大全(转)
  2. ie不支持getElementsByClassName的解决办法
  3. BizTalk动手实验(六)Orchestration开发
  4. SharePoint Server 2010 &amp; WorkFlow related Limits
  5. HashMap源代码深入剖析
  6. 增加Android可用内存
  7. 小波变换 C++ opencv 实现
  8. react redux 相关技术
  9. codeforces932E Team Work
  10. POJ 1743 Musical Theme(后缀数组 + 二分)题解
  11. python练习(-)
  12. Delphi2007精简版加载Borland.Studio.Together.dll错误解决办法
  13. PHP iconv 解决utf-8和gb2312编码转换问题
  14. 2041 ACM 超级楼梯
  15. 线段树 || BZOJ 1112: [POI2008]砖块Klo
  16. zabbix_server.conf、zabbix_agentd.conf配置文件详解
  17. 【mysql】字段支持JSON类型
  18. 每日英语:Redfin Real-Estate Firm Gets Cold Shoulder in Silicon Valley
  19. 使用JPA保存对象时报nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly错误
  20. Python学习网站

热门文章

  1. POJ 1716
  2. Git 使用方法
  3. Play Framework 2.2.6 安装
  4. Linux shell 脚本小记
  5. unity3d泰斗破坏神2----课程列表
  6. JavaWeb项目开发案例精粹-第2章投票系统-002配置文件及公共类
  7. React开发项目例子
  8. App应用与思考
  9. MSSQLServer基础07(事务,存储过程,分页的存储过程,触发器)
  10. getpeername