2500-使用MyBatis操作MySQL进行批量更新的注意事项

原则上一条SQL只更新一条数据库操作,但有时需要批量操作数据,特别是一些DML语句,在操作数据库时,数据库会报出异常,不允许混合语句,此时需要额外配置进行兼容。

例如:

Caused by: 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 'update purchase_instrument_bill_detail ??
? ? ? ? ? ? ? ? ?SET out_count = '1',
' at line 8
Caused by: java.sql.SQLException: sql injection violation, multi-statement not
allowcom.alibaba.druid.wall.WallFilter.check(WallFilter.java:714)
atcom.alibaba.druid.wall.WallFilter.connection_prepareStatement(WallFilter.java:240)
atcom.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:448)
atcom.alibaba.druid.filter.FilterAdapter.connection_prepareStatement(FilterAdapter.java:928)

解决方案:

  1. 数据库连接加参数
  2. 连接池需要配置

1. 数据库连接加参数

添加参数allowMultiQueries=true

可解决数据库连接层面的异常问题,指定连接数据库时,可执行混合SQL。

参考连接配置: jdbc:mysql://\({datasource.host}:\){datasource.port}/${datasource.name}?relaxAutoCommit=true&useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&useSSL=false&allowMultiQueries=true

2. 连接池需要配置

如果使用的数据库连接池是Druid,则需要额外配置参数。其他种类连接池,如C3P0,DBCP等,尚未考证。

## 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙,此处去除防火墙
spring.datasource.druid.filters=config,stat,slf4j
## 配置过滤器wall的参数
spring.datasource.druid.filter.wall.config.use-allow=false
spring.datasource.druid.filter.wall.config.multi-statement-allow=true

wall是com.alibaba.druid.wall.WallFilter的简称,提供sql的检查和过滤等功能,默认这里会对混合SQL进行拦截,此处为了执行大SQL,可关闭防火墙功能。(有SQL注入风险,谨慎使用)



附上一个MyBatis的xml脚本demo:


    <update id="batchUpdateForRepay" parameterType="com.xxx.settle.credit.generate.domain.bo.RepayUpdateCreditInfoBO">

        <foreach collection="list" index="index" item="item" separator=";">
            update tb_credit_info
            <trim prefix="set" suffixOverrides=",">
                <if test="item.state != null">state = #{item.state},</if>
                <if test="item.leftPrincipalAmount != null">left_principal = left_principal - #{item.leftPrincipalAmount},</if>
                <if test="item.currentPeriodChange != null">current_period = current_period + #{item.currentPeriodChange},</if>
                <if test="item.receivedPrincipalAmount != null">received_principal = received_principal + #{item.receivedPrincipalAmount},</if>
                <if test="item.remark != null">remark = #{item.remark},</if>
            </trim>
            where product_id = #{item.productId} and credit_id = #{item.creditId}
        </foreach>

    </update>

原文地址:https://www.cnblogs.com/starmoon1994/p/10151722.html

时间: 2024-08-29 22:01:59

2500-使用MyBatis操作MySQL进行批量更新的注意事项的相关文章

mybatis学习之路----批量更新数据两种方法效率对比

原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方式有两种, 一种用for循环通过循环传过来的参数集合,循环出N条sql, 另一种 用mysql的case when 条件判断变相的进行批量更新 下面进行实现. 注意第一种方法要想成功,需要在db链接url后面带一个参数  &allowMultiQueries=true 即:  jdbc:mysql:

一次Mysql下批量更新造成的死锁案例分析

最近,公司现网的业务中出现上图所示的死锁异常,沿着问题分析,发现这个问题涉及很多数据库的基础知识. 背景: 使用数据库:Mysql 涉及表格:t_invest 数据库隔离级别:可重复读(Repeatable Read) 死锁场景:saveRepaymentInfo事务的A()方法对t_invest表执行如下update操作: <update id = "A" parameterType = "java.util.List"> <foreach co

MySQL Workbench批量更新或删除

在使用MySQL Workbench进行批量更新或删除时,会出现如下错误: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect. 0.000 s

mybatis的一种批量更新方法【我】

接手一个项目,项目主要架构用的 servlet 3.0 + spring + mybatis 其中发现一个问题: 操作数据时,批量插入可以,批量更新,使用各种写法都无法成功,直接报 mybatis转换异常等等,最后解决方法是所有批量更新都用如下写法: userXML.xml文件 userXML.xml文件 <!-- 更新user表 --> <insert id="updateUser" parameterType="java.util.List"&

kotlin + springboot整合mybatis操作mysql数据库及单元测试

项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller层测试参考: https://www.cnblogs.com/PollyLuo/p/9630822.html mysql版本:5.5.62 点击下载 1.kotlin版springboot项目创建 访问https://start.spring.io/, 创建项目demo(maven + kotlin

SQL快速操作技巧2_datatable批量更新到数据表

(为了给您节省时间,您可以只看高亮部分内容) 要将datatable 批量更新到数据表其实是两步: (1)将datatable快速存入一个表中: (2)根据一个数据表更新另外一个数据表: 下面细说: (1)将datatable快速存入一个表中: 1)   insert循环插入:2)   sqldataadapter.update(dataset,tablename);3)   sqlbulkcopy.WriteToServer(datatable); 1.生成测试的datatable表,表结构如

Spring Boot(六)集成 MyBatis 操作 MySQL 8

一.简介 1.1 MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集. 1.2 MyBatis发展史 MyBatis 原本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis ,2013年11月迁移到Github. ### 1.3 MyBatis和H

php 操作 mysql 实现批量执行mysql语句 mysql文件

1 <?php 2 /** 3 * 批量运行sql文件 4 * 正则分隔是重点 preg_split("/;[\r\n]+/", filecontent) 5 */ 6 $config = require '../dataconfig.php'; 7 $dbhost = $config['DB_HOST']; 8 $dbname = $config['DB_USER']; 9 $dbpass = $config['DB_PWD']; 10 $db_database = $conf

Mybatis 3+Mysql 实现批量插入

终于实现了ibatis的批量插入,此方法插入3000条数据,比单条插入可以节省一半的时间 XML代码: <insert id="insertBatch" parameterType="ArrayList"> insert intouser(id,account,password,active,status,name,gender,active_date,expiry_date,type,remark,group_id,disable,exam_numbe