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

动态删除购物车中的商品

错误的写法:

 <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”作为键。"

 

http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html

猜你喜欢

转载自wzf7065.iteye.com/blog/2292770