MyBatis的返回參數類型


MyBatis的返回參數類型分兩種

1. 對應的分類為:

1.1.resultMap:

1.2.resultType:

 

2 .對應返回值類型:

2.1.resultMap:結果集

2.2.resultType:int,string ,long ,class

3. 注意點:

在MyBatis進行查詢映射時,其實查詢出來的每一個屬性都是放在一個對應的Map里面的,其中鍵是屬性名,值則是其對應的值。
     
3.1 當提供的返回類型屬性是resultType時,MyBatis會將Map里面的鍵值對取出賦給resultType所指定的對象對應的屬性。所以其實MyBatis的每一個查詢映射的返回類型都是ResultMap,只是當提供的返回類型屬性是resultType的時
候,MyBatis對自動的給把對應的值賦給resultType所指定對象的屬性。
     
3.2 當提供的返回類型是resultMap時,因為Map不能很好表示領域模型,就需要自己再進一步的把它轉化為對應的對象,這常常在復雜查詢中很有作用。

4.案例

4.1:resultMap案例

<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案例

<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案例

<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案例:查詢結果對應類中的屬性值

<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>  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM