MybatisPlus的Service层IService中提供的CRUD常用方法

大类作用
save插入
saveorupdate有则改无则插入
remove删除
update修改
get返回一条记录
list返回多条记录
page用于分页查询
count返回记录总数
query查询
lambda内部支持lambda写法

Service

通用 Service CRUD 封装IService (opens new window)接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆
Service CRUD接口官方文档
在controller层随便调用一个IService的方法就可以进入源码查看方法
在这里插入图片描述

Save

// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量),指定批次数量
boolean saveBatch(Collection<T> entityList, int batchSize);

参数说明

类型参数名描述
Tentity实体对象
CollectionentityList实体对象集合
intbatchSize插入批次数量(默认1000)

案例

Device device = new Device();
device.setWorkshops("车间4");
device.setDeviceType("生产设备");
device.setDeviceNumber("1001");
device.setDeviceName("砖头");
device.setDeviceModel("ST001");
device.setFactory("工厂");
iDeviceService.save(device);
List<Device> list = new ArrayList<>();
for (Long i = 1l; i < 5; i++) {
    Device device = new Device();
    device.setWorkshops("车间"+i);
    device.setDeviceType("生产设备");
    device.setDeviceNumber("1001111"+i);
    device.setDeviceName("砖头"+i);
    device.setDeviceModel("ST0000"+i);
    device.setFactory("工厂");
    list.add(device);
}
iDeviceService.saveBatch(list);

SaveOrUpdate

// TableId 注解存在更新记录,否插入一条记录
boolean saveOrUpdate(T entity);
// 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// 批量修改插入,先按实体查询,如果查询到记录则修改,否则新增
boolean saveOrUpdateBatch(Collection<T> entityList);
// 批量修改插入
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

参数说明

类型参数名描述
WrapperupdateWrapper实体对象封装操作类 UpdateWrapper
Tentity实体对象
CollectionentityList实体对象集合
intbatchSize插入批次数量(默认1000)

案例

List<Device> list = new ArrayList<>();
Device device = new Device();
device.setWorkshops("车间5");
device.setDeviceType("生产设备");
device.setDeviceNumber("10011115");
device.setDeviceName("砖头");
device.setDeviceModel("ST00005");
device.setFactory("工厂");
list.add(device);
iDeviceService.saveOrUpdate(device);
List<Device> list = new ArrayList<>();
Device device1 = new Device();
device1.setWorkshops("车间1");
device1.setDeviceType("生产设备");
device1.setDeviceNumber("10010111");
device1.setDeviceName("砖头");
device1.setDeviceModel("ST00101");
device1.setFactory("工厂");
list.add(device1);
Device device2 = new Device();
device2.setWorkshops("车间1");
device2.setDeviceType("生产设备");
device2.setDeviceNumber("10010131");
device2.setDeviceName("砖头");
device2.setDeviceModel("ST00102");
device2.setFactory("工厂");
list.add(device2);
iDeviceService.saveOrUpdateBatch(list);

Remove

// 根据 queryWrapper 设置的条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);

参数说明

类型参数名描述
WrapperqueryWrapper实体包装类 QueryWrapper
Serializableid主键 ID
Map<String, Object>columnMap表字段 map 对象
Collection<? extends Serializable>idList主键 ID 列表

Collection<? extends Serializable>:一种包含未知类型的Collection,必须是Serializable或其子类

案例

UpdateWrapper<Device> duw = new UpdateWrapper<>();
Map<String, Object> map = new HashMap<>();
map.put("deviceName","砖头");
duw.allEq(map);
iDeviceService.remove(duw);

Update

// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
boolean update(Wrapper<T> updateWrapper);
// 根据 whereWrapper 条件,更新记录
boolean update(T updateEntity, Wrapper<T> whereWrapper);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList);
// 根据ID 批量更新
boolean updateBatchById(Collection<T> entityList, int batchSize);

参数说明

类型参数名描述
WrapperqueryWrapper实体包装类 QueryWrapper
Tentity实体对象
CollectionentityList实体对象集合
intbatchSize插入批次数量(默认1000)

Get

用于返回一条记录

// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录,自定义有多个结果是否需要抛异常,true抛出异常。false返回结果集中第一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录用map保存
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

参数说明

类型参数名描述
WrapperqueryWrapper实体对象封装操作类 QueryWrapper
Tentity实体对象
Serializableid主键 ID
booleanthrowEx有多个 result 是否抛出异常
Function<? super Object, V>mapper转换函数

List

用于返回多条记录

// 查询所有
List<T> list();
// 根据queryWrapper条件查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
Collection<T> listByMap(Map<String, Object> columnMap);
// 查询所有列表返回List<Map>集合
List<Map<String, Object>> listMaps();
// 根据操作对象传递的条件查询列表返回List<Map>集合
List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);
// 查询全部记录但只返回第一个字段的值
List<Object> listObjs();
// 查询全部记录
<V> List<V> listObjs(Function<? super Object, V> mapper);
// 根据 Wrapper 条件,查询全部记录
List<Object> listObjs(Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录
<V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

参数说明

类型参数名描述
WrapperqueryWrapper实体对象封装操作类 QueryWrapper
Collection<? extends Serializable>idList主键 ID 列表
Function<? super Object, V>mapper转换函数
Map<String, Object>columnMap表字段 map 对象

Page

用于分页查询

// 无条件分页查询
IPage<T> page(IPage<T> page);
// 条件分页查询
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
// 无条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page);
// 条件分页查询
IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);

封装的数据类型不一样,但是都返回page对象

参数说明

类型参数名描述
WrapperqueryWrapper实体对象封装操作类 QueryWrapper
IPagepage翻页对象

Count

用于返回记录总数

// 查询总记录数
int count();
// 根据 Wrapper 条件,查询总记录数
int count(Wrapper<T> queryWrapper);

参数说明

类型参数名描述
WrapperqueryWrapper实体对象封装操作类 QueryWrapper

Chain链式方法,先条件拼接再调用特定的改查方法

query

表示查询

// 链式查询 普通
QueryChainWrapper<T> query();
// 链式查询 lambda 式。注意:不支持 Kotlin
LambdaQueryChainWrapper<T> lambdaQuery();

// 示例:
query().eq("column", value).one();
lambdaQuery().eq(Entity::getId, value).list();

update

表示修改

// 链式更改 普通
UpdateChainWrapper<T> update();
// 链式更改 lambda 式。注意:不支持 Kotlin
LambdaUpdateChainWrapper<T> lambdaUpdate();

// 示例:
update().eq("column", value).remove();
lambdaUpdate().eq(Entity::getId, value).update(entity);
  • 20
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今年不养猪只除草

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值