zoukankan      html  css  js  c++  java
  • mybatis返回HashMap结果类型与映射

    Xhtml代码 
    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>  

    测试代码:

    Java代码 
    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高级映射:

    表结构:

     

    Java代码 
    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信息:

    Java代码 
    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.       
    9.     private List<Post> posts/*=new ArrayList<Post>()*/;  
    10.       
    11.     public List<Post> getPosts() {  
    12.         return posts;  
    13.     }  
    14.     public void setPosts(List<Post> posts) {  
    15.         this.posts = posts;  
    16.     }  
    17.     public Integer getId() {  
    18.         return id;  
    19.     }  
    20.     public void setId(Integer id) {  
    21.         this.id = id;  
    22.     }  
    23.     public String getTitle() {  
    24.         return title;  
    25.     }  
    26.     public void setTitle(String title) {  
    27.         this.title = title;  
    28.     }  
    29.     public String getContent() {  
    30.         return content;  
    31.     }  
    32.     public void setContent(String content) {  
    33.         this.content = content;  
    34.     }  
    35.     public Author getAuthor() {  
    36.         return author;  
    37.     }  
    38.     public void setAuthor(Author author) {  
    39.         this.author = author;  
    40.     }  
    41.     @Override  
    42.     public String toString() {  
    43.         return "Blog [content=" + content + ", id=" + id + ", title=" + title  
    44.                 + "]";  
    45.     }  
    46. }  
    Java代码 
    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.       
    23. }  
    Java代码 
    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.       
    30.       
    31. }  

    Blog.xmll配置:

    Xhtml代码 
    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.     <!--  
    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" 指属性posts为元素为Post的ArrayList类型     
    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.         SELECT id, title, content FROM blog WHERE id = #{id}  
    62.     </select>  
    63.     <select id="selectPosts" parameterType="int" resultType="Post" >  
    64.         SELECT p.id,p.subject,p.body as "postContent" FROM posts p WHERE p.blog_id =#{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.          left outer JOIN posts p ON t.id = p.blog_id  
    85.          WHERE t.ID = #{id}  
    86.     </select>  

    测试代码:

    Java代码 
    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.      */  
    17.     @Test  
    18.     public void testSelectBlogAndRelatedAuthor2() {  
    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 Exception {  
    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 Exception {  
    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.     }  
  • 相关阅读:
    Luogu P4071 [SDOI2016]排列计数
    CF 961E Tufurama
    Luogu P2057 [SHOI2007]善意的投票
    Luogu P2756 飞行员配对方案问题
    POJ2151
    POJ 3349&&3274&&2151&&1840&&2002&&2503
    POJ 2388&&2299
    EZ 2018 03 30 NOIP2018 模拟赛(六)
    POJ 1459&&3436
    BZOJ 1001: [BeiJing2006]狼抓兔子
  • 原文地址:https://www.cnblogs.com/duanxz/p/3831588.html
Copyright © 2011-2022 走看看