The expression ‘idList’ evaluated to a null value.
错误代码:
@Override
public int updateByID(List<Long> idList) {
return this.getSqlSession().update(NAMESPACE+".updateByID",idList);
}
<update id="updateByID" parameterType="java.util.List">
update table_name
set IS_FORWARD ='Y',DATA_STATUS = 'N'
<where>
ID IN
<foreach collection="idList" open="(" close=")" item="item" index="index" separator=",">
#{item,jdbcType=BIGINT}
</foreach>
</where>
</update>
正确代码:
java代码不动,改SQL,foreach 中的collection属性,参数是List,则固定是collection=“list”,而不是自己定义的list名称。
<update id="updateByID" parameterType="java.util.List">
update table_name
set IS_FORWARD ='Y',DATA_STATUS = 'N'
<where>
ID IN
<foreach collection="list" open="(" close=")" item="item" index="index" separator=",">
#{item,jdbcType=BIGINT}
</foreach>
</where>
</update>
原因分析:
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可