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);
相关推荐
若云止水1 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - cycle->modules[i]->ctx
linux·nginx·ubuntu
亦世凡华、1 小时前
快速部署:在虚拟机上安装 CentOS 7 的详细步骤
linux·运维·经验分享·centos·安装教程
Elastic 中国社区官方博客2 小时前
使用 Elastic-Agent 或 Beats 将 Journald 中的 syslog 和 auth 日志导入 Elastic Stack
大数据·linux·服务器·elasticsearch·搜索引擎·信息可视化·debian
星图辛某人2 小时前
《Linux命令行和shell脚本编程大全》第四章阅读笔记
linux·运维·笔记
gallonyin2 小时前
免root运行python保活守护进程supervisor
linux·开发语言·python
爱宇阳3 小时前
如何在 Windows 10 启用卓越性能模式及不同电源计划对比
windows·卓越模式·电池选项
Charary3 小时前
字符设备驱动开发与杂项开发
linux·驱动开发
孤寂大仙v4 小时前
【Linux笔记】理解文件系统(上)
linux·运维·笔记
Tomorrow'sThinker4 小时前
Python零基础学习第三天:函数与数据结构
开发语言·windows·python