首页 > 其他分享 >Mybatis批量操作

Mybatis批量操作

时间:2022-10-11 17:08:54浏览次数:60  
标签:NAME 批量 demo WHEN USER DEMO Mybatis 操作 ID


foreach标签

  • foreach元素的属性主要有 item,index,collection,open,separator,close
  • item表示集合中每一个元素进行迭代时的别名
  • index指定一个名字,用于表示在迭代过程中,每次迭代到的位置
  • open表示该语句以什么开始
  • separator表示在每次进行迭代之间以什么符号作为分隔 符
  • close表示以什么结束

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:

  • 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,collection的属性值为map中对应的key值
  • 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
  • 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

批量插入

DAO层方法:

/**
*
* @Title: batchSaveDemo
* @Description: 批量保存
* @param parameterList
* @throws
void batchSaveDemoList(List<Map<String, Object>> parameterList) throws

Mapper.xml中的sql:

<!-- 批量保存 -->
<insert id="batchSaveDemoList" parameterType="list">
INSERT INTO DEMO
VALUES
<foreach collection="list" item="demo" index="index" separator=",">
(
#{demo.ID},
#{demo.USER_NAME},
#{demo.AGE},
#{demo.BIRTH_DAY},
#{demo.SEX}
)
</foreach>
</insert>

执行的sql:

INSERT INTO DEMO VALUES

批量更新

第一种写法

DAO层方法:

/**
*
* @Title: batchUpdateDemoList
* @Description: 批量保存
* @param parameterList
* @throws
void batchUpdateDemoList(List<Map<String, Object>> parameterList) throws

Mapper.xml中的sql:

<!-- 批量更新 -->
<update id="batchUpdateDemoList" parameterType="list">
UPDATE DEMO
SET USER_NAME =
<foreach collection="list" item="demo" index="index" open="CASE ID" close="END">
WHEN #{demo.ID} THEN #{demo.USER_NAME}
</foreach>
WHERE ID IN
<foreach collection="list" item="demo" index="index" separator="," open="(" close=")">
#{demo.ID}
</foreach>
</update>

执行的sql:

UPDATE DEMO 
SET USER_NAME =
CASE ID
WHEN ? THEN ?
WHEN ? THEN ?
WHEN ? THEN ?
WHEN ? THEN ?
END
WHERE
ID IN

第二种写法

trim标签

  • prefix:前缀覆盖并增加其内容
  • suffix:后缀覆盖并增加其内容
  • prefixOverrides:前缀判断的条件
  • suffixOverrides:后缀判断的条件
  • prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容
  • 如果同时有prefixOverrides,suffixOverrides 表示会用prefix,suffix覆盖Overrides中的内容
  • 如果只有prefixOverrides,suffixOverrides 表示删除开头的或结尾的xxxOverides指定的内容

xml中的sql

<!-- 批量更新 -->
<update id="batchUpdateDemoList" parameterType="list">
UPDATE DEMO
<trim prefix="SET" suffixOverrides=",">
<trim prefix="USER_NAME = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
WHEN ID = #{demo.ID} THEN #{demo.USER_NAME}
</foreach>
</trim>
</trim>
WHERE ID IN
<foreach collection="list" item="demo" index="index" separator="," open="(" close=")">
#{demo.ID}
</foreach>
</update>

生成的sql:

UPDATE DEMO 
SET USER_NAME =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
END
WHERE
ID IN

同时修改多个字段

<!-- 批量更新 -->
<update id="batchUpdateDemoList" parameterType="list">
UPDATE DEMO
<trim prefix="SET" suffixOverrides=",">
<trim prefix="USER_NAME = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
WHEN ID = #{demo.ID} THEN #{demo.USER_NAME}
</foreach>
</trim>
<trim prefix="AGE = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
WHEN ID = #{demo.ID} THEN #{demo.AGE}
</foreach>
</trim>
</trim>
WHERE ID IN
<foreach collection="list" item="demo" index="index" separator="," open="(" close=")">
#{demo.ID}
</foreach>
</update>

生成的sql:

UPDATE DEMO 
SET
USER_NAME =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
END,
AGE =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
END
WHERE
ID IN

提供默认值

<!-- 批量更新 -->
<update id="batchUpdateDemoList" parameterType="list">
UPDATE DEMO
<trim prefix="SET" suffixOverrides=",">
<trim prefix="USER_NAME = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
WHEN ID = #{demo.ID} THEN #{demo.USER_NAME}
</foreach>
ELSE '默认值'
</trim>
<trim prefix="AGE = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
WHEN ID = #{demo.ID} THEN #{demo.AGE}
</foreach>
ELSE 0
</trim>
</trim>
WHERE ID IN
<foreach collection="list" item="demo" index="index" separator="," open="(" close=")">
#{demo.ID}
</foreach>
</update>

生成sql:

UPDATE DEMO 
SET
USER_NAME =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE '默认值'
END,
AGE =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE 0
END
WHERE
ID IN

对更新的数据进行判断

这样的话只有要更新的list中USER_NAME != null and USER_NAME != ”的数据才能进行USER_NAME 更新.其他的将使用默认值更新,而不会保持原数据不变.

<!-- 批量更新 -->
<update id="batchUpdateDemoList" parameterType="list">
UPDATE DEMO
<trim prefix="SET" suffixOverrides=",">
<trim prefix="USER_NAME = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
<if test="demo.USER_NAME != null and demo.USER_NAME != ''">
WHEN ID = #{demo.ID} THEN #{demo.USER_NAME}
</if>
</foreach>
ELSE '默认值'
</trim>
<trim prefix="AGE = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
WHEN ID = #{demo.ID} THEN #{demo.AGE}
</foreach>
ELSE 0
</trim>
</trim>
WHERE ID IN
<foreach collection="list" item="demo" index="index" separator="," open="(" close=")">
#{demo.ID}
</foreach>
</update>

USER_NAME满足条件时生成的sql

UPDATE DEMO 
SET
USER_NAME =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE '默认值'
END,
AGE =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE 0
END
WHERE
ID IN

USER_NAME不满足条件时生成的sql

UPDATE DEMO 
SET
USER_NAME =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE '默认值'
END,
AGE =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE 0
END
WHERE
ID IN

只对满足条件的数据进行更新

<!-- 批量更新 -->
<update id="batchUpdateDemoList" parameterType="list">
UPDATE DEMO
<trim prefix="SET" suffixOverrides=",">
<trim prefix="USER_NAME = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
<if test="demo.USER_NAME != null and demo.USER_NAME != ''">
WHEN ID = #{demo.ID} THEN #{demo.USER_NAME}
</if>
<if test="demo.USER_NAME == null or demo.USER_NAME == ''">
WHEN ID = #{demo.ID} THEN DEMO.USER_NAME<!-- 原数据 -->
</if>
</foreach>
ELSE '默认值'
</trim>
<trim prefix="AGE = CASE" suffix="END,">
<foreach collection="list" item="demo" index="index">
WHEN ID = #{demo.ID} THEN #{demo.AGE}
</foreach>
ELSE 0
</trim>
</trim>
WHERE ID IN
<foreach collection="list" item="demo" index="index" separator="," open="(" close=")">
#{demo.ID}
</foreach>
</update>

USER_NAME满足条件时生成的sql

UPDATE DEMO 
SET
USER_NAME =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE '默认值'
END,
AGE =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE 0
END
WHERE
ID IN

USER_NAME不满足条件时生成的sql

UPDATE DEMO 
SET
USER_NAME =
CASE
WHEN ID = ? THEN DEMO.USER_NAME
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE '默认值'
END,
AGE =
CASE
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
WHEN ID = ? THEN ?
ELSE 0
END
WHERE
ID IN

批量删除

第一种写法:数组

/**
*
* @Title: batchDeleteDemoList
* @Description: 批量删除
* @param idsArray
* @throws
void batchDeleteDemoList(String[] idsArray) throws

parameterType为数组的类型,而不是array

<!-- 批量删除 -->
<delete id="batchDeleteDemoList" parameterType="string">
DELETE FROM DEMO
WHERE ID IN
<foreach collection="array" item="item" index="index" separator="," open="(" close=")">
#{item}
</foreach>
</delete>

生成的sql

DELETE FROM DEMO WHERE ID IN

第二种写法:List

/**
*
* @Title: batchDeleteDemoList
* @Description: 批量删除
* @param idsList
* @throws
void batchDeleteDemoList(List<String> idsList) throws
<!-- 批量删除 -->
<delete id="batchDeleteDemoList" parameterType="list">
DELETE FROM DEMO
WHERE ID IN
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
#{item}
</foreach>
</delete>

生成sql

DELETE FROM DEMO WHERE ID IN

第三种写法:Map

/**
*
* @Title: batchDeleteDemoList
* @Description: 批量删除
* @param map
* @throws
void batchDeleteDemoList(Map<String, Object> map) throws

map.put(“IDS”, idsArray);
idsArray是一个字符串数组

<!-- 批量删除 -->
<delete id="batchDeleteDemoList" parameterType="map">
DELETE FROM DEMO
WHERE ID IN
<foreach collection="IDS" item="item" index="index" separator="," open="(" close=")">
#{item}
</foreach>
</delete>

生成sql

DELETE FROM DEMO WHERE ID IN


标签:NAME,批量,demo,WHEN,USER,DEMO,Mybatis,操作,ID
From: https://blog.51cto.com/u_15824687/5747138

相关文章

  • Mybatis通用Mapper(springboot环境下使用)
    ​​mapper3通用接口大全​​​:​​https://gitee.com/free/Mapper.git​​1、添加pom文件依赖<!--mapper--><dependency><groupId>tk.mybatis</groupId><artifact......
  • Java集合操作
    1、集合转数组集合转数组建议采用以下写法://接口方法<T>T[]toArray(T[]a);List<String>strList=newArrayList<>();strList.add("str1");strList.add("str2");strList......
  • Oracle-19C 相关操作命令整理
    ----运行oracle19c镜像dockerrun--nameoracle19c-p1521:1521-p5500:5500\-eORACLE_SID=orcl\-eORACLE_PDB=orclpdb1\-eORACLE_PWD=sysadmin01\-eORACLE_......
  • 简记操作系统中逻辑地址到物理地址的转换
    这里稍微整理一下操作系统中进程逻辑地址到实际物理地址中的转换,可惜开始没注意到用的笔太细了,当然,本身写字也比较随意。#include<iostream>usingnamespacestd;#def......
  • 操作系统:Centos、Ubuntu和Kali等 Linux系统下载【三】
    ​​https://developer.aliyun.com/mirror/​​......
  • jpa整合mybatis模板解析、hibernate整合mybatis模板解析
    jpa整合mybatis模板解析、hibernate整合mybatis模板解析jpa是hibernate的封装,主要用于spring全家桶套餐。hibernate难以编写复杂的SQL。例如一个订单查询,查询条件有时间......
  • 如何批量打印PDF文件?
    有不少大学生或老师在下载一些电子版的论文或资料后,发现这些电子版资料都是PDF格式的。PDF格式文件的优点很多,例如稳定性强,安全性高,兼容跨平台打开,在不同的操作系统、电子......
  • Deepin操作系统虚拟机玩3D
    1.下载VMwarePlayer下载地址:[VMwareWorkstation16.2.4PlayerforLinux64-bit](https://customerconnect.vmware.com/en/downloads/details?downloadGroup=WKST-PLA......
  • NETCORE中如何操作Appsettings.json 文件
    对于很多初学NETCORE的同学来说,怎么从appsettings.json文件中获取各种类型数据,一直没搞明白。今天我们就对它的几种数据格式的读取做个说明。appsettings.json 是我们......
  • 510约束_外键约束和511和约束_外键约束的级联操作
    外键约束外键约束:foreignkey,让表与表产生关系,从而保证数据的正确性1.在创建表时,可以添加外键语法:CREATETABLE表名(....(值)外键列CONSTRAINT外键名称,FOREIGNKEY(外......