1、问题概述?
本文主要讲述mybatis中通过forEache批量删除数据
批量操作并没有固定的办法,理论上只要能实现一次性操作多条数据,都叫批量操作,常见的批量操作可以采用如下功能办法:
1、使用编程语言中的for循环实现批量删除或添加等。
2、使用一些批处理框架如Spring Batch
3、使用一些ORM框架提供的批处理工具。
2、forEach批量操作使用
2.1、获取前端选中的数据集
前端数据封装成如下格式:既是数组类型的json字符串。
function test(){
$.get("/test",{"ids":"[{\"id\":\"10002\"},{\"id\":\"10003\"}]"});}
2.2、导入数组json解析包
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.53</version>
</dependency>
2.3、后端接收数据并解析成List<String>集合。
import com.alibaba.fastjson2.JSONArray;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
@Controller
public class TestController {
@RequestMapping("/test")
@ResponseBody
public void test(String ids) throws Exception{
JSONArray listObjectThir = JSONArray.parseArray(ids);
for(Object mapList : listObjectThir){
for (Object entry : ((Map)mapList).entrySet()){
System.out.println(((Map.Entry)entry).getKey() + "<=键==值=>" +((Map.Entry)entry).getValue());
}
}
}
}
2.4、创建StudentService业务类
@Service
public class StudentService{
@AutoWired(required=false)
private StudentMapper studentMapper;
@AutoWired
public List<Student> findByName(List<String> ids){
Map<String,Object> map=new HashMap<>();
map.put("ids",ids);
studentMapper.findByName(map);
}
}
2.5、创建mapper接口
public interface StudentMapper{
public List<Student> findByName(Map<String,Object> map);
}