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);
相关推荐
「QT(C++)开发工程师」1 分钟前
免费在线 Ubuntu/Linux 运行环境
linux·运维·ubuntu
hhhh明6 分钟前
ubuntu22.04 桌面可视化(vncserver+novnc 方式)
linux·运维·服务器
Fcy6486 分钟前
Linux下 进程间通信详解(一)管道、进程池与简单的Linux 进程间聊天室
linux·服务器·管道·进程间通信·进程池
‎ദ്ദിᵔ.˛.ᵔ₎7 分钟前
Linux 权限
linux
拳里剑气8 分钟前
Linux:权限
linux·学习方法
ole ' ola15 分钟前
Linux DDR内存使用情况
linux·运维·服务器
糖果店的幽灵17 分钟前
LangChain 1.3 完全教程:从入门到精通-Part 10: Memory(记忆系统)
windows·microsoft·langchain
Kingairy24 分钟前
Linux 机器信任关系
linux·运维·服务器
流浪0011 小时前
Linux系统篇(一):从零入门操作系统:冯诺依曼体系到进程的完整理解
linux·运维·服务器
tealcwu1 小时前
【Unity实战】Unity IAP 5.3 中实现 Windows Custom Store 实战教程
windows·unity·游戏引擎