先说结论:
resultmap与resulttype的区别为:对象不同、描述不同、类型适用不同。 说人话就是,resultmap和resulttype功能差不多,但是resultmap功能更强大
resultType:
使用resultType进行输出映射时,只有查询出来的列名和pojo(简单实例对象)中的属性名一致,该列才可以映射成功。 武断一点来说:一般是以下这几种类型才用resultType 1、基本类型 :resultType=基本类型(int,String等基本数据类型) 2、List类型: resultType=List中元素的类型 3、Map类型 单条记录:resultType =map 多条记录:resultType =Map中value的类型
<select id="count" resultType="int">
select count(id) from t_paper as p
LEFT JOIN t_type as t
ON
p.type_id=t.id
</select>
resultMap:
前面说过,resultMap和resultType的功能类似,但是resultMap更强大一点,resultMap可以实现将查询结果映射为复杂类型的pojo,简单来说就是,resultType解决不了的,都可以交给resultMap来解决。 在使用resultMap之前我们需要先定义一个符合当前需求的resultMap.。
<resultMap id="paperResult" type="Paper">
<!-- column:数据库字段名 property:实体的属(变量)名 -->
<result column="id" property="id"/>
<result column="title" property="title"/>
<result column="type_id" property="typeId"/>
<result column="paper_summary" property="paperSummary"/>
<result column="paper_path" property="paperPath"/>
</resultMap>
<select id="selectPaperListByCondition" resultMap="paperResult">
SELECT
p.*, t.type_name from t_paper as p
LEFT JOIN
t_type as t
ON
p.type_id=t.id
WHERE
title= and type_name=
<where>
<if test="title != null and title != ">
and title like %${title}%
</if>
<if test="typeName != null and typeName != ">
and type_name=#{typeName}
</if>
</where>
limit #{start},#{size}
</select>