久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

解決mybatis批量更新(update foreach)失敗的問(wèn)題

瀏覽:183日期:2023-10-21 10:25:20

如下所示:

<!--批量更新報(bào)表 --> <update parameterType='java.util.List'> <foreach collection='issueList' item='item' index='index' separator=';'> update sys_issue <set> <if test='item.first != null and item.first != ’’'>first_class = #{item.first}, </if> <if test='item.second != null and item.second != ’’'>second_class = #{item.second}, </if> updated_time = now() </set> where id = #{item.Id} </foreach> </update>

報(bào)錯(cuò)如下:

The error occurred while setting parameters

問(wèn)題描述:

上網(wǎng)查詢說(shuō)是 配置mysql的時(shí)候沒(méi)有開(kāi)啟批量插入,就查詢了項(xiàng)目 是否 配置了 allowMultiQueries=true ,發(fā)現(xiàn)本地和線上都配置了該語(yǔ)句,問(wèn)題沒(méi)出在這。那么問(wèn)題出在哪呢?后來(lái)發(fā)現(xiàn)是由于線上將 & 變成了 &amp; 造成的。

* 前后對(duì)比如下:*

修改前:

jdbc.url=jdbc:mysql://XXX/abc?useUnicode=true&zeroDateTimeBehavior=convertToNull&amp;allowMultiQueries=true

修改后:

jdbc.url=jdbc:mysql://XXX/abc?useUnicode=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true

補(bǔ)充知識(shí):Mybatis 批量更新失敗,單條成功

在項(xiàng)目開(kāi)發(fā)過(guò)程中,結(jié)合業(yè)務(wù)場(chǎng)景,需要對(duì)某個(gè)表進(jìn)行批量更新,完成所有的業(yè)務(wù)代碼和Mybatis XML文件后,單元測(cè)試時(shí),發(fā)現(xiàn)調(diào)用批量更新只有一條記錄時(shí),執(zhí)行成功,傳入多條記錄,進(jìn)行批量更新時(shí),則提示失敗,錯(cuò)誤信息如下:

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:

其中XML映射批量更新的結(jié)構(gòu)如下:

<!-- 批量更新 --> <update parameterType='java.util.List'> <foreach collection='list' item='item' separator=';'> update xxxxxx <set> <if test='item.xxx!= null'> xxx= #{item.xxx,jdbcType=BIGINT}, </if>...... </set> where id =#{item.id} </foreach> </update>

經(jīng)過(guò)代碼排查,以及批量update語(yǔ)句通過(guò)SQL工具直接執(zhí)行均能成功,排除代碼和sql語(yǔ)句問(wèn)題,最后求助Google大神,發(fā)現(xiàn)使用mybatis進(jìn)行批量插入與更新時(shí),必須在配置連接url時(shí)指定allowMultiQueries=true

mysql.db.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&&allowMultiQueries=true

經(jīng)測(cè)試,完美解決此問(wèn)題。

以上這篇解決mybatis批量更新(update foreach)失敗的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

相關(guān)文章:
主站蜘蛛池模板: 安龙县| 芒康县| 石河子市| 遂平县| 和田县| 永顺县| 镇安县| 桑日县| 兴文县| 江油市| 关岭| 蒙城县| 德兴市| 泗阳县| 鲜城| 库伦旗| 广水市| 平陆县| 治县。| 灵武市| 乌拉特中旗| 长葛市| 汉沽区| 夏津县| 波密县| 河间市| 文昌市| 临江市| 南岸区| 商城县| 佛冈县| 漯河市| 闵行区| 永济市| 安阳市| 延边| 邯郸市| 视频| 高雄市| 玉龙| 岚皋县|