三分钟读懂mybatis中resultMap和resultType区别

 更新时间:2023年07月31日 08:34:54   作者:吴皮皮今天吃饱了吗  
这篇文章主要给大家介绍了mybatis中resultMap和resultType区别的相关资料,resultType和resultMap都是mybatis进行数据库连接操作处理返回结果的,需要的朋友可以参考下

先说结论:

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>

总结 

到此这篇关于mybatis中resultMap和resultType区别的文章就介绍到这了,更多相关mybatis resultMap和resultType区别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论