当前位置: 首页 > news >正文

MyBatis配置允许批量插入或更新数据

MyBatis配置allowMultiQueries=true允许使用foreach标签批量插入或更新数据

执行update更新操作:

<!-- 批量更新 --><update id="updateBatchByKey" parameterType="java.util.List"><foreach collection="list" item="item" index="index" separator=";">UPDATE order_main_product<set><if test="item.factoryId != null">factory_id = #{item.factoryId},</if><if test="item.factoryCode != null and item.factoryCode != ''">factory_code = #{item.factoryCode},</if><if test="item.factoryName != null and item.factoryName != ''">factory_name = #{item.factoryName},</if><if test="item.orderId != null">order_id = #{item.orderId},</if><if test="item.parentId != null">parent_id = #{item.parentId},</if><if test="item.shipmentAmount != null and item.shipmentAmount != ''">shipment_amount =#{item.shipmentAmount},</if>......</set>WHERE id = #{item.id}</foreach></update>

执行报错:

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 'UPDATEorder_main_product
SETproduct_count = 1,delivery_time = '2024-08' at line 2错误位置: line: 1

解决:更改jdbc配置,加上allowMultiQueries=true

url: jdbc:mysql://172.16.1.145:3306/new_crm?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true

在MySQL连接中设置allowMultiQueries=true允许在SQL语句后使用分号分隔,执行多个命令。

原因:

MyBatis默认情况下不允许批量插入或更新数据的原因是出于安全考虑。在许多情况下,数据的插入和更新都需要经过验证和控制,以确保数据的完整性和一致性。如果允许默认的批量操作,可能会导致不正确的数据插入或更新,从而影响应用程序的正常运行。

通过配置allowMultiQueries=true可以开启MyBatis的批量操作功能。这个配置项告诉MyBatis允许在单个数据库连接中执行多个SQL语句,从而实现批量插入或更新数据的功能。但是要注意,在开启批量操作之前,确保你已经了解并理解了可能引发的安全风险,并且在使用批量操作时要进行适当的验证和控制,以确保数据的完整性和安全性。

尽管 allowMultiQueries 可以用于某些情况下的方便性,但出于安全和性能的考虑,通常不建议在生产环境中启用它。


http://www.mrgr.cn/news/7767.html

相关文章:

  • DevExpress中Blazor部分学习
  • 前端实现截图(Vue)
  • ansible:远程自动化运维
  • 【赵渝强老师】执行Oracle的冷备份与冷恢复
  • 【EI会议征稿】第四届能源、动力与电气工程国际学术会议(EPEE 2024)
  • 【Hot100】LeetCode—543. 二叉树的直径
  • linux上datax 安装以及使用
  • 在线英语学习小程序App源码开发技术探讨
  • 鸿蒙HarmonyOS实战:IPC与RPC设备内进程通信
  • Android 应用中广播权限未指定风险与解决方案
  • Linux 可视化管理工具:Webmin
  • 【三维室内数据集】ScanNet v2使用说明
  • 分区表学习相关资料记录
  • (十三)Flink SQL
  • linux查询目录文件基础操作
  • 使用静态IP为什么比动态IP的人多?
  • 如何实现一棵红黑树
  • OpenCV+Python识别机读卡
  • 博客建站7 - hexo博客独立服务器如何自动部署?
  • java JVM G1垃圾收集器一些主要特性和工作原理