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了。
如果您觉得这篇文章帮助到了您,可以给作者一点鼓励