The expression 'list' evaluated to a null value

mybatis 使用foreach时出现"The expression 'list' evaluated to a null value"问题

动态删除购物车中的商品

错误的写法:

 <update 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> 
</update>

正确的写法:

 <update 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> 
</update>

错误的原因在于:

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

参考自:http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html

文章地址:https://blog.csdn.net/gufachongyang02/article/details/27186823

猜你喜欢

转载自blog.csdn.net/qq_36698956/article/details/90204225