当前位置: 代码迷 >> 综合 >> mybatis 使用foreach时出现"The expression #39;list#39; evaluated to a null
  详细解决方案

mybatis 使用foreach时出现"The expression #39;list#39; evaluated to a null

热度:91   发布时间:2023-09-20 02:25:11.0

 动态删除购物车中的商品

错误的写法:

 <delete id="deleteCartByMultiGoodsId" parameterType="java.util.HashMap">
 delete from ecs_cart where  user_id=#{userId}
 and  goods_id in  
   <foreach collection="list" item="goodsIdList" index="index" open="(" separator="," close=")">  
       #{goodsIdList}  
     </foreach> 
</delete>

正确的写法:

 <delete id="deleteCartByMultiGoodsId" parameterType="java.util.HashMap">
  delete from ecs_cart where  user_id=#{userId}
   and  goods_id in  
    <foreach collection="goodsIdList" item="goodsIdList" index="index" open="(" separator="," close=")">  
        #{goodsIdList}  
     </foreach> 
</delete>

错误的原因在于:

"你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时 候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。"

还有种可能是map中key对应的list是null

  相关解决方案