MyBatis批量更新(update foreach)報錯問題
MyBatis批量更新報錯解決
在使用mybatis執(zhí)行批量更新(update foreach)數(shù)據的時候
報錯如下:
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
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java?C++題解leetcode字符串輪轉KMP算法詳解
這篇文章主要為大家介紹了Java?C++題解leetcode字符串輪轉KMP算法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09