MyBatis Plus 中常用的 Service 功能

save():插入单条数据

clike 复制代码
service.save(entity);

removeById():根据 ID 删除数据。

clike 复制代码
service.removeById(id);

updateById():根据 ID 更新单条数据。

clike 复制代码
service.updateById(entity);

getById():根据 ID 查询单条数据。

clike 复制代码
service.getById(id);

list():查询所有数据。

clike 复制代码
service.list();

saveBatch():批量插入数据。

clike 复制代码
service.saveBatch(list);

removeBatchByIds():批量根据 ID 删除数据。

clike 复制代码
service.removeBatchByIds(ids);

page():分页查询。需要传入一个 Page 对象

clike 复制代码
Page<User> page = new Page<>(1, 10); // 当前页、每页条数
IPage<User> userPage = service.page(page, null); // 第二个参数是查询条件,可以为 null

QueryWrapper:用于构建查询条件。

clike 复制代码
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("age", 25).like("name", "John");
List<User> users = service.list(queryWrapper);

UpdateWrapper:用于构建更新条件

clike 复制代码
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "John").set("age", 30);
service.update(updateWrapper);

updateBatchById():批量更新

clike 复制代码
service.updateBatchById(list);

saveOrUpdateBatch():批量保存或更新。

clike 复制代码
service.saveOrUpdateBatch(list);

updateBatchById():批量更新。

clike 复制代码
service.updateBatchById(list);
相关推荐
weixin_430750932 分钟前
OpenMediaVault debian Linux安装配置企业私有网盘(三) 静态ip地址配置
linux·服务器·debian·nas·网络存储系统
403240732 分钟前
[Jetson/Ubuntu 22.04] 解决挂载 exFAT 硬盘报错 “unknown filesystem type“ 及只读权限问题的终极指南
linux·运维·ubuntu
Source.Liu7 分钟前
【沟通协作软件】使用 Rufus 制作 Ubuntu 启动盘的详细过程
linux·ubuntu
Love丶伊卡洛斯8 分钟前
Ubuntu 部署 STUN服务端
linux·运维·ubuntu
龙之叶24 分钟前
【Android Monkey源码解析四】- 异常捕获/页面控制
android·windows·adb·monkey
梁洪飞26 分钟前
通过链接文件和Start.S学习armv7
linux·arm开发·嵌入式硬件·学习·arm
DN金猿37 分钟前
使用ubuntu安装nginx时报错
linux·nginx·ubuntu
小赵还有头发1 小时前
安装Ceres与glog
linux·学习·无人机·ceres·glog
负二代0.01 小时前
Linux下的网络管理
linux·网络
优选资源分享1 小时前
Advanced Renamer v4.20 便携版 批量文件重命名工具
windows·实用工具