使用MyBatis-Plus 批量更新实现步骤如下:
1. 创建 Service, 实现 ServiceImpl
@Service
public class EmpService extends ServiceImpl<EmpMapper, Emp> {
}
ServiceImpl 是MyBatis-Plus 提供的,
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
2. 调用
调用时直接 empService.updateBatchById 即可
package com.hejjon.controller;
import com.hejjon.entity.Emp;
import com.hejjon.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* Created by caoshi at 21:47 2022-03-08
*/
@RestController
@RequestMapping("demo2")
public class DemoController2 {
@Autowired
private EmpService empService;
/**
* 批量更新
* @return
*/
@RequestMapping("/testBatchUpdate")
public String testBatchUpdate() {
List<Emp> empDoList = new ArrayList<>();
Emp emp1 = new Emp();
emp1.setEmpno(7369);
emp1.setEname("测试1");
Emp emp2 = new Emp();
emp2.setEmpno(7499);
emp2.setEname("测试2");
empDoList.add(emp1);
empDoList.add(emp2);
empService.updateBatchById(empDoList);
return "success";
}
}
|