前言
工作中,经常会遇到很多批量操作的需求:批量添加、批量更新、批量删除、批量导入、批量审核等等,下面这篇文章我们将一一复现,首先我们先了解一下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.1Mapper.xml
<!--批量插入员工数据--><insertid="saveEmp"parameterType="java.util.List">INSERTINTOt_employee(id,name,age,salary,department_id,update_time)VALUES<foreachcollection="list"item="item"index="index"separator=",">(#{item.id},#{item.name},#{item.age},#{item.salary},#{item.departmentId},now())</foreach></insert>
2.2Controller层
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}
@ResponseBody:返回Json数据@RequestBody:接受Json数据
2.3Json数组集合数据
[{"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批量更新第一种方法
<updateid="updateBatch"parameterType="java.util.List"><foreachcollection="list"item="item"index="index"separator=";">UPDATEt_employee<set><iftest="item.name!=nullanditem.name!=''">name=#{item.name},</if><iftest="item.age!=null">age=#{item.age},</if><iftest="item.salary!=null">salary=#{item.salary},</if><iftest="item.salary!=null">salary=#{item.departmentId},</if></set>whereid=#{item.id}</foreach></update>
记得连接数据库加:
allowMultiQueries=true
不然会报如下错误:
YouhaveanerrorinyourSQLsyntax;checkthemanu althatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'UPDATEt_employee
MySQL连接数据库时,添加语句:“allowMultiQueries=true”的作用:
1.可以在sql语句后携带分号,实现多语句执行。2.可以执行批处理,同时发出多个SQL语句。
这种方式就是通过SQL拼接,单条单条地进行更新。
1.2批量更新第二种方法
<updateid="updateBatch"parameterType="java.util.List">updatet_employee<trimprefix="set"suffixOverrides=","><trimprefix="name=case"suffix="end,"><foreachcollection="list"item="i"index="index"><iftest="i.name!=nullandi.name!=''">whenid=#{i.id}then#{i.name}</if></foreach></trim><trimprefix="age=case"suffix="end,"><foreachcollection="list"item="i"index="index"><iftest="i.age!=null">whenid=#{i.id}then#{i.age}</if></foreach></trim></trim>where<foreachcollection="list"separator="or"item="i"index="index">id=#{i.id}</foreach></update>
实际上是通过casewhen语句进行批量更新,只要一条SQL语句:
当然除了上面两种方式外,还可通过如下:
批量更新第三种方法,用ONDUPLICATEKEYUPDATE,就是一个批量插入操作,在插入的时候,如果已存在,则更新,所以可以变相达到批量修改的效果。
一般不推荐这种更新大数据量的SQL,关于这种方式小编前面的文章也有说过使用方式,这里不再赘述。
注意:上面的方式是针对多个字段的情况,如果只是更新单个字段,可以这么写:
<!--批量更新第二种方法,针对单个字段进行批量更新--><updateid="updateBatch"parameterType="java.util.List">UPDATEt_employeeSETname=CASE<foreachcollection="list"item="item"index="index">WHENid=#{item.id}THEN#{item.name}</foreach>ENDWHEREidIN<foreachcollection="list"index="index"item="item"open="("separator=","close=")">#{item.id}</foreach></update>
2.Controller层
@PostMapping("updateBatch")@ResponseBodypublicCommonResult<Employee>updateBatch(@RequestBodyList<Employee>employeeList){returnemployeeService.updateBatch(employeeList);}
3.Json集合数据
[{"id":1,"name":"DT测试111","age":2611},{"id":2,"name":"DT测试211","age":2611}]
四、批量删除
1.传入的是List数组对象
1.Mapper.xml
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}0
2.Controller层
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}1
3.Json集合数据
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}2
2.传入的是数组
1.Mapper.xml
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}3
2.Controller层
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}4
3.Json数组
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}5
2.传入的是Map集合
1.Mapper.xml
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}6
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}7
2.Controller层
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}8
3.map数据
{"ids":@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}5}
五、批量查询
1.Mapper.xml
[{"id":1,"name":"DT测试1","age":26,"salary":10000.0,"departmentId":1},{"id":2,"name":"DT测试2","age":26,"salary":10000.0,"departmentId":2}]0
2.Controller层
[{"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
3.Json集合数据
@PostMapping("saveBath")@ResponseBodypublicCommonResult<Employee>saveBath(@RequestBodyList<Employee>employeeList){returnemployeeService.saveEmp(employeeList);}2
至于其它的数据格式就不再赘述了,很简单,变一下数据格式就可以了: