首页 > 其他分享 >Mybatis 实现多条件、多值批量更新

Mybatis 实现多条件、多值批量更新

时间:2023-03-08 13:46:51浏览次数:32  
标签:批量 Title WHEN when 多值 Mybatis New id

UPDATE course
    SET name = CASE id 
        WHEN 1 THEN 'name1'
        WHEN 2 THEN 'name2'
        WHEN 3 THEN 'name3'
    END, 
    title = CASE id 
        WHEN 1 THEN 'New Title 1'
        WHEN 2 THEN 'New Title 2'
        WHEN 3 THEN 'New Title 3'
    END
WHERE id IN (1,2,3)

在Mybatis中的配置则如下:

<update id="updateBatch" parameterType="list">
            update course
            <trim prefix="set" suffixOverrides=",">
             <trim prefix="peopleId =case" suffix="end,">
                 <foreach collection="list" item="i" index="index">
                         <if test="i.peopleId!=null">
                          when id=#{i.id} then #{i.peopleId}
                         </if>
                 </foreach>
              </trim>
              <trim prefix=" roadgridid =case" suffix="end,">
                 <foreach collection="list" item="i" index="index">
                         <if test="i.roadgridid!=null">
                          when id=#{i.id} then #{i.roadgridid}
                         </if>
                 </foreach>
              </trim>
              
              <trim prefix="type =case" suffix="end," >
                 <foreach collection="list" item="i" index="index">
                         <if test="i.type!=null">
                          when id=#{i.id} then #{i.type}
                         </if>
                 </foreach>
              </trim>
       <trim prefix="unitsid =case" suffix="end," >
                  <foreach collection="list" item="i" index="index">
                          <if test="i.unitsid!=null">
                           when id=#{i.id} then #{i.unitsid}
                          </if>
                  </foreach>
           </trim>
             </trim>
            where
            <foreach collection="list" separator="or" item="i" index="index" >
              id=#{i.id}
          </foreach>
</update>

 

标签:批量,Title,WHEN,when,多值,Mybatis,New,id
From: https://www.cnblogs.com/xiaofengfree/p/17191734.html

相关文章

  • Mybatis关联映射
    1.一对一stu拥有一个stu_card1.1student实体类将外键代表的实体放在属性publicclassStudent{privateIntegersid;privateStringsname;priv......
  • mybatis异常 java.lang.ClassCastException: com.sun.proxy.$Proxy445
    此类异常处理:java.lang.ClassCastException:com.sun.proxy.$Proxy445cannotbecasttocom.xxx.**MapperBridgeApplication启动类的注解没有扫描到对应的Mapper文......
  • Spring Boot + MybatisX = 王炸!!
    1.什么是MybatisX?MybatisX是一款基于IDEA的快速开发插件,方便在使用mybatis以及mybatis-plus开始时简化繁琐的重复操作,提高开发速率。2.使用MybatisX的好处节省大量......
  • mybatis 源码简单探究
    一、初始化环境中文网:https://mybatis.net.cn前提maven环境jdk1.8mysql8.0pom.xml<dependencies><dependency><groupId>mysql</grou......
  • MybatisPlus的逻辑删除
    在这里简述一下什么是逻辑删除,对于我们数据库表的设计,其中许多表会有一个status字段(就是标记当条数据是否显示在前端,也有叫做deleteflag),当他标记为0或者1即为删除,此仅仅表......
  • 手牵手入门Spring6整合Mybatis3.5
    方式一Object类get和set,无参构造+有参构造Pom.xml引入依赖<!--打包方式jar--><packaging>jar</packaging><!--配置多个仓库--><repositories><!--Spring6-->......
  • 批量下载浏览器网页中全部链接的方法
    批量下载浏览器网页中全部链接的方法   本文介绍在Chrome浏览器中,通过迅雷自动批量选中网页中全部下载链接并进行下载的方法。  前期的文章批量下载Landsat遥感......
  • 【Mybatis】数据库动态查询
    在工作目录中新建文件夹idea中file→new→project→Maven      pom.xml中dependencies之内的是依靠<?xmlversion="1.0"encoding="UTF-8"?><projectxm......
  • 跟老杜从零入门MyBatis到架构思维(二)MyBatis入门程序
    MyBatis入门程序只要你会JDBC,MyBatis就可以学。2.1版本软件版本:●IntelliJIDEA:2022.1.4●NavicatforMySQL:16.0.14●MySQL数据库:8.0.30组件版本:●MySQL驱......
  • 帝国CMS:数据字段存为了文本格式,如何进行批量替换?
    帝国CMS数据库中,形如以下内容,表示newstext内容保存到了txt文本,这样做的好处是相对于大量文字内容的,可以快速的读取渲染。但是弊端是不方便批量进行数据修改。  对于这......