MyBatis批量操作
- 前言
- 一、MybatIs標簽foreach
- 1.collection屬性主要有一下3種情況:
- 二、批量添加
- 2.1 Mapper.xml
- 2.2 Controller層
- 2.3 Json陣列集合資料
- 三、批量更新
- 1.Mapper.xml
- 1.1 批量更新第一種方法
- 1.2 批量更新第二種方法
- 2.Controller層
- 3.Json集合資料
- 四、批量洗掉
- 1. 傳入的是List陣列物件
- 1.Mapper.xml
- 2.Controller層
- 3.Json集合資料
- 2. 傳入的是陣列
- 1.Mapper.xml
- 2.Controller層
- 3.Json陣列
- 2. 傳入的是Map集合
- 1.Mapper.xml
- 2.Controller層
- 3.map資料
- 五、批量查詢
- 1.Mapper.xml
- 2.Controller層
- 3.Json集合資料
- 總結
前言
作業中,經常會遇到很多批量操作的需求:批量添加、批量更新、批量洗掉、批量匯入、批量審核等等,下面這篇文章我們將一一復現,首先我們先了解一下mybatis的標簽foreach回圈:
一、MybatIs標簽foreach
foreach的主要用在構建in條件中,它可以在SQL陳述句中進行迭代一個集合,
foreach元素的屬性主要有 item,index,collection,open,separator,close,
1》item集合中每一個元素進行迭代時的別名
2》index表示在迭代程序中,每次迭代到的位置
3》open該陳述句以什么開始
4》separator在每次進行迭代之間以什么符號作為分隔符
5》close以什么結束
1.collection屬性主要有一下3種情況:
1.1 如果傳入的是單引數且引數型別是一個List的時候,collection屬性值為list
1.2 如果傳入的是單引數且引數型別是一個array陣列的時候,collection的屬性值為array
1.3 如果傳入的引數是多個的時候,我們就需要把它們封裝成一個Map了
二、批量添加
當傳入引數是list集合的時候:
2.1 Mapper.xml
<!--批量插入員工資料-->
<insert id="saveEmp" parameterType="java.util.List">
INSERT INTO t_employee(id, name, age, salary, department_id,update_time)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id},#{item.name},#{item.age},#{item.salary},#{item.departmentId},now())
</foreach>
</insert>
2.2 Controller層
@PostMapping("saveBath")
@ResponseBody
public CommonResult<Employee> saveBath(@RequestBody List<Employee> employeeList){
return employeeService.saveEmp(employeeList);
}
@ResponseBody:回傳Json資料 @RequestBody:接受Json資料
2.3 Json陣列集合資料
[
{
"id": 1,
"name": "DT測驗1",
"age": 26,
"salary": 10000.0,
"departmentId": 1
},
{
"id": 2,
"name": "DT測驗2",
"age": 26,
"salary": 10000.0,
"departmentId": 2
}
]
三、批量更新
1.Mapper.xml
1.1 批量更新第一種方法
<update id="updateBatch" parameterType="java.util.List" >
<foreach collection="list" item="item" index="index" separator=";">
UPDATE t_employee
<set>
<if test="item.name != null and item.name != ''" >
name = #{item.name},
</if>
<if test="item.age != null" >
age = #{item.age},
</if>
<if test="item.salary != null" >
salary = #{item.salary},
</if>
<if test="item.salary != null" >
salary = #{item.departmentId},
</if>
</set>
where id = #{item.id}
</foreach>
</update>
記得連接資料庫加:
allowMultiQueries=true
不然會報如下錯誤:
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 t_employee
MySQL連接資料庫時,添加陳述句:“allowMultiQueries=true”的作用:
1.可以在sql陳述句后攜帶分號,實作多陳述句執行,
2.可以執行批處理,同時發出多個SQL陳述句,
這種方式就是通過SQL拼接,單條單條的進行更新,
1.2 批量更新第二種方法
<update id="updateBatch" parameterType="java.util.List" >
update t_employee
<trim prefix="set" suffixOverrides=",">
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.name != null and i.name != ''">
when id=#{i.id} then #{i.name}
</if>
</foreach>
</trim>
<trim prefix="age=case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.age != null">
when id=#{i.id} then #{i.age}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id = #{i.id}
</foreach>
</update>
實際上是通過case when陳述句進行批量更新,只要一條SQL陳述句:

當然除了上面兩種方式外,還可通過如下:
批量更新第三種方法,用ON DUPLICATE KEY UPDATE,就是一個批量插入操作,在插入的時候,如果已存在,則更新,所以可以變相達到批量修改的效果,
一般不推薦這種更新大資料量的SQL,關于這種方式小編前面的文章也有說過使用方式,這里不再贅述,
注意:上面的方式是針對多個欄位的情況,如果只是更新單個欄位,可以這么寫:
<!-- 批量更新第二種方法,針對單個欄位進行批量更新 -->
<update id="updateBatch" parameterType="java.util.List">
UPDATE t_employee
SET name = CASE
<foreach collection="list" item="item" index="index">
WHEN id = #{item.id} THEN #{item.name}
</foreach>
END
WHERE id IN
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item.id}
</foreach>
</update>
2.Controller層
@PostMapping("updateBatch")
@ResponseBody
public CommonResult<Employee> updateBatch(@RequestBody List<Employee> employeeList){
return employeeService.updateBatch(employeeList);
}
3.Json集合資料
[
{
"id": 1,
"name": "DT測驗111",
"age": 2611
},
{
"id": 2,
"name": "DT測驗211",
"age": 2611
}
]
四、批量洗掉
1. 傳入的是List陣列物件
1.Mapper.xml
<delete id="deleteBath" parameterType="java.util.List">
DELETE FROM t_employee WHERE id IN
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item.id}
</foreach>
</delete>
2.Controller層
@PostMapping("deleteBath")
@ResponseBody
public CommonResult<Employee> deleteBath(@RequestBody List<Employee> employeeList){
return employeeService.deleteBath(employeeList);
}
3.Json集合資料
[
{
"id": 1
},
{
"id": 2
}
]
2. 傳入的是陣列
1.Mapper.xml
<delete id="deleteBath" parameterType="java.util.Arrays">
DELETE FROM t_employee WHERE id IN
<foreach collection="array" item="ids" open="(" separator="," close=")">
#{ids}
</foreach>
</delete>
2.Controller層
@PostMapping("deleteBath")
@ResponseBody
public CommonResult<Employee> deleteBath(@RequestBody int[] ids){
return employeeService.deleteBath(ids);
}
3.Json陣列
[1,2]
2. 傳入的是Map集合
1.Mapper.xml
<delete id="deleteBath" parameterType="java.util.Map">
DELETE FROM t_employee WHERE id IN
<foreach collection="ids" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
int deleteBath(@Param("ids") Map<String, Integer> ids);
2.Controller層
@PostMapping("deleteBath")
@ResponseBody
public CommonResult<Employee> deleteBath(@RequestBody Map<String,Object> map){
// 接收List
List<Integer> ids = (List<Integer>) map.get("ids");
Map<String, Integer> stringMap = new HashMap<>();
ids.forEach(id -> stringMap.put("ids", id));
return employeeService.deleteBath(stringMap);
}
3.map資料
{
"ids": [1,2]
}
五、批量查詢
1.Mapper.xml
<select id="findBath" resultType="com.dt.springbootdemo.entity.Employee" parameterType="com.dt.springbootdemo.entity.Employee">
SELECT * FROM t_employee WHERE id IN
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item.id}
</foreach>
</select>
2.Controller層
@GetMapping("findBath")
@ResponseBody
public CommonResult<List<Employee>> findBath(@RequestBody List<Employee> employeeList){
return employeeService.findBath(employeeList);
}
3.Json集合資料
[
{
"id": 1
},
{
"id": 2
}
]
至于其它的資料格式就不再贅述了,很簡單,變一下資料格式就可以了:
總結
本篇完結!熬夜干貨,創作不易,動動小手點贊吧!!!!后面會繼續輸出更多干貨給大家,喜歡的請關注小編CSDN:https://blog.csdn.net/qq_41107231 以及掘金:https://juejin.cn/user/3940246036699848
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/287898.html
標籤:其他
上一篇:環境大資料MapReduce
下一篇:RDD編程初級實踐
