Xhtml代码 


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

测试代码:

Java代码 


1. /**
2.      * 测试返回HashMap
3.      */
4. @SuppressWarnings("unchecked")  
5. @Test
6. public void
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. "title"));  
12.     }


 

ibatis高级映射:

表结构:

 


Java代码 

1. create table BLOG  
2. (  
3. 20),  
4. 50),  
5. 4000),  
6. 20)  
7. )  
8. create table AUTHOR  
9. (  
10. 20),  
11. 50)  
12. );  
13. create table POSTS  
14. (  
15. 20),  
16. 50),  
17. 4000),  
18. 20)  
19. )

  

bean信息:

Java代码 


1. package
2. import
3. public class
4. private
5. private
6. private
7. private
8.       
9. private List<Post> posts/*=new ArrayList<Post>()*/;  
10.       
11. public
12. return
13.     }  
14. public void
15. this.posts = posts;  
16.     }  
17. public
18. return
19.     }  
20. public void
21. this.id = id;  
22.     }  
23. public
24. return
25.     }  
26. public void
27. this.title = title;  
28.     }  
29. public
30. return
31.     }  
32. public void
33. this.content = content;  
34.     }  
35. public
36. return
37.     }  
38. public void
39. this.author = author;  
40.     }  
41. @Override
42. public
43. return "Blog [content=" + content + ", id=" + id + ", title="
44. "]";  
45.     }  
46. }


Java代码 

1. package
2. public class
3. private
4. private
5. public
6. return
7.     }  
8. public void
9. this.id = id;  
10.     }  
11. public
12. return
13.     }  
14. public void
15. this.name = name;  
16.     }  
17. @Override
18. public
19. //      System.out.println("Author[id="+id+",name="+name+"]");
20. return "Author[id="+id+",name="+name+"]";  
21.     }  
22.       
23. }


Java代码 


1. package
2. public class
3. private
4. private
5. private
6. public
7. return
8.     }  
9. public void
10. this.id = id;  
11.     }  
12. public
13. return
14.     }  
15. public void
16. this.subject = subject;  
17.     }  
18. public
19. return
20.     }  
21. public void
22. this.postContent = postContent;  
23.     }  
24. @Override
25. public
26. return "Post [postContent=" + postContent + ", id=" + id + ", subject="
27. "]";  
28.     }  
29.       
30.       
31. }

Blog.xmll配置:

Xhtml代码 


1. <!-- 高级结果映射 -->
2.     <!--  
3.          一对一关联 嵌套查询 应用select属性  
4. id="selectBlogAndRelatedAuthor"
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. id
20. </select>
21. <select id="selectAuthor" parameterType="int" resultType="Author">
22. 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. t.BLOG_AUTHOR_ID = a.ID  
40. t.ID
41. </select>
42.       
43.     <!--  
44.          一对多关联  嵌套查询,应用select属性   
45. <collection property="posts" column="id" javaType="ArrayList" ofType="Post"
46. select="selectPosts"/>中的column指得是Post所对应表中的引用的主表中的主键id,  
47.         注意:这个column指的是主表(Blog)中的id,我在这犯了个错,写为Post所对应表中的外键id,这是不对的,  
48.                        应为所引用主表的主键id。  
49. property="posts" javaType="ArrayList" ofType="Post"
50.           
51.         同样没有进行延迟加载,不知为何。。。  
52. >
53. <resultMap type="Blog" id="blogResult3" >
54. <id property="id" column="id"/>
55. <result property="title" column="title"/>
56. <result property="content" column="content"/>
57. <collection property="posts" column="id" javaType="ArrayList" ofType="Post"
58. select="selectPosts"/>
59. </resultMap>
60. <select id="selectBlogAndRelatedPosts" parameterType="int" resultMap="blogResult3" >
61. id
62. </select>
63. <select id="selectPosts" parameterType="int" resultType="Post" >
64. p.blog_id
65. </select>
66.       
67.     <!--  
68.          一对多关联  嵌套结果,在使用这种方式时,sql语句应该使用别名以保证不重复,如果不这样,可能  
69.         出现结果不正确的现象,比如以下的post_id别名  
70. >
71. <resultMap type="Blog" id="blogResultVersion" >
72. <id property="id" column="id" />
73. <result property="title" column="title"/>
74. <result property="content" column="content"/>
75. <collection property="posts" javaType="ArrayList" ofType="Post" column="id">
76. <id property="id" column="post_id"/>
77. <result property="subject" column="subject"/>
78. <result property="postContent" column="postContent"/>
79. </collection>
80. </resultMap>
81. <select id="selectBlogAndRelatedPosts2" parameterType="int"  resultMap="blogResultVersion">
82.         SELECT t.id, t.title, t.content, p.id as "post_id", p.subject, p.BODY as "postContent"  
83.           FROM blog t  
84. t.id = p.blog_id  
85. t.ID
86. </select>

测试代码:

Java代码 

1. /**
2.      * 测试一对一关联 嵌套查询
3.      */
4. @Test
5. public void
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.      */
17. @Test
18. public void
19.         SqlSession session = sqlSessionFactory.openSession();  
20.         Blog blog = (Blog) session.selectOne(  
21. "cn.enjoylife.BlogMapper.selectBlogAndRelatedAuthor2", 15);  
22.         session.close();  
23.         System.out.println(blog.toString());  
24.         System.out.println(blog.getAuthor());  
25.     }  
26.       
27. /**
28.      * 测试一对多关联 嵌套查询
29.      */
30. @Test
31. public void testSelectBlogAndRelatedPosts() throws
32.         SqlSession session = sqlSessionFactory.openSession();  
33.         Blog blog = (Blog) session.selectOne(  
34. "cn.enjoylife.BlogMapper.selectBlogAndRelatedPosts", 15);  
35.         System.out.println(blog.toString());  
36.         session.close();  
37.         System.out.println(blog.getPosts());  
38.     }  
39.       
40. /**
41.      * 测试一对多关联 嵌套结果
42.      */
43. @Test
44. public void testSelectBlogAndRelatedPosts2() throws
45.         SqlSession session = sqlSessionFactory.openSession();  
46.         Blog blog = (Blog) session.selectOne(  
47. "cn.enjoylife.BlogMapper.selectBlogAndRelatedPosts2", 15);  
48.         session.close();  
49.         System.out.println(blog.toString());  
50.         System.out.println(blog.getPosts());  
51.     }