MyBatis查询不出关联数据Collection属性和resultMap属性


tags: 解决错误


#Mybatis中Collection的resultMap和ofType选择#

在做项目的时候,关联List集合的数据可以使用Collection标签来进行关联。

在教程里边是使用ofType属性拿到数据的,但我觉得resultMap和ofType没什么区别呀。于是我就使用了resultMap属性了。



        <!--与最小销售单元的关系-->
        <collection property="ebSkus" resultMap="com.rl.ecps.sqlMap.EbSkuMapper.BaseResultMap">
            
            <!--最小销售单元与规格值的关系-->
               <collection property="specList" resultMap="com.rl.ecps.sqlMap.EbSpecValueMapper.BaseResultMap"></collection>

        </collection>
复制代码

但是死活拿不到speceList的数据....

  • 如果在集合中还有对象的关系要体现出来的话,那我们只能使用ofType。
  • 如果集合中都是对象本身基本属性了,那么可以使用resultMap


<collection property="skuList" ofType="com.rl.ecps.model.EbSku">
  		<id column="SKU_ID" jdbcType="DECIMAL" property="skuId" />
	    <result column="ITEM_ID" jdbcType="DECIMAL" property="itemId" />
	    <result column="SKU" jdbcType="VARCHAR" property="sku" />
	    <result column="SKU_PRICE" jdbcType="DECIMAL" property="skuPrice" />
	    <result column="SHOW_STATUS" jdbcType="DECIMAL" property="showStatus" />
	    <result column="STOCK_INVENTORY" jdbcType="DECIMAL" property="stockInventory" />
	    <result column="SKU_UPPER_LIMIT" jdbcType="DECIMAL" property="skuUpperLimit" />
	    <result column="LOCATION" jdbcType="VARCHAR" property="location" />
	    <result column="SKU_IMG" jdbcType="VARCHAR" property="skuImg" />
	    <result column="SKU_SORT" jdbcType="DECIMAL" property="skuSort" />
	    <result column="SKU_NAME" jdbcType="VARCHAR" property="skuName" />
	    <result column="MARKET_PRICE" jdbcType="DECIMAL" property="marketPrice" />
	    <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime" />
	    <result column="UPDATE_TIME" jdbcType="TIMESTAMP" property="updateTime" />
	    <result column="CREATE_USER_ID" jdbcType="DECIMAL" property="createUserId" />
	    <result column="UPDATE_USER_ID" jdbcType="DECIMAL" property="updateUserId" />
	    <result column="ORIGINAL_SKU_ID" jdbcType="DECIMAL" property="originalSkuId" />
	    <result column="LAST_STATUS" jdbcType="DECIMAL" property="lastStatus" />
	    <result column="MERCHANT_ID" jdbcType="DECIMAL" property="merchantId" />
	    <result column="SKU_TYPE" jdbcType="DECIMAL" property="skuType" />
	    <result column="SALES" jdbcType="DECIMAL" property="sales" />
	    <result column="RES_CODE" jdbcType="VARCHAR" property="resCode" />
	    <result column="PACK_ID" jdbcType="DECIMAL" property="packId" />
	    <collection property="specList" ofType="com.rl.ecps.model.EbSpecValue">
	    	<id column="SPEC_ID" jdbcType="DECIMAL" property="specId" />
		    <result column="SKU_ID" jdbcType="DECIMAL" property="skuId" />
		    <result column="FEATURE_ID" jdbcType="DECIMAL" property="featureId" />
		    <result column="SPEC_VALUE" jdbcType="VARCHAR" property="specValue" />
	    </collection>
  	</collection>
复制代码

总结

如果关联的对象仅仅是当前对象的本身,那么我们可以使用resultMap

如果关联对象中还有其他关联属性的话,那只能使用ofType了。

如果您觉得这篇文章帮助到了您,可以给作者一点鼓励

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值