MyBatis批量修改&解决批量修改出现的问题
先贴一下我的代码
<update id="updateUsers" parameterType="LinkedList" >
<foreach collection="list" item="user" index="index" open="" close="" separator=";">
UPDATE TS_STAFF
<set>
<if test="user.userName != null and user.userName != ''">
STAFF_NAME = #{user.userName},
</if>
<if test="user.groupId != null and user.groupId != ''">
ORG_ID = #{user.groupId},
</if>
<if test="user.userPwd != null and user.userPwd != ''">
PASSWD = #{user.userPwd},
</if>
</set>
WHERE USER_UUID = #{user.userUUID}
</foreach>
</update>
这里要着重讲一下标签里每个字段结尾的的逗号,有两种写法:
- 每个字段后面都写逗号
- 最后一个字段后面不写逗号,其余都写
重点来了!
如果报以下错误:
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE TS_STAFF
SET STAFF_NAME = '谢琳_南充',
' at line 11
看到这个错误,先不要去找逗号和其它问题,先用博主提供的这种方式:
在你连接数据库的配置文件中,找到url
然后在末尾加上一个参数:
&allowMultiQueries=true
OK,解决!
allowMultiQueries=true的作用:
- 可以在sql语句后携带分号,实现多语句执行
- 可以执行批处理,同时发出多个SQL语句