Mybatis优雅的批量update

2 篇文章 0 订阅
1 篇文章 0 订阅
本文通过一个简单的例子展示了如何在Springboot项目中进行批量更新数据库的操作,包括创建实体、编写Controller、Service、Mapper以及XML配置。在执行过程中遇到的问题是MySQL默认不支持多条SQL语句的执行,解决办法是在数据库连接URL中添加allowMultiQueries=true参数。
摘要由CSDN通过智能技术生成

先看hello world

1、本文使用springboot框架测试

2、开始编码

        2.1、创建一个数据库操作实体

import lombok.Data;

@Data
public class TestZiShu {
    private String id;
    private String name;
    private String age;
}

        2.2、测试controller

@RestController
@RequestMapping("testUpdate")
public class TestUpdate {

    @Resource
    TestUpdateService testUpdateService;

    @RequestMapping("update")
    public Integer testUpdate(@RequestBody List<TestZiShu> testZiShuList){
        return testUpdateService.testUpdate(testZiShuList);
    }

}

        2.3、service(结构还是要有的)

@Service
public class TestUpdateService {

    @Resource
    TestYuMapper testYuMapper;

    public Integer testUpdate(List<TestZiShu> testZiShuList){
        return testYuMapper.batchUpdate(testZiShuList);
    }
}

        2.4、mapper

public interface TestYuMapper {
    
    /**
     * 测试批量更新
     * @return int
     */
    int batchUpdate(List<TestZiShu> list);

}

        2.5、mapper.xml--注意入参实体和foreach标签的使用

    <update id="batchUpdate" >
        <foreach collection="list" item="item" separator=";">
            UPDATE testzishu
            SET
                `age` = #{item.age}
            WHERE
                `name` = #{item.name}
        </foreach>
    </update>

        2.6、数据库表

CREATE TABLE `testzishu` (
  `id` varchar(255) NOT NULL,
  `age` varchar(255) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

        2.7、如果你的项目可以启动话,就可以测试了

        入参:

[
    {
        "name": "小黑",
        "age": "22"
    },
    {
        "name": "小红",
        "age": "21"
    }
]

        2.8、执行结果sql

        本条SQL会一次性发送给数据库执行,只需要进行一次网络IO,可以提高效率。

==>  Preparing: UPDATE testzishu SET `age` = ? WHERE `name` = ? ; UPDATE testzishu SET `age` = ? WHERE `name` = ?
==> Parameters: 22(String), 小黑(String), 21(String), 小红(String)
<==    Updates: 1

3、常见问题

       问题1、 默认情况下,数据库是不支持执行这样由‘;’号拼接的长串的,执行的时候会报错,提示说执行的SQL有语法错误,操作结果如下:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  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 testzishu
            SET
                `age` = '21'
            WHERE
' at line 7
### The error may exist in file [C:\Users\server\target\classes\mapper\TestYuMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: UPDATE testzishu             SET                 `age` = ?             WHERE                 `name` = ?          ;              UPDATE testzishu             SET                 `age` = ?             WHERE                 `name` = ?
### 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 testzishu
            SET
                `age` = '21'
            WHERE
' at line 7
; 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 testzishu
            SET
                `age` = '21'
            WHERE
' at line 7] with root 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 testzishu
            SET
                `age` = '21'
            WHERE
' at line 7

        问题1解决方案:

                修改项目配置文件,数据库配置处url中指定allowMultiQueries参数值为true,目的是告诉数据库以支持‘;’号分隔的多条语句的执行!

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/water?allowMultiQueries=true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值