小问题

记一个开发过程中因为小细节的遗漏而引发的 "莫名其妙",公司中有个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. 02 sublime text3下配置Python的编译运行环境

    内容参考如下文章,加入了自己的干货: https://www.cnblogs.com/huluwahaha/p/7503930.html 在sublime中如何配置Python的编译环境呢? 打开Su ...

  2. AE2018简单的编辑

    来源:https://jingyan.baidu.com/article/1876c8525cf522890a137651.html Ae 2018 怎样锁定图层,阻止对图层进行编辑? 听语音 原创 ...

  3. JavaFX FileChooser文件选择器,缓存上一次打开的目录

    例1:点击按钮Choose File打开文件选择器,并打开指定的目录.这是通过final void setInitialDirectory(final File value)方法实现的. 1 impo ...

  4. vue点击切换样式,点击切换地址栏,点击显示或者隐藏

    1. vue点击显示切换 :class='{"span":index==0}' class原本是 类选择器 加上 :class就是绑定属性的意思 '{"span" ...

  5. css做模糊处理

    -webkit-filter: blur(9px); filter: blur(9px);

  6. webpack5文档解析(上)

    webpack5 声明:所有的文章demo都在我的仓库里 webpack5 起步 概念 webpack是用于编译JavaScript模块. 一个文件依赖另一个文件,包括静态资源(图片/css等),都会 ...

  7. operator bool()是什么

    operator bool()是什么 在C++中,operator TypeName()语法用来将对象转换为指定的TypeName类型,当这里TypeName为bool时,就可以直接在条件判断式里面直 ...

  8. form中的标签例子

    <form action="dreamdu.php" method="post" id="dreamduform"> <f ...

  9. 不要以为Bug写的好就是好程序员,其实这只占不到15%!

      最近和一位从事多年架构工作的技术哥们见面,聊到了近期面试程序员的一些经历,谈到了"如何判断程序员水平高低"这个话题,颇有些感触,觉得有价值,因此花了些时间整理.分享给大家. 正 ...

  10. golang 爬取百度贴吧绝地求生页面

    package main import ( "github.com/antchfx/htmlquery" "io" "net/http" & ...