小问题

记一个开发过程中因为小细节的遗漏而引发的 "莫名其妙",公司中有个2B(to B)供应链项目,持久层用的是 JPA,这里我就不吐槽 JPA 了,这种 SQL 嵌入在代码里的方式真的不够简洁。

由于是新功能的开发,查询的功能中需要多字段的条件查询,涉及到多张表的关联操作。我试着用 JPA 来写,发现写的很头疼,于是干脆来个改造,将 Mybatis 引入到了项目中,自己掌控 SQL 的感觉还是蛮好的,而且 SQL 和业务代码分离。

报错了

查询,插入,删除,更新这些功能都还好,但是在一次需要批量更新的时候报错了,提示我 SQL语法错误。

Mapper接口的代码如下

@Repository
public interface BasCustomerReservationOrderItemMapper {
void UpdateBatchByIds(@Param("list") List<BasCustomerReservationOrderItem> basCustomerReservationOrderItemList);
}

SQL 映射 xml 文件内容如下

<update id="UpdateBatchByIds">
<foreach collection="list" item="record" index="index" open="" close="" separator=";">
update bas_customer_reservation_order_item
<set>
<if test="record.artno != null">
artno = #{record.artno,jdbcType=VARCHAR},
</if>
<if test="record.quantity != null">
quantity = #{record.quantity,jdbcType=DECIMAL},
</if>
<if test="record.sort != null">
sort = #{record.sort,jdbcType=INTEGER},
</if>
<if test="record.unit != null">
unit = #{record.unit,jdbcType=VARCHAR},
</if>
<if test="record.createBy != null">
create_by = #{record.createBy,jdbcType=VARCHAR},
</if>
<if test="record.createDate != null">
create_date = #{record.createDate,jdbcType=TIMESTAMP},
</if>
<if test="record.updateBy != null">
update_by = #{record.updateBy,jdbcType=VARCHAR},
</if>
<if test="record.updateDate != null">
update_date = #{record.updateDate,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{record.id,jdbcType=BIGINT}
</foreach>
</update>

这个批量更新语句很简单,但是执行的时候确总是报语法错误,报错信息如下

### SQL: update bas_customer_reservation_order_item        SET artno = ?,                             quantity = ?,                             sort = ?,                             unit = ?,                                               update_by = ?,                             update_date = ?        where id = ?      ;        update bas_customer_reservation_order_item        SET artno = ?,                             quantity = ?,                             sort = ?,                             unit = ?,                                               update_by = ?,                             update_date = ?        where id = ?
### Cause: java.sql.SQLSyntaxErrorException: 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 'update bas_customer_reservation_order_item
SET artno = '100014',
' at line 22
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 'update bas_customer_reservation_order_item
SET artno = '100014',
' at line 22

字段类型,中英文符号,……,我全部检查了一遍都没发现错误,按照提示的line 22那里反复检查也确实没有错误呀。

原来如此

报错信息误导了我,导致我一直纠结在 SQL 语法那块,后来突然智慧之光一闪,单个的查询更新都没问题,只是这个批量处理的时候有问题,是不是不支持批量的呢?

通过查询相关文档后发现,原来如此

Mybatis 映射文件中的 sql 语句默认是不支持以" ; " 结尾的,也就是不支持多条 sql 语句的执行。如果要支持这种语句,需要在连接 mysql 的 url 上加 &allowMultiQueries=true 这个才可以执行。

所以只需要在application.yml的数据源连接配置的地方加上&allowMultiQueries=true即可

spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver

再次调试,这时就可以正常执行了,数据得以更新成功,细节决定成败。

Mybatis 批量更新遇到的小问题的更多相关文章

  1. mybatis批量更新报错badsql

    mybatis批量更新时语法写的都对,但是报错,需要在连接上面加上allowMultiQueries=true 示例:jdbc:MySQL://192.168.1.236:3306/test?useU ...

  2. mybatis批量更新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

    mybatis批量更新update-设置多个字段值 2016年08月01日 12:49:26 姚一号 阅读数:29539 标签: mysql mybatis批量更新批量更新allowMultiQuer ...

  3. Mybatis批量更新<转>

    Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插入还有批量删除的帖子.但是批量更新却没有详细的解决方案. 实现目标 这里主要讲的是1张table中.根 ...

  4. Mybatis批量更新详解

    转:http://www.cnblogs.com/winkey4986/p/3915151.html Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插 ...

  5. mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样

    Mybatis批量更新数据 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批 ...

  6. mybatis批量更新策略

    我们知道循环中操作db会导致连接数满,严重影响数据库性能.所以在对db进行DQL与DML时,根据业务逻辑尽量批量操作,这里我们介绍下使用mybatis批量更新mysql的两种方式. 方式一: < ...

  7. Mybatis批量更新数据库与批量插入数据库(以oracle为例)

    一.批量更新 1.普通写法(一条记录update一次,性能比较差,容易造成阻塞.不建议使用) <update id="updateBatch" parameterType=& ...

  8. mybatis 批量更新 Parameter '__frch_item_0' not found. Available parameters are [list]

    一次在做批量更新数据的时候报错 Parameter '__frch_item_0' not found. Available parameters are [list] 记过反复查找,最后才发现是一个 ...

  9. MyBatis批量更新

    逐条更新 这种方式显然是最简单,也最不容易出错的,即便出错也只是影响到当条出错的数据,而且可以对每条数据都比较可控. 代码 updateBatch(List<MyData> datas){ ...

随机推荐

  1. c语言 static的用法

    static在c里面可以用来修饰变量,也可以用来修饰函数.先看用来修饰变量的时候.变量在c里面可分为存在全局数据区.栈和堆里.其实我们平时所说的堆栈是栈而不是堆,不要弄混.int a ;int mai ...

  2. 【总结】Oracle数据库 查看表空间和增加表空间

    一.Oracle查看 表空间 的名称及其大小 查看 表空间的名称及其大小的SQL语句: select t1.tablespace_name,round(sum(bytes/(1024*1024)),0 ...

  3. ubuntu19.10如何设置固定ip

    $ip a 看见系统中有两块网卡 lo: ...... ens33: ...... #cd /etc/netplan$ls目录下面有文件01-network-manager-all.yaml $sud ...

  4. arcgis-java-100.8.0.jar下载

    链接: https://pan.baidu.com/s/1HoW2IhPvHRw9LBZphxC5Rw 提取码: pexn

  5. 多测师_高级肖sir分享pycharm中设置主题和设置代码颜色方法

    一.修改pycharm中的主题颜色 二.修改代码颜色 File-->Settings-->Editor--> Color Scheme-->Language Defaults- ...

  6. day55 Pyhton 前端Jquery07

    昨日回顾: 表单,点击submit提交以后,服务端受到信息 import socket import pymysql from urllib.parse import unquote def run( ...

  7. python 编写名字管理系统

    1 #打印功能提示 2 print('='*50) 3 print(' 名字管理系统 v1.1') 4 print('1.添加新的名字') 5 print('2.删除一个名字') 6 print('3 ...

  8. oh my zsh 常用插件

    date: "2020-10-18T12:36:00+08:00" title: "oh my zsh 常用插件" tags: ["zsh" ...

  9. centos8平台:用fontconfig安装及管理字体(fc-list/fc-match/fc-cache)

    一,fc-list所属的rpm包 [root@blog ~]$ whereis fc-list fc-list: /usr/bin/fc-list /usr/share/man/man1/fc-lis ...

  10. DataX 3.0 源码解析一

    源码解析 基本调用类分析 任务启动由python脚本新建进程进行任务执行,后续执行由Java进行,以下将对java部分进行分 其中的调用原理机制. Engine 首先入口类为com.alibaba.d ...