mybatis中resultMap标签中的extends属性有什么用?

在MyBatis中,通过在子类resultMap中使用extends属性继承父类的resultMap,可以避免重复定义父类已有的属性。这样在XML映射文件中,对于继承了父类的PromotionProduct类,只需要关注新增的属性集合映射,如skuStockList、productLadderList和productFullReductionList,大大减少了代码量并提高了可维护性。
摘要由CSDN通过智能技术生成

继承父类的resultMap,然后父类有的那一部分属性标签(id、result标签)就不用自己写了,例如:

子类:

public class PromotionProduct extends PmsProduct {
    //商品库存信息
    private List<PmsSkuStock> skuStockList;
    //商品打折信息
    private List<PmsProductLadder> productLadderList;
    //商品满减信息
    private List<PmsProductFullReduction> productFullReductionList;
    ……
}

父类:

public class PmsProduct implements Serializable {
        private Long id;

        private Long brandId;

        private Long productCategoryId;
        
        ……
}

XXXMapper.xml:

<mapper namespace="com.macro.mall.mapper.PmsProductMapper">
	<resultMap id="BaseResultMap" type="com.macro.mall.model.PmsProduct">
	    <id column="id" jdbcType="BIGINT" property="id" />
	    <result column="brand_id" jdbcType="BIGINT" property="brandId" />
	    <result column="product_category_id" jdbcType="BIGINT" property="productCategoryId" />
	    …………
	</resultMap>
	
    <resultMap id="promotionProductMap" type="com.macro.mall.portal.domain.PromotionProduct" extends="com.macro.mall.mapper.PmsProductMapper.BaseResultMap">
        <id column="id" jdbcType="BIGINT" property="id" />
        <collection property="skuStockList" columnPrefix="sku_" resultMap="com.macro.mall.mapper.PmsSkuStockMapper.BaseResultMap">
        </collection>
        <collection property="productLadderList" columnPrefix="ladder_" resultMap="com.macro.mall.mapper.PmsProductLadderMapper.BaseResultMap">
        </collection>
        <collection property="productFullReductionList" columnPrefix="full_" resultMap="com.macro.mall.mapper.PmsProductFullReductionMapper.BaseResultMap">
        </collection>
    </resultMap>
</mapper>

说明:

PromotionProduct对应第2个resultMap标签,PmsProduct对应第1个resultMap标签,其中PromotionProduct类继承了PmsProduct类,那么按理来说第2个resultMap标签应该写上所有的的属性,包括从PmsProduct中继承过来的属性,不过我们已经写了第1个resultMap标签,对于那些从PmsProduct中继承过来的属性,直接使用extends属性继承一下就可以 了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值