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);
相关推荐
用户805533698033 小时前
主线 U-Boot 上 RK3506:和闭源 rkbin 拔河的三个隐性契约
linux·嵌入式
用户034095297913 小时前
linux fcitx 5 雾凇拼音 设置在中文输入法下仍然输入英文标点
linux
Web3探索者2 天前
可视化服务器管理和传统命令行区别是什么?新手教程:Linux 运维到底该用图形界面还是 SSH 命令行?
linux·ssh
zylyehuo2 天前
Linux系统中网线与USB网络共享冲突
linux
Sokach10153 天前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux
AlfredZhao4 天前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
zzzzzz3105 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode5 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
A小辣椒7 天前
TShark:Wireshark CLI 功能
linux
A小辣椒7 天前
TShark:基础知识
linux