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 用于不符合驼峰规则、字段名重复,层级关系复杂的自由手动映射