mybatis通过foreach实现批量操作

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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雾林小妖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值