MyBatis学习(六)- ResultMap

官方文档地址:http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html      

  resultMap 元素是 MyBatis 中最重要最强大的元素。它就是让你远离 90%的需要从结果 集中取出数据的 JDBC 代码的那个东西, 而且在一些情形下允许你做一些 JDBC 不支持的事 情。 事实上, 编写相似于对复杂语句联合映射这些等同的代码, 也许可以跨过上千行的代码。 ResultMap 的设计就是简单语句不需要明确的结果映射,而很多复杂语句确实需要描述它们 的关系。

resultMap
  • constructor - 类在实例化时,用来注入结果到构造方法中
    • idArg - ID 参数;标记结果作为 ID 可以帮助提高整体效能
    • arg - 注入到构造方法的一个普通结果
  • id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能
  • result – 注入到字段或 JavaBean 属性的普通结果
  • association – 一个复杂的类型关联;许多结果将包成这种类型
    • 嵌入结果映射 – 结果映射自身的关联,或者参考一个
  • collection – 复杂类型的集
    • 嵌入结果映射 – 结果映射自身的集,或者参考一个
  • discriminator – 使用结果值来决定使用哪个结果映射
    • case – 基于某些值的结果映射
      • 嵌入结果映射 – 这种情形结果也映射它本身,因此可以包含很多相 同的元素,或者它可以参照一个外部的结果映射。

association:

表示“有一个”的关系

我们新添加一个类

[java]  view plain copy
  1. package org.ygy.model;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.ygy.util.Configs;  
  6. import org.ygy.util.DateUtil;  
  7.   
  8. /** 
  9.  * 博客类 
  10.  * @author yuguiyang 
  11.  * 
  12.  */  
  13. public class Blog {  
  14.     private int id;//ID,自动增长  
  15.     private String title;//标题  
  16.     private String content;//内容  
  17.     private Date reportTime;//发布时间  
  18.     private User author;//作者  
  19.   
  20.     public int getId() {  
  21.         return id;  
  22.     }  
  23.   
  24.     public void setId(int id) {  
  25.         this.id = id;  
  26.     }  
  27.   
  28.     public String getTitle() {  
  29.         return title;  
  30.     }  
  31.   
  32.     public void setTitle(String title) {  
  33.         this.title = title;  
  34.     }  
  35.   
  36.     public String getContent() {  
  37.         return content;  
  38.     }  
  39.   
  40.     public void setContent(String content) {  
  41.         this.content = content;  
  42.     }  
  43.       
  44.     public Date getReportTime() {  
  45.         return reportTime;  
  46.     }  
  47.   
  48.     public void setReportTime(Date reportTime) {  
  49.         this.reportTime = reportTime;  
  50.     }  
  51.   
  52.     public User getAuthor() {  
  53.         return author;  
  54.     }  
  55.   
  56.     public void setAuthor(User author) {  
  57.         this.author = author;  
  58.     }  
  59.   
  60.     @Override  
  61.     public String toString() {  
  62.         return "Blog [id=" + id + ", title=" + title + ", content=" + content  
  63.                 + ", report_time=" + DateUtil.fromDate(reportTime, Configs.CURRENTTIME) + ", author=" + author + "]";  
  64.     }  
  65.       
  66. }  

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3.   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4.   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5.     
  6. <mapper namespace="org.ygy.mapper.BlogMapper">  
  7.       
  8.     <resultMap type="Blog" id="BlogMap">  
  9.         <id column="blog_id" property="id"/>  
  10.         <result column="title" property="title"/>  
  11.         <result column="b_content" property="content"/>  
  12.         <result column="reportTime" property="reportTime"></result>  
  13.           
  14.         <!-- 每一个Blog都只有一个author ,  -->  
  15.         <association column="author_id" property="author" javaType="User">  
  16.             <id column="user_id" property="id"/>  
  17.             <result column="name" property="name"></result>   
  18.         </association>  
  19.     </resultMap>  
  20.   
  21.     <select id="queryAll" resultMap="BlogMap">  
  22.         select b.id as blog_id, b.title , b.b_content,b.reportTime , u.id as user_id , u.name from t_blog b  
  23.         left outer join t_user u on b.author_id = u.id   
  24.     </select>  
  25.       
  26.     <sql id="baseColumn">  
  27.         title , b_content , author_id , reportTime  
  28.     </sql>  
  29.       
  30.     <!-- 插入一条记录 -->  
  31.     <insert id="insert" parameterType="Blog">  
  32.         insert into t_blog  
  33.         (<include refid="baseColumn"/>)  
  34.         values(#{title} , #{content} , #{author.id} , #{reportTime})  
  35.     </insert>  
  36.       
  37.     <!-- 更新记录 -->  
  38.     <update id="update" parameterType="Blog">  
  39.         update t_blog  
  40.         <set>  
  41.             <if test="title != null">title = #{title}</if>  
  42.             <if test="content != null">b_content = #{content}</if>  
  43.             <if test="report_time != null">reportTime = #{reportTime}</if>  
  44.         </set>  
  45.         where id=#{id}  
  46.     </update>  
  47. </mapper>  

关联元素处理“有一个”类型的关系。比如,在我们的示例中,一个博客有一个用户。关联映射就工作于这种结果之上。你指定了目标属性,来获取值的列,属性的 java 类型(很多情况下 MyBatis 可以自己算出来),如果需要的话还有 jdbc 类型,如果你想覆盖或获取的结果值还需要类型控制器。

关联中不同的是你需要告诉 MyBatis 如何加载关联。MyBatis 在这方面会有两种不同的方式:

  • 嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型。
  • 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集。首先,然让我们来查看这个元素的属性。所有的你都会看到,它和普通的只由 select 和

resultMap 属性的结果映射不同。

上面使用的应该算是嵌套结果,下面写一个嵌套查询的例子:

[html]  view plain copy
  1. <resultMap type="Blog" id="blogMap_2">  
  2.         <id column="id" property="id"/>  
  3.         <result column="title" property="title"/>  
  4.         <result column="b_content" property="content"/>  
  5.         <result column="reportTime" property="reportTime"></result>  
  6.           
  7.         <association column="author_id" property="author" javaType="User" select="selectUser" />  
  8.     </resultMap>  
  9.       
  10.     <select id="selectBlog" resultMap="blogMap_2">  
  11.         select * from t_blog   
  12.     </select>  
  13.       
  14.     <select id="selectUser" parameterType="int" resultType="User">  
  15.         select id , name from t_user where id=#{id}  
  16.     </select>  

collection

[html]  view plain copy
  1. <resultMap type="User" id="userMap">  
  2.         <id column="id" property="id"/>  
  3.         <result column="name" property="name"/>  
  4.           
  5.         <collection property="blogList" ofType="Blog">  
  6.             <id column="blog_id" property="id"/>  
  7.             <result column="title" property="title"/>  
  8.             <result column="b_content" property="content"/>  
  9.             <result column="reportTime" property="reportTime"/>  
  10.         </collection>  
  11.     </resultMap>  
  12.       
  13.     <select id="selectUserBlog" resultMap="userMap">  
  14.         select u.id , u.name , b.id as blog_id, b.title , b.b_content ,b.reportTime from t_user u  
  15.         left outer join t_blog b on u.id=b.author_id   
  16.     </select>  
在User类中添加的List<Blog> blogList;
[java]  view plain copy
  1. package org.ygy.model;  
  2.   
  3. import java.util.List;  
  4.   
  5. /** 
  6.  * 用户 
  7.  * @author yuguiyang 
  8.  * 
  9.  */  
  10. public class User {  
  11.     private Integer id;//自动增长的ID  
  12.     private String name;//用户名  
  13.     private String password;//密码  
  14.     private String email;//邮箱  
  15.     private Integer age;//年龄  
  16.     private Integer gender;//性别,0-男 ; 1-女  
  17.     private List<Blog> blogList;  
  18.   
  19.     public Integer getId() {  
  20.         return id;  
  21.     }  
  22.   
  23.     public void setId(Integer id) {  
  24.         this.id = id;  
  25.     }  
  26.   
  27.     public String getName() {  
  28.         return name;  
  29.     }  
  30.   
  31.     public void setName(String name) {  
  32.         this.name = name;  
  33.     }  
  34.   
  35.     public String getPassword() {  
  36.         return password;  
  37.     }  
  38.   
  39.     public void setPassword(String password) {  
  40.         this.password = password;  
  41.     }  
  42.   
  43.     public String getEmail() {  
  44.         return email;  
  45.     }  
  46.   
  47.     public void setEmail(String email) {  
  48.         this.email = email;  
  49.     }  
  50.   
  51.     public Integer getAge() {  
  52.         return age;  
  53.     }  
  54.   
  55.     public void setAge(Integer age) {  
  56.         this.age = age;  
  57.     }  
  58.   
  59.     public Integer getGender() {  
  60.         return gender;  
  61.     }  
  62.   
  63.     public void setGender(Integer gender) {  
  64.         this.gender = gender;  
  65.     }  
  66.       
  67.     public List<Blog> getBlogList() {  
  68.         return blogList;  
  69.     }  
  70.   
  71.     public void setBlogList(List<Blog> blogList) {  
  72.         this.blogList = blogList;  
  73.     }  
  74.   
  75.     @Override  
  76.     public String toString() {  
  77.         return "User [id=" + id + ", name=" + name + ", password=" + password  
  78.                 + ", email=" + email + ", age=" + age + ", gender=" + gender  
  79.                 + ", blogList=" + blogList + "]";  
  80.     }  
  81.   
  82. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值