mybatis-plus批量增加、批量修改、批量删除样例+建表语句+postman接口
使用mybatis-plus开发中会遇到数据量多的情况下,插入和修改效率低,主要原因是“新增“和“修改”方法是对一条数据进行处理的,如果有一万条数据就会和数据库交互一万次所以效率就低。如何提高效率就需要批量操作,如下展示批量插入和批量修改的代码,数据库使用mysql。
1、建表语句
1 2 3 4 5 6 | CREATE TABLE yc_test_t ( id int COMMENT '主键ID' , name VARCHAR (40) COMMENT '姓名' , note VARCHAR (100) COMMENT '备注' , PRIMARY KEY (id) ) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COMMENT= '测试' ; |
2、实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package com.example.demo.domain; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; /** * <p> * 测试表。 * </p> * * @author yc * @since 2024-09-03 */ @TableName (value = "yc_test_t" ) public class YcTestT { private static final long serialVersionUID = 1L; /** * ID。 */ @TableId private Integer id; /** * 姓名。 */ private String name; /** * 备注。 */ private String note; public void setId(Integer id) { this .id = id; } public Integer getId() { return id; } public void setName(String name) { this .name = name; } public String getName() { return name; } public void setNote(String note) { this .note = note; } public String getNote() { return note; } } |
3、mapper类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.example.demo.mapper; import com.example.demo.domain.YcTestT; import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** * <p> * 测试表 Mapper 接口。 * </p> * * @author yc * @since 2024-09-03 */ public interface YcTestTMapper extends BaseMapper<YcTestT> { } |
4、接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.example.demo.service; import com.example.demo.domain.YcTestT; import com.baomidou.mybatisplus.extension.service.IService; import java.util.Collection; /** * <p> * 测试表 服务类。 * </p> * * @author yc * @since 2024-09-03 */ public interface IYcTestTService extends IService<YcTestT> { //批量插入 boolean saveBatch(Collection<YcTestT> entityList); /** * 批量更新。 * @param oldNote 旧 * @param newNote 新 * @return status */ boolean updateBatch(String oldNote,String newNote); /** * 单记录新增测试表。 * * @param ycTestT 参数说明 * @return status */ int insert(YcTestT ycTestT); /** * 批量删除。 * @param name * @return null */ void deleteBatch(String name); } |
5、接口实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | package com.example.demo.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.example.demo.domain.YcTestT; import com.example.demo.mapper.YcTestTMapper; import com.example.demo.service.IYcTestTService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import java.util.Collection; /** * <p> * 测试表 服务实现类。 * </p> * * @author yc * @since 2024-09-03 */ @Service public class YcTestTServiceImpl extends ServiceImpl<YcTestTMapper, YcTestT> implements IYcTestTService { @Autowired YcTestTMapper ycTestTMapper; //批量插入 @Override public boolean saveBatch(Collection<YcTestT> entityList) { return super .saveBatch(entityList); } /** * 批量更新。 * @param oldNote 旧 * @param newNote 新 * @return status */ @Override public boolean updateBatch(String oldNote,String newNote) { // 创建 UpdateWrapper 实例 UpdateWrapper<YcTestT> updateWrapper = new UpdateWrapper<>(); // 设置更新条件,例如根据 userId 更新 updateWrapper.lambda().eq(YcTestT::getNote, oldNote); // 设置需要更新的字段值 updateWrapper.set( "note" ,newNote); // 调用 update 方法进行批量更新 return this .update(updateWrapper); } /** * 批量删除。 * @param name * @return null */ @Override public void deleteBatch(String name) { // 创建Wrapper对象 QueryWrapper<YcTestT> queryWrapper = new QueryWrapper<>(); // 添加条件 queryWrapper.eq( "name" , name); // 调用delete方法删除数据 ycTestTMapper.delete(queryWrapper); } /** * 单记录新增测试表。 * * @param ycTestT 参数说明 * @return status */ @Override public int insert(YcTestT ycTestT) { return ycTestTMapper.insert(ycTestT); } } |
6、控制类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.service.IYcTestTService; import com.example.demo.domain.YcTestT; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; /** * <p> * 测试表 前端控制器。 * </p> * * @author yc * @since 2024-09-03 */ @RestController @RequestMapping ( "/test" ) public class YcTestTController { @Autowired private IYcTestTService ycTestTService; /** * 批量插入。 * * @return AjaxResult */ @PostMapping (value = "/saveBatch" ) @ResponseBody public String saveBatch() { List<YcTestT> list = new ArrayList<>(); for ( int i = 0 ; i < 5000 ; i++) { YcTestT ycTestT = new YcTestT(); ycTestT.setId(i); ycTestT.setName( "张三" + (i + "" )); ycTestT.setNote( "旧" ); list.add(ycTestT); if ((i + 1 ) % 3000 == 0 ) { try { ycTestTService.saveBatch(list); } catch (Exception e) { // 批量插入失败,改为单条插入 for ( int j = 0 ; j < list.size(); j++) { try { YcTestT testT = list.get(j); //单条插入 ycTestTService.insert(testT); } catch (Exception ex) { ex.printStackTrace(); } } } finally { list.clear(); } } } //处理除3000余数的数据 if (list.size() >= 1 ) { ycTestTService.saveBatch(list); } return "批量插入成功!" ; } @PostMapping (value = "/updateBatch" ) @ResponseBody public String updateBatch() { ycTestTService.updateBatch( "旧" , "新" ); return "批量更新成功!" ; } @PostMapping (value = "/deleteBatch" ) @ResponseBody public String deleteBatch() { ycTestTService.deleteBatch( "张三0" ); return "批量删除成功!" ; } } |
7、postman接口
批量插入:http://127.0.0.1:8080/test/saveBatch
批量修改:http://127.0.0.1:8080/test/updateBatch
批量删除:http://127.0.0.1:8080/test/deleteBatch
8、效果-后台执行的更新效果是一次批量更新
源码获取方式(免费):
(1)登录-注册:http://resources.kittytiger.cn/
(2)签到获取积分
(3)搜索:mybatis-plus批量增加-修改-删除样例
(4)文件列表
分类:
springboot
, java
标签:
java
, springboot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2021-09-03 二、docker的安装和镜像管理
2021-09-03 一、docker入门(概念)