在Mybatis实现批量更行操作时,<foreach>标签实现,实质就是将多条sql拼接后让数据库执行,然后Oracle11g是不支持在同行执行多条命令的。所以要做特殊处理。
一,未处理代码
<update id="saveNos" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";" open="" close="">
update matchtable
<set>
<if test="item.subtIds != null">
SUB_IDS = #{item.subIds, jdbcType=VARCHAR},
</if>
<if test="item.subNames != null">
SUB_NAMES = #{item.subNames, jdbcType=VARCHAR}
</if>
</set>
where id = #{item.id, jdbcType=VARCHAR}
</foreach>
</update>
这中mapper最后发到数据库中的sql脚本是将所有的sql拼接在一行发给数据库,这样Oracle11g运行是会报错误的ora-00911。
二,特殊处理
<update id="saveNos" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";" open="begin" close=";end;">
update matchtable
<set>
<if test="item.subtIds != null">
SUB_IDS = #{item.subIds, jdbcType=VARCHAR},
</if>
<if test="item.subNames != null">
SUB_NAMES = #{item.subNames, jdbcType=VARCHAR}
</if>
</set>
where id = #{item.id, jdbcType=VARCHAR}
</foreach>
</update>
即用,begin ..........;end;命令块包裹一下就可以了。