MyBatis批量更新(update foreach)報錯問題
MyBatis批量更新報錯解決
在使用mybatis執(zhí)行批量更新(update foreach)數(shù)據(jù)的時候
報錯如下:
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '......
### The error may involve ....
### The error occurred while setting parameters ### SQL:
剛開始一直以為是自己SQL寫錯了,但是檢查N遍,發(fā)現(xiàn)也沒問題,而且到Navicat中執(zhí)行也沒說SQL報錯,
批量更新SQL如下:
<!--批量更新報表 -->
<update id="updateBatchUser" parameterType="java.util.List">
<foreach collection="userList" item="item" index="index" separator=";">
update sys_user
<set>
<if test="item.userName != null and item.userName!= ''">user_name = #{item.userName}, </if>
<if test="item.userNo != null">user_no = #{item.userNo }, </if>
...
updated_time = now()
</set>
where id = #{item.id}
</foreach>
</update>解決方案
MySQL的批量更新是要我們主動去設置的
使用mybatis進行批量插入與更新時
必須在配置連接url時加上 &allowMultiQueries=true 即可

jdbc:mysql://xx:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true&allowMultiQueries=true
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在剛開始學習Java的時候,就了解了Java基礎中的變量,雖然知道這個以后會經(jīng)常用到,但沒想到了基本語法這里,竟然又冒出來了成員變量和局部變量。變來變?nèi)ヌ菀鬃屓烁銜灹?,今天我們就挑揀出來梳理一下?/div> 2016-07-07
Java?C++題解leetcode字符串輪轉(zhuǎn)KMP算法詳解
這篇文章主要為大家介紹了Java?C++題解leetcode字符串輪轉(zhuǎn)KMP算法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09最新評論

