MyBatis之ResultMap简介,关联对象

来源:互联网 发布:最帅程序员 编辑:程序博客网 时间:2024/05/16 06:22

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。


1.resultType

 

在MyBatis进行查询映射的时候,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。当提供的返回类型属性是resultType的时候,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap的时候,因为Map不能很好表示领域模型,我们就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。

 

这里要强调的是,Mybatis是对返回的结果的每一行做映射的。所以,下面的语句返回的是Integer,而不是List


<select id="count" parameterType="AreaDto" resultType="java.lang.Integer">          SELECT id FROM USER  </select> 

返回一个int

<select id="count" parameterType="AreaDto" resultType="java.lang.Integer">          SELECT count(*) FROM USER  </select> 

返回map

<select id=”selectUsers” parameterType=”int” resultType=”hashmap”>      select id, username, hashedPassword      from some_table      where id = #{id}  </select> 

这样一个语句简单作用于所有列被自动映射到HashMap的键上,这由resultType属性指定。这在很多情况下是有用的,但是HashMap不能很好描述一个领域模型。那样你的应用程序将会使用JavaBeans或POJOs(Plain Old Java Objects,普通Java对象)来作为领域模型

返回javaBEAN 对象

这些情况下,MyBatis会在幕后自动创建一个ResultMap,基于属性名来映射列到JavaBean的属性上


2.resultMap

 

MyBatis会自动创建一个ResultMap对象,然后基于查找出来的属性名进行键值对封装,然后再看到返回类型是Blog对象,再从ResultMap中取出与Blog对象对应的键值对进行赋值。

当返回类型直接是一个ResultMap的时候也是非常有用的,这主要用在进行复杂联合查询上,因为进行简单查询是没有什么必要的。

 

简单resultMap配置

<resultMap type="com.liulanghan.Blog" id="BlogResult">        <id column="id" property="id"/>        <result column="title" property="title"/>        <result column="content" property="content"/>        <result column="owner" property="owner"/>    </resultMap>      <select id="selectBlog" parameterType="int" resultMap="BlogResult">          select * from t_blog where id = #{id}    </select>

结果集的列比resultMap多会报错么?
不会,只映射resultMap中有的列。

 

结果集的列比resultMap少会报错么?
不会,只映射结果集中有的列。

 

详细说明,请参考:Mybatis中resultMap


附:一个一直让自己迷糊的地方


<resultMap id="promotionActivity" type="PromotionActivity"><id property="id" column="id"/><result property="name" column="name"/><result property="balanceName" column="balance_name"/> ...//省略<result property="newCustomer" column="new_customer"/><association property="orderPlatform" javaType="OrderPlatform"><id property="id" column="platform_id"/><result property="name" column="orderplatform_name"/></association><association property="promotionTemplate" javaType="PromotionTemplate"><id property="id" column="templat_id"/><result property="name" column="template_name"/><result property="types" column="template_types"/><result property="relation" column="template_relation"/><result property="minCups" column="template_mincups"/><result property="cups" column="template_cups"/><result property="limitedProduct" column="template_limitedproduct"/></association>   </resultMap> <select id="get" parameterType="long" resultMap="promotionActivity">                  SELECT PA.id,PA.balance_name,PA.name,PA.total_times,PA.start_time,PA.end_time,PA.template_id,             PA.region_limit_type,PA.region_limit,PA.new_customer,PA.uniqueness,PT.name as template_name,                      PT.types as template_types,                      PT.relation as template_relation,PT.min_cups as template_mincups,             PT.cups as template_cups,PT.limited_product as template_limitedproduct,                 OT.name as orderplatform_name,PA.day_total_times,PA.customer_total_times,PA.customer_day_total_times,             PA.repeat_times,PT.id as templat_id,OT.id as platform_id             from promotion_activity PA             left join promotion_template PT on PT.id=PA.template_id                 left join order_platform OT on OT.id=PA.platform_id            where PA.active='Y'            AND PA.id = #{id}            </select><select id="queryPage" parameterType="Map" resultMap="promotionActivity">                  SELECT PA.id,PA.balance_name,PA.name,PA.total_times,PA.start_time,PA.end_time,PA.template_id,             PA.region_limit_type,PA.region_limit from promotion_activity PA                      where active='Y'         <if test="cityId != null and cityId != ''" >            and ((region_limit like CONCAT('%',',${cityId},','%' ) or             region_limit like CONCAT('%',',${cityId}') or            region_limit like CONCAT('${cityId},','%' ) or            region_limit like '${cityId}')             and region_limit_type='c' )              or (region_limit_type = 'z' and                                       <foreach collection="zoneList" index="index" item="region" open="("                separator="or" close=")">                  PA.region_limit like  CONCAT('%',',${region},','%' ) or                 PA.region_limit like  CONCAT('%',',${region}' ) or                 PA.region_limit like  CONCAT('${region},','%' ) or                 PA.region_limit like '${region}'                        </foreach> )         </if>            </select>      



1 0
原创粉丝点击