为了帮助网友解决“mybatis 中使用foreach 传”相关的问题,中国学网通过互联网对“mybatis 中使用foreach 传”相关的解决方案进行了整理,用户详细问题包括:mybatismapsql
<update id="updateallByEntity" parameterType="java.util.List"> update T_WEEKDAY <foreach item="item" index="index" collection="list" open="{" separator="," close="}"> <set> <if test="item.workflag!=null "> WORKFLAG=#{item.workflag,jdbcType=DECIMAL}, </if> <if test="item.updateBy!=null "> UPDATE_BY=#{item.updateBy,jdbcType=VARCHAR}, </if> <if test="item.updateDate!=null "> UPDATE_DATE=#{item.updateDate,jdbcType=TIMESTAMP}, </if> </set> <where> <if test="item.weekdayId!=null "> WEEKDAY_ID=#{item.weekdayId,jdbcType=DECIMAL}, </if> </where> </foreach> </update>
传入的list的值为:
[{updateBy=21, weekdayId=1, updateDate=Thu Mar 26 22:36:44 CST 2015, workflag=1}, {updateBy=21, weekdayId=2, updateDate=Thu Mar 26 22:36:44 CST 2015, workflag=1}, {updateBy=21, weekdayId=3, updateDate=Thu Mar 26 22:36:44 CST 2015, workflag=1}, {updateBy=21, weekdayId=4, updateDate=Thu Mar 26 22:36:44 CST 2015, workflag=1}, {updateBy=21, weekdayId=5, updateDate=Thu Mar 26 22:36:44 CST 2015, workflag=1}, {updateBy=21, weekdayId=6, updateDate=Thu Mar 26 22:36:44 CST 2015, workflag=0}, {updateBy=21, weekdayId=7, updateDate=Thu Mar 26 22:36:44 CST 2015, workflag=1}]
报错:
org.springframework.jdbc.UncategorizedSQLException:
### Error updating database. Cause: java.sql.SQLException: 出现不支持的 SQL92 标记: 22:
### The error may involve com.bsth.si.mapper.WeekdayMapper.updateallByEntity-Inline
### The error occurred while setting parameters
### Cause: java.sql.SQLException: 出现不支持的 SQL92 标记: 22: ,具体解决方案如下:
解决方案1:
UPDATE_DATE=#{item.updateDate,jdbcType=TIMESTAMP},
你第12代码多个逗号吧,
直接把sql打印出来确认一下哪的错误
解决方案2:
Executing: update T_WEEKDAY SET ( WORKFLAG=?, UPDATE_BY=?, UPDATE_DATE=? WHERE WEEKDAY_ID=? ; WORKFLAG=?, UPDATE_BY=?, UPDATE_DATE=? WHERE WEEKDAY_ID=? ; WORKFLAG=?, UPDATE_BY=?, UPDATE_DATE=? WHERE WEEKDAY_ID=? ; WORKFLAG=?, UPDATE_BY=?, UPDATE_DATE=? WHERE WEEKDAY_ID=? ; WORKFLAG=?, UPDATE_BY=?, UPDATE_DATE=? WHERE WEEKDAY_ID=? ; WORKFLAG=?, UPDATE_BY=?, UPDATE_DATE=? WHERE WEEKDAY_ID=? ; WORKFLAG=?, UPDATE_BY=?, UPDATE_DATE=? WHERE WEEKDAY_ID=? )
现在是报缺失右括号
map如下:
<update id="updateallByEntity" parameterType="list">
update T_WEEKDAY
<set>
<foreach item="item" index="key" collection="list" open="(" separator=";" close=")">
WORKFLAG=#{item.workflag,jdbcType=DECIMAL},
UPDATE_BY=#{item.updateBy,jdbcType=VARCHAR},
UPDATE_DATE=#{item.updateDate,jdbcType=TIMESTAMP}
WHERE WEEKDAY_ID=#{item.weekdayId,jdbcType=DECIMAL}
</foreach>
</set>
</update>
求解啊,今天搞了一晚上了!
解决方案3:
http://bbs.csdn.net/topics/380234660
受以上贴子中最后一个人的回复,我解决了我,mybatis中使用foreach,批量update多个对象的问题。在此感谢:ypin2003
其实想通了,就很简单了,只要通过foreach直接把需要的多个update用begin end;包起来就可以实现批量更新了
例如:
<update id="updatesql" >
<foreach collection="list" item="item" open="begin" separator=";" close=";end;">
UPDATE A SET colC = #{item.c}
WHERE (colA = #{item.a} ADN colB = #{item.b})
</foreach>
</update>
原文:http://www.cnblogs.com/xuerong/p/5299546.html