Mybatis 实现动态批量修改

简介: Mybatis 实现动态批量修改

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

xdm,祝大家节日快乐!!👨‍💻

今天听《路过人间》演唱会Live限定版,爱上了一句歌词。

说来惭愧,人对爱只学会,视死如归。

1.业务需求

如下:

前台传给我一个 documentIdList<UpdateDocumentAnswer> 对象给我。

执行条件:通过这个documentIdList<UpdateDocumentAnswer>中对UpdateDocumentAnswer.id,修改document_answer表的数据。

简单说:就是希望通过一条update语句,根据不同的条件改变多条需要改变的数据。

1704462426231.jpg

思考一:

我们先按照我们最简单的思维思考:

即拆成一句一句执行,for循环执行多条 update 语句。

update document_answer set where document_id=#{documentId} and id=#{answer.id}

这样写的话,我们的mapper层接收的参数,应该为:

int patchByDocumentId(@Param("documentId") Long documentId,@Param("answers") UpdateDocumentAnswer answers);

实现是可以实现的,但是因为需要执行多条update语句,效率是真的不敢恭维。

如果大家有尝试过,都会知道,for循环执行sql语句是真的要不得的。一条普通的sql,我们都要优化完再优化,更别说一个方法要执行多条sql语句了。

所有就啥勒??

推荐大家使用 百度、Bing、Google进行搜索👨‍💻

我们想到过这种问题,大概率别人也会遇上的,搜一搜,确实有答案低。

所以我们接着进入思考二吧。🏍

思考二:

还记得文章前面所说:就是希望通过一条update语句,根据不同的条件改变多条需要改变的数据。

我们直接 搜怎么一条update用不同条件修改多条数据勒

就是会搜到一个下面的这样的sql语句。

update 表名 set 
列1=
  case
    when 条件1 then 值1
    when 条件2 then 值2 end,
列2=
  case
    when 条件1 then 值1
    when 条件2 then 值2 end,
where 条件

说实话,看到这条语句的那一刻,感觉自己又没有学过mysql了,连crud工程师都算不上(捂脸)。

解释:

我们要 修改列1, 当when 条件1 满足时,则将 列1 修改为 then 后面跟着的 值1,when 条件2 满足,则将列1修改为then 后面跟着的值2。

这样一样,我们就可以执行多条语句了啊。

2.实现

我们将之前的mapper层的接口传入的参数做一下更改。

int patchByDocumentId(@Param("documentId") Long documentId,@Param("answers") List<UpdateDocumentAnswer> answers);

mapper.xml的实现如下:

<update id="patchByDocumentId">
    update document_answer
    <set>
        <trim prefix="template_question_id = case" suffix="end,">
            <foreach collection="answers" item="answer">
                <if test="answer.templateQuestionId != null">
                    when id=#{answer.id} then #{answer.templateQuestionId}
                </if>
            </foreach>
        </trim>
        <trim prefix="answer = case" suffix="end,">
            <foreach collection="answers" item="answer">
                <if test="answer.answer != null">
                    when id=#{answer.id} then #{answer.answer}
                </if>
            </foreach>
        </trim>
        <trim prefix="comments = case" suffix="end,">
            <foreach collection="answers" item="answer">
                <if test="answer.comments != null">
                    when id=#{answer.id} then #{answer.comments}
                </if>
            </foreach>
        </trim>
    </set>
    <where>
        document_id=#{documentId}
        <if test="answers != null">
            and id in
            <foreach collection="answers" separator="," item="answer" open="(" close=")">
                #{answer.id}
            </foreach>
        </if>
    </where>
</update>

生成的sql日志

update document_answer 
SET 
template_question_id = 
  case 
    when id=? then ? 
    when id=? then ? end, 
answer = 
  case 
    when id=? then ? 
    when id=? then ? end, 
comments = 
  case 
    when id=? then ? 
    when id=? then ? end 
WHERE document_id=? and id in ( ? , ? )

换上我们想要的值:

update document_answer 
SET 
template_question_id = 
  case 
    when id=1 then 2 
    when id=1 then 3 end, 
answer = 
  case 
    when id=1 then '内容1' 
    when id=2 then '内容2' end, 
comments = 
  case 
    when id=1 then '评论1' 
    when id=2 then '评论2' end 
WHERE document_id=2 and id in ( 1 , 2 )

执行规则: 上面这种方式,更新的记录的数量取决于list集合的数量,且每条记录中的值和对应记录的ID是一一对应的。

结束了,周日更文一篇。

后语

更惭愧的是,我还没有学会。我们一起加油吧


希望本篇文章能让你感到有所收获!!!

我们:待别日相见时,都已有所成

欢迎大家一起讨论问题😁,躺了🛌

目录
相关文章
|
5月前
|
SQL Java 关系型数据库
Mybatis多表关联查询与动态SQL(下)
Mybatis多表关联查询与动态SQL
114 0
|
5月前
|
SQL Java 数据库连接
Mybatis多表关联查询与动态SQL(上)
Mybatis多表关联查询与动态SQL
133 0
|
3月前
|
SQL Java 数据库连接
mybatis动态SQL常用语法总结
MyBatis 使用 OGNL 表达式语言处理动态SQL,如 `if` 标签进行条件判断,`choose`、`when`、`otherwise` 实现多条件选择,`where`、`set` 管理SQL关键字,`trim` 提供通用修剪功能,`foreach` 遍历集合数据。`sql` 和 `include` 用于代码重用,`selectKey` 处理插入后的返回值。参数传递支持匿名、具名、列表、Map、Java Bean和JSON方式。注意SQL转义及使用合适的jdbcType映射Java类型。
72 7
|
4月前
|
SQL XML Java
MyBatis动态SQL------------------choose用法
MyBatis动态SQL------------------choose用法
48 1
|
4月前
|
SQL XML Java
MyBatis第四课动态SQL
MyBatis第四课动态SQL
|
4月前
|
SQL XML Java
Mybatis进阶——动态SQL(1)
Mybatis进阶——动态SQL(1)
33 3
|
4月前
|
SQL 缓存 Java
Java框架之MyBatis 07-动态SQL-缓存机制-逆向工程-分页插件
Java框架之MyBatis 07-动态SQL-缓存机制-逆向工程-分页插件
|
4月前
|
SQL Java 数据库连接
MyBatis动态SQL
MyBatis动态SQL
47 0
|
4月前
|
JSON 前端开发 数据格式
MyBatis-Plus动态分页查询
MyBatis-Plus动态分页查询
|
4月前
|
SQL Java 数据库连接
【MyBatis】MyBatis操作数据库(二):动态SQL、#{}与${}的区别
【MyBatis】MyBatis操作数据库(二):动态SQL、#{}与${}的区别
45 0