三分鐘讀懂mybatis中resultMap和resultType區(qū)別
先說結(jié)論:
resultmap與resulttype的區(qū)別為:對(duì)象不同、描述不同、類型適用不同。
說人話就是,resultmap和resulttype功能差不多,但是resultmap功能更強(qiáng)大
resultType:
使用resultType進(jìn)行輸出映射時(shí),只有查詢出來的列名和pojo(簡(jiǎn)單實(shí)例對(duì)象)中的屬性名一致,該列才可以映射成功。武斷一點(diǎn)來說:一般是以下這幾種類型才用resultType
1、基本類型 :resultType=基本類型(int,String等基本數(shù)據(jù)類型)
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更強(qiáng)大一點(diǎn),resultMap可以實(shí)現(xiàn)將查詢結(jié)果映射為復(fù)雜類型的pojo,簡(jiǎn)單來說就是,resultType解決不了的,都可以交給resultMap來解決。 在使用resultMap之前我們需要先定義一個(gè)符合當(dāng)前需求的resultMap.。
<resultMap id="paperResult" type="Paper">
<!-- column:數(shù)據(jù)庫字段名 property:實(shí)體的屬(變量)名 -->
<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>總結(jié)
到此這篇關(guān)于mybatis中resultMap和resultType區(qū)別的文章就介紹到這了,更多相關(guān)mybatis resultMap和resultType區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaWeb基礎(chǔ)教程之Java基礎(chǔ)加強(qiáng)版
這篇文章主要介紹了JavaWeb基礎(chǔ)教程之Java基礎(chǔ)加強(qiáng)版的相關(guān)資料,需要的朋友可以參考下2016-07-07
Java list.remove( )方法注意事項(xiàng)
這篇文章主要介紹了Java list.remove( )方法注意事項(xiàng),非常簡(jiǎn)單易懂,需要的朋友可以參考下2018-08-08
解決springboot responseentity<string>亂碼問題
這篇文章主要介紹了解決springboot responseentity<string>亂碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Spring?Boot?+?EasyExcel?+?SqlServer?進(jìn)行批量處理數(shù)據(jù)的高效方法
在日常開發(fā)和工作中,我們可能要根據(jù)用戶上傳的文件做一系列的處理,本篇文章就以Excel表格文件為例,主要介紹了Spring?Boot?+?EasyExcel?+?SqlServer?進(jìn)行批量處理數(shù)據(jù)的高效方法,需要的朋友可以參考下2024-06-06

