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);
相关推荐
业余幻想家21 小时前
Windows10/Windows11家庭版系统关闭自动更新
运维·windows
ANGLAL1 天前
17.MyBatis动态SQL语法整理
java·sql·mybatis
南林yan1 天前
Debian系统的多内核共存
linux·debian·linux内核
阿猿收手吧!1 天前
windows本机vscode通过ssh免密登录远程linux服务器 && git push/pull 免密
服务器·windows·vscode
zxm85131 天前
如何在Windows系统中加入程序自启动
windows
skywalk81631 天前
尝试Auto-coder.chat使用星河社区AIStudio部署的几个大模型:文心4.5-21b、Deepseek r1 70b、llama 3.1 8b
linux·服务器·人工智能·大模型·aistudio
~~李木子~~1 天前
Windows软件自动扫描与分类工具 - 技术文档
windows·分类·数据挖掘
QiTinna1 天前
系统运维Day02_数据同步服务
linux·同步·rsync
阿猿收手吧!1 天前
【Linux网络】shutdown()与close()的区别
linux·网络
LCG元1 天前
Linux 磁盘管理从入门到精通:LVM 扩容实战案例
linux