mybatis resultMap整理

resultMap 是手动建立一种 映射javaBean 的 关联关系。

resultType 是一种自动映射javaBean的映射关系类型,通常配置驼峰命名来自动匹配。


使用场景:

resultType:

用于简单的javaBean映射,或者复杂的层级关系里面 ,用n+1 的查询


<resultMap type="Product" id="ProductResultMapT">
<id property="id" column="a_id" />
<result property="showName" column ="showName"/>

<result property="propsinit" column ="a_propsinit"/>
<result property="isHighlight" column ="a_is_highlight"/>
<result property="createTime" column ="a_create_time"/>
<result property="modifyTime" column ="a_modify_time"/>
<result property="minSalePrice" column ="a_min_sale_price"/>
<result property="referProductId" column ="a_refer_product_id"/>
<result property="isDirectSale" column ="a_is_direct_sale"/>
<result property="indexImage" column ="a_index_image"/>
<result property="price" column ="a_price"/>
<result property="articleNumber" column ="a_article_number"/>
<result property="category.weight" column ="b_weight"/>
<result property="state" column ="a_state"/>
<result property="isBlock" column ="a_is_block"/>
<result property="isShowcase" column ="a_is_showcase"/>
<result property="fileInfo" column ="a_file_info"/>
<result property="category.title" column="b_title" />
<result property="categoryId" column="a_category_id" />
<result property="labelId" column="a_label_id" />
<result property="memberFlag" column="a_is_hide" /><!-- 是否隐藏人气 -->

<result property="productStats.downCountAlltime" column="c_down_count_alltime" />
<result property="productStats.taobaoCountAlltime" column="c_taobao_count_alltime" />
<result property="productStats.totalperson" column="totalperson" />
   <result property="complain.id" column="mid" />
   <result property="complain.isFixed" column="is_fixed" />
   <result property="complain.createTime" column="mcreateTime" />
   <result property="complain.credit" column="credit" />
   <result property="complain.ip" column="ip" />
   <collection property="productTags" column="a_id" select="getProductTags" ofType="ProductTag">     <!-- 产品打标 -->
   
    </collection>

</resultMap>


像这种,便用到了n+1查询,1表示外层,n表示1对多的产品打标。  但通常不推荐n+1 查询,比如这里 外层是产品,下一级是产品打标,是一对多的关系,

当然这种可以多级,  n就查询了n次,这种效率不是很好,

resultMap:


<resultMap type="Product" id="ProductResultMapT">
<id property="id" column="a_id" />
<result property="showName" column ="showName"/>

<result property="propsinit" column ="a_propsinit"/>
<result property="isHighlight" column ="a_is_highlight"/>
<result property="createTime" column ="a_create_time"/>
<result property="modifyTime" column ="a_modify_time"/>
<result property="minSalePrice" column ="a_min_sale_price"/>
<result property="referProductId" column ="a_refer_product_id"/>
<result property="isDirectSale" column ="a_is_direct_sale"/>
<result property="indexImage" column ="a_index_image"/>
<result property="price" column ="a_price"/>
<result property="articleNumber" column ="a_article_number"/>
<result property="category.weight" column ="b_weight"/>
<result property="state" column ="a_state"/>
<result property="isBlock" column ="a_is_block"/>
<result property="isShowcase" column ="a_is_showcase"/>
<result property="fileInfo" column ="a_file_info"/>
<result property="category.title" column="b_title" />
<result property="categoryId" column="a_category_id" />
<result property="labelId" column="a_label_id" />
<result property="isHideStats" column="a_is_hide" /><!-- 是否隐藏人气 -->

<result property="productStats.downCountAlltime" column="c_down_count_alltime" />
<result property="productStats.taobaoCountAlltime" column="c_taobao_count_alltime" />
<result property="productStats.totalperson" column="totalperson" />
   <result property="complain.id" column="mid" />
   <result property="complain.isFixed" column="is_fixed" />
   <result property="complain.createTime" column="mcreateTime" />
   <result property="complain.credit" column="credit" />
   <result property="complain.ip" column="ip" />
    <collection property="productTags" column="id" ofType="ProductTag">     <!-- 产品打标 -->
    <result property="icon"  column="icon" />
    <result property="name"  column="icon_name" />
    </collection>
</resultMap>

这种,可以一次查询,由mybatis进行组装,但是不能满足分页。可以把由产品id,去执行一个in查询,这种效率可以,特别在于笛卡尔乘积很大的时候,可以拆分成很多的in查询。提高效率。

resultMap 用于不符合驼峰规则、字段名重复,层级关系复杂的自由手动映射


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值