- <!-- 返回HashMap結果 類型-->
- <!-- 如果想返回JavaBean,只需將resultType設置為JavaBean的別名或全限定名 -->
- <!-- TypeAliasRegistry類初始化時注冊了一些常用的別名,如果忘記了別名可以在這里面查看 -->
- <select id="selectBlogRetHashMap" parameterType="int" resultType="map">
- SELECT id AS "id", title AS "title", content AS "content" FROM Blog WHERE id = #{id}
- </select>
- /**
- * 測試返回HashMap
- */
- @SuppressWarnings("unchecked")
- @Test
- public void testSelectBlogRetHashMap() {
- SqlSession session = sqlSessionFactory.openSession();
- HashMap<String,Object> blog = (HashMap<String,Object>) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogRetHashMap", 15);
- session.close();
- System.out.println(blog.get("title"));
- }
ibatis高級映射:
表結構:
- create table BLOG
- (
- ID NUMBER(20),
- TITLE VARCHAR2(50),
- CONTENT VARCHAR2(4000),
- BLOG_AUTHOR_ID NUMBER(20)
- )
- create table AUTHOR
- (
- ID NUMBER(20),
- AUTHOR_NAME VARCHAR2(50)
- );
- create table POSTS
- (
- ID NUMBER(20),
- SUBJECT VARCHAR2(50),
- BODY VARCHAR2(4000),
- BLOG_ID NUMBER(20)
- )
- package cn.enjoylife.domain;
- import java.util.List;
- public class Blog {
- private Integer id;
- private String title;
- private String content;
- private Author author;
- private List<Post> posts/*=new ArrayList<Post>()*/;
- public List<Post> getPosts() {
- return posts;
- }
- public void setPosts(List<Post> posts) {
- this.posts = posts;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public Author getAuthor() {
- return author;
- }
- public void setAuthor(Author author) {
- this.author = author;
- }
- @Override
- public String toString() {
- return "Blog [content=" + content + ", id=" + id + ", title=" + title
- + "]";
- }
- }
- package cn.enjoylife.domain;
- public class Author {
- private Integer id;
- private String name;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- // System.out.println("Author[id="+id+",name="+name+"]");
- return "Author[id="+id+",name="+name+"]";
- }
- }
- package cn.enjoylife.domain;
- public class Post {
- private Integer id;
- private String subject;
- private String postContent;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getSubject() {
- return subject;
- }
- public void setSubject(String subject) {
- this.subject = subject;
- }
- public String getPostContent() {
- return postContent;
- }
- public void setPostContent(String postContent) {
- this.postContent = postContent;
- }
- @Override
- public String toString() {
- return "Post [postContent=" + postContent + ", id=" + id + ", subject=" + subject
- + "]";
- }
- }
- <!-- 高級結果映射 -->
- <!--
- 一對一關聯 嵌套查詢 應用select屬性
- 1)id="selectBlogAndRelatedAuthor" 中的 blog_author_id 對應到
- <association property="author" column="blog_author_id"/>中的column="blog_author_id"
- 2)我們執行selectBlogAndRelatedAuthor 1次之后,所產生的每條記錄都要再進行一個查詢來獲取author信息(N),
- 這就是N+1問題,應該使用延遲加載的方式,否則這有可能產生致命的后果。
- 3)對於一對一關聯,我設置了
- <settings>
- <setting name="lazyLoadingEnabled" value="true"/>
- </settings>
- 卻沒有對Author進行延遲加載,不知為何。。。
- -->
- <resultMap id="blogResult" type="Blog" >
- <association property="author" column="blog_author_id"
- javaType="Author" select="selectAuthor" />
- </resultMap>
- <select id="selectBlogAndRelatedAuthor" parameterType="int" resultMap="blogResult" >
- SELECT id,title,content,blog_author_id FROM blog WHERE id = #{id}
- </select>
- <select id="selectAuthor" parameterType="int" resultType="Author">
- SELECT id,author_name as "name" FROM author WHERE id = #{id}
- </select>
- <!--
- 一對一關聯 嵌套結果
- -->
- <resultMap id="blogResult2" type="Blog">
- <id property="id" column="id" />
- <result property="title" column="title"/>
- <association property="author" column="blog_author_id"
- javaType="Author">
- <id property="id" column="author_id"/>
- <result property="name" column="author_name"/>
- </association>
- </resultMap>
- <select id="selectBlogAndRelatedAuthor2" parameterType="int" resultMap="blogResult2" >
- SELECT t.ID, t.TITLE, t.CONTENT, a.id as "author_id", a.author_name
- FROM blog t
- INNER JOIN author a ON t.BLOG_AUTHOR_ID = a.ID
- AND t.ID = #{id}
- </select>
- <!--
- 一對多關聯 嵌套查詢,應用select屬性
- <collection property="posts" column="id" javaType="ArrayList" ofType="Post"
- select="selectPosts"/>中的column指得是Post所對應表中的引用的主表中的主鍵id,
- 注意:這個column指的是主表(Blog)中的id,我在這犯了個錯,寫為Post所對應表中的外鍵id,這是不對的,
- 應為所引用主表的主鍵id。
- property="posts" javaType="ArrayList" ofType="Post" 指屬性posts為元素為Post的ArrayList類型
- 同樣沒有進行延遲加載,不知為何。。。
- -->
- <resultMap type="Blog" id="blogResult3" >
- <id property="id" column="id"/>
- <result property="title" column="title"/>
- <result property="content" column="content"/>
- <collection property="posts" column="id" javaType="ArrayList" ofType="Post"
- select="selectPosts"/>
- </resultMap>
- <select id="selectBlogAndRelatedPosts" parameterType="int" resultMap="blogResult3" >
- SELECT id, title, content FROM blog WHERE id = #{id}
- </select>
- <select id="selectPosts" parameterType="int" resultType="Post" >
- SELECT p.id,p.subject,p.body as "postContent" FROM posts p WHERE p.blog_id =#{id}
- </select>
- <!--
- 一對多關聯 嵌套結果,在使用這種方式時,sql語句應該使用別名以保證不重復,如果不這樣,可能
- 出現結果不正確的現象,比如以下的post_id別名
- -->
- <resultMap type="Blog" id="blogResultVersion" >
- <id property="id" column="id" />
- <result property="title" column="title"/>
- <result property="content" column="content"/>
- <collection property="posts" javaType="ArrayList" ofType="Post" column="id">
- <id property="id" column="post_id"/>
- <result property="subject" column="subject"/>
- <result property="postContent" column="postContent"/>
- </collection>
- </resultMap>
- <select id="selectBlogAndRelatedPosts2" parameterType="int" resultMap="blogResultVersion">
- SELECT t.id, t.title, t.content, p.id as "post_id", p.subject, p.BODY as "postContent"
- FROM blog t
- left outer JOIN posts p ON t.id = p.blog_id
- WHERE t.ID = #{id}
- </select>
- /**
- * 測試一對一關聯 嵌套查詢
- */
- @Test
- public void testSelectBlogAndRelatedAuthor() {
- SqlSession session = sqlSessionFactory.openSession();
- Blog blog = (Blog) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogAndRelatedAuthor", 15);
- System.out.println(blog.toString());
- session.close();
- System.out.println(blog.getAuthor());
- }
- /**
- * 測試一對一關聯 嵌套結果
- */
- @Test
- public void testSelectBlogAndRelatedAuthor2() {
- SqlSession session = sqlSessionFactory.openSession();
- Blog blog = (Blog) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogAndRelatedAuthor2", 15);
- session.close();
- System.out.println(blog.toString());
- System.out.println(blog.getAuthor());
- }
- /**
- * 測試一對多關聯 嵌套查詢
- */
- @Test
- public void testSelectBlogAndRelatedPosts() throws Exception {
- SqlSession session = sqlSessionFactory.openSession();
- Blog blog = (Blog) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogAndRelatedPosts", 15);
- System.out.println(blog.toString());
- session.close();
- System.out.println(blog.getPosts());
- }
- /**
- * 測試一對多關聯 嵌套結果
- */
- @Test
- public void testSelectBlogAndRelatedPosts2() throws Exception {
- SqlSession session = sqlSessionFactory.openSession();
- Blog blog = (Blog) session.selectOne(
- "cn.enjoylife.BlogMapper.selectBlogAndRelatedPosts2", 15);
- session.close();
- System.out.println(blog.toString());
- System.out.println(blog.getPosts());
- }