解决mybatis批量更新出现SQL报错问题
作者:拒绝熬夜啊 时间:2023-11-29 04:12:47
一、问题重现
1.配置文件
spring:
#DataSource数据源
datasource:
url: jdbc:mysql://127.0.0.1:3306/mybatis_test?useSSL=false&
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#MyBatis配置
mybatis:
type-aliases-package: com.hl.mybatis.pojo #别名定义
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #指定 MyBatis 所用日志的具体实现,未指定时将自动查找
map-underscore-to-camel-case: true #开启自动驼峰命名规则(camel case)映射
lazy-loading-enabled: true #开启延时加载开关
aggressive-lazy-loading: false #将积极加载改为消极加载(即按需加载),默认值就是false
lazy-load-trigger-methods: "" #阻挡不相干的操作触发,实现懒加载
cache-enabled: true #打开全局缓存开关(二级环境),默认值就是true
2.sql
@Update({"<script>" +
"<foreach item='item' collection='list' index='index' open='' close='' separator=';'>" +
" UPDATE tb_user " +
"<set>" +
"<if test='item.userAccount != null'>user_account = #{item.userAccount},</if>" +
"<if test='item.userPassword != null'>user_password=#{item.userPassword}</if>" +
"</set>" +
" WHERE user_id = #{item.userId} " +
"</foreach>" +
"</script>"})
int updateBatch(@Param("list")List<UserInfo> userInfoList);
3.测试
查看控制台错误
发现这里告诉我有一个语法错误,然后发现user_id,有一个符号。>
这里经过测试更新一条是成功的
二、问题分析
Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行
但是在SQL编辑器中执行多条sql语句的时候是可以以分号结尾的,如:
三、解决方法
在application.properties配置文中的数据源url后面添加一个参数
&allowMultiQueries=true【允许sql语句中有多个insert或者update语句 == 支持sql批量操作】
原来的配置文件:
url: jdbc:mysql://127.0.0.1:3306/mybatis_test?useSSL=false&
现在的配置文件
url: jdbc:mysql://127.0.0.1:3306/mybatis_test?useSSL=false&&&allowMultiQueries=true
再次测试
来源:https://blog.csdn.net/weixin_43296313/article/details/122996637
标签:mybatis,批量更新,SQL,报错
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
opencv3/C++图像滤波实现方式
2023-06-23 15:37:08
![](https://img.aspxhome.com/file/2023/0/82530_0s.jpg)
C#生成带注释的dll并引用实现
2023-08-31 16:01:09
![](https://img.aspxhome.com/file/2023/2/86992_0s.png)
Java Exception 捕获和显示实例详解
2022-07-05 22:41:09
Android仿支付宝中余额宝的数字动画效果
2021-10-17 08:32:54
![](https://img.aspxhome.com/file/2023/1/137721_0s.gif)
解决IDEA鼠标点击光标变大问题
2022-12-07 11:52:37
![](https://img.aspxhome.com/file/2023/4/79964_0s.jpg)
Android开发实现拨打电话与发送信息的方法分析
2023-06-19 07:09:13
Android application捕获崩溃异常怎么办
2023-09-26 11:07:12
Java流式操作之Collectors工具类操作指南
2022-12-01 01:25:55
![](https://img.aspxhome.com/file/2023/9/72389_0s.png)
dotnet core链接mongodb代码实例
2023-07-20 14:26:09
Windows+Apache+resin配置
2023-10-16 08:53:07
C# 中如何利用lambda实现委托事件的挂接
2022-02-06 03:18:00
Java maven三种仓库,本地仓库,私服,中央仓库的配置
2023-04-13 12:05:17
![](https://img.aspxhome.com/file/2023/2/62132_0s.png)
unity实现翻页效果
2022-09-01 17:10:15
![](https://img.aspxhome.com/file/2023/7/119607_0s.gif)
教你使用idea搭建ssm详细教程(Spring+Spring Mvc+Mybatis)
2021-09-23 12:37:42
![](https://img.aspxhome.com/file/2023/6/62736_0s.png)
浅谈java内存管理与内存溢出异常
2022-04-22 15:03:18
![](https://img.aspxhome.com/file/2023/2/66252_0s.png)
Unity3D更改默认的脚本编辑器
2021-06-05 14:58:53
![](https://img.aspxhome.com/file/2023/5/68625_0s.png)
android开发教程之view组件添加边框示例
2023-05-24 17:16:44
Spring中校验器(Validator)的深入讲解
2022-03-05 11:58:35
![](https://img.aspxhome.com/file/2023/5/71245_0s.jpg)
详解在LINUX上部署带有JAR包的JAVA项目
2022-12-30 21:07:14
![](https://img.aspxhome.com/file/2023/7/78427_0s.png)
使用Java实现类似Comet风格的web app
2023-04-01 10:23:22