mybatis中resultMap返回类型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wobuaizhi/article/details/83104464

前面写了一篇关于resultType的文章,有兴趣的可以看看

这次写一篇resultMap的。就像官网上面说的“结果集的映射是 MyBatis 最强大的特性,对其有一个很好的理解的话,许多复杂映射的情形都能迎刃而解。”。通过使用resultMap或resultType可以解决复杂映射问题。基础简单的使用resultType就可以了,但是如果出现table中列和实体类中的属性不一致或者多表级联。那么这时resultMap就可以发挥作用了。

下面先给出一个demo例子,然后结合例子讲解。

  <resultMap id="UionResultMap" type="com.xx.xx.entity.Test">
	<id column="id" property="id" jdbcType="INTEGER" />
	<result column="name" property="name" jdbcType="VARCHAR" />
	<result column="dcid" property="dcid" jdbcType="INTEGER" />
	<result column="dc_code" property="dcCode" jdbcType="VARCHAR" />
	<result column="refresh_period" property="refreshPeriod" jdbcType="INTEGER" />
 </resultMap>
  <select id="findPropGroupList" resultMap="UionResultMap">
    select 
    	id,name,dcid,dc_code,refresh_period
    from Test
  </select>

这是一个基础的列子,可以看到select标签中使用的是resultMap,对应的是resultMap标签定义的一个映射集。

在这个映射集合中可以看到table中的字段和实体类中的属性是怎么一一对应起来的。jdbcType告诉框架如何解析这个属性,column是数据库中table的列,property是实体类中对应的属性,type是对应的实体类,id="UionResultMap"是这个resultMap的一个引用标志。

那么resultMap就这么简单吗?当然不会。

下面给出一个复杂的例子,来自官网

  <resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
  </constructor>
  <result property="title" column="blog_title"/>
  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
    <result property="favouriteSection" column="author_favourite_section"/>
  </association>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <association property="author" javaType="Author"/>
    <collection property="comments" ofType="Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType="Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="DraftPost"/>
    </discriminator>
  </collection>
</resultMap>

这里例子把resultMap这个标签的几个主要功能都显示了。下面一一介绍。

  • constructor - 用于在实例化类时,注入结果到构造方法中
    • idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能
    • arg - 将被注入到构造方法的一个普通结果
  • id – 一个 ID 结果;标记出作为 ID 的结果可以帮助提高整体性能
  • result – 注入到字段或 JavaBean 属性的普通结果
  • association – 一个复杂类型的关联;许多结果将包装成这种类型
    • 嵌套结果映射 – 关联可以指定为一个 resultMap 元素,或者引用一个
    • 这个说明一下,上面最后得到的是一个Blog实体类,这个association关联的Author。相当对是多个Bolg对应一个Author,也就是多对一的关系
  • collection – 一个复杂类型的集合
    • 嵌套结果映射 – 集合可以指定为一个 resultMap 元素,或者引用一个
    • 这个说明一下,这个collection标识Blog对应的多个post。也就是一对多的关系。例子中post集合里面还有comment和Tag集合。
  • discriminator – 使用结果值来决定使用哪个 resultMap
    • case – 基于某些值的结果映射
      • 嵌套结果映射 – 一个 case 也是一个映射它本身的结果,因此可以包含很多相 同的元素,或者它可以参照一个外部的 resultMap。这个用的比较少

所以最后的结果大概是下面这样 

Blog
	id      -- 基础属性
	title	-- 基础属性
	Author	-- 实体类
	Post	-- 集合
		id		-- 基础属性
		subject -- 基础属性
		Author 	-- 实体类
		Comment -- 集合
			id	-- 基础属性
		Tag		-- 集合
			id	-- 基础属性
			

结合之前说的resultType,mybatis的映射笔记结束。

大家共同进步

猜你喜欢

转载自blog.csdn.net/wobuaizhi/article/details/83104464