MyBatis的返回參數類型分兩種html
1. 對應的分類爲:java
1.1.resultMap:api
1.2.resultType:spa
2 .對應返回值類型:.net
2.1.resultMap:結果集code
2.2.resultType:int,string ,long ,classhtm
3. 注意點:對象
在MyBatis進行查詢映射時,其實查詢出來的每個屬性都是放在一個對應的Map裏面的,其中鍵是屬性名,值則是其對應的值。
3.1 當提供的返回類型屬性是resultType時,MyBatis會將Map裏面的鍵值對取出賦給resultType所指定的對象對應的屬性。因此其實MyBatis的每個查詢映射的返回類型都是ResultMap,只是當提供的返回類型屬性是resultType的時
候,MyBatis對自動的給把對應的值賦給resultType所指定對象的屬性。
3.2 當提供的返回類型是resultMap時,由於Map不能很好表示領域模型,就須要本身再進一步的把它轉化爲對應的對象,這經常在複雜查詢中頗有做用。blog
4.案例ip
4.1:resultMap案例
[html] view plain copy ![在CODE上查看代碼片](http://static.javashuo.com/static/loading.gif)
![派生到個人代碼片](http://static.javashuo.com/static/loading.gif)
- <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
- select
- <include refid="Base_Column_List" />
- from common_car_make
- where id = #{id,jdbcType=BIGINT}
- </select>
4.2 resultType--long案例
[html] view plain copy ![在CODE上查看代碼片](http://static.javashuo.com/static/loading.gif)
![派生到個人代碼片](http://static.javashuo.com/static/loading.gif)
- <select id="queryCarTypeByModelIdCount" resultType="java.lang.Long" parameterType="java.util.Map">
- select count(*) from common_car_type cm
- where 1=1
- <if test="carModelId != null">
- and cm.car_model_id = #{carModelId,jdbcType=DECIMAL}
- </if>
- </select>
4.3 resultType--int案例
[html] view plain copy ![在CODE上查看代碼片](http://static.javashuo.com/static/loading.gif)
![派生到個人代碼片](http://static.javashuo.com/static/loading.gif)
- <select id="queryCategoryBrandCount" resultType="java.lang.Integer" parameterType="java.util.HashMap" >
- select count(1)
- from common_category_brand
- where 1=1
- <if test="categoryId != null" >
- and category_id = #{categoryId,jdbcType=BIGINT}
- </if>
- <if test="brandId != null" >
- and brand_id = #{brandId,jdbcType=BIGINT}
- </if>
- </select>
4.4 resultType--class案例:查詢結果對應類中的屬性值
[html] view plain copy ![在CODE上查看代碼片](http://static.javashuo.com/static/loading.gif)
![派生到個人代碼片](http://static.javashuo.com/static/loading.gif)
- <select id="selectCommonBrand" resultType="com.epeit.api.model.CommonBrandPo" parameterType="java.lang.Long" >
- select
- id, brand_name brandName, brand_type brandType, icon, delete_flag deleteFlag
- from common_brand
- where id = #{id,jdbcType=BIGINT}
- </select>