MyBatis返回類型resultType和resultMap

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上查看代碼片派生到個人代碼片

  1. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >  
  2.     select   
  3.     <include refid="Base_Column_List" />  
  4.     from common_car_make  
  5.     where id = #{id,jdbcType=BIGINT}  
  6.   </select>  

4.2 resultType--long案例

[html] view plain copy 在CODE上查看代碼片派生到個人代碼片

  1. <select id="queryCarTypeByModelIdCount" resultType="java.lang.Long" parameterType="java.util.Map">  
  2.         select count(*)  from common_car_type cm  
  3.         where 1=1  
  4.         <if test="carModelId != null">  
  5.             and  cm.car_model_id = #{carModelId,jdbcType=DECIMAL}  
  6.         </if>  
  7.     </select>  

4.3 resultType--int案例

[html] view plain copy 在CODE上查看代碼片派生到個人代碼片

  1. <select id="queryCategoryBrandCount" resultType="java.lang.Integer" parameterType="java.util.HashMap" >  
  2.         select count(1)  
  3.         from common_category_brand  
  4.         where 1=1  
  5.         <if test="categoryId != null" >  
  6.             and category_id = #{categoryId,jdbcType=BIGINT}  
  7.         </if>  
  8.         <if test="brandId != null" >  
  9.             and brand_id = #{brandId,jdbcType=BIGINT}  
  10.         </if>  
  11.     </select>  


4.4 resultType--class案例:查詢結果對應類中的屬性值

[html] view plain copy 在CODE上查看代碼片派生到個人代碼片

  1. <select id="selectCommonBrand" resultType="com.epeit.api.model.CommonBrandPo" parameterType="java.lang.Long" >  
  2.         select  
  3.         id, brand_name brandName, brand_type brandType, icon, delete_flag deleteFlag  
  4.         from common_brand  
  5.         where id = #{id,jdbcType=BIGINT}  
  6.     </select>
相關文章
相關標籤/搜索