首页 > 其他分享 >MybatisPlus(六) IService层CRUD相关接口使用

MybatisPlus(六) IService层CRUD相关接口使用

时间:2023-03-11 20:23:57浏览次数:36  
标签:queryWrapper IService MybatisPlus CRUD Collection 查询 boolean Wrapper ID

Save(添加)

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

参数说明:

类型 参数名 描述
T entity 实体对象
Collection<T> entityList 实体对象集合
int batchSize 插入批次数量

 

 

 

 

 

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);

参数说明:

类型 参数名 描述
T entity 实体对象
Wrapper<T> updateWrapper 实体对象封装操作类 UpdateWrapper
Collection<T> entityList 实体对象集合
int batchSize 插入批次数量

 

 

 

 

 

 

Remove(删除)

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

参数说明:

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

 

 

 

 

 

 

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);

参数说明:

类型 参数名 描述
Wrapper<T> updateWrapper 实体对象封装操作类 UpdateWrapper
T entity 实体对象
Collection<T> entityList 实体对象集合
int batchSize 更新批次数量

 

 

 

 

 

 

Get(查询)

// 根据 ID 查询
T getById(Serializable id);
// 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
// 根据 Wrapper,查询一条记录
Map<String, Object> getMap(Wrapper<T> queryWrapper);
// 根据 Wrapper,查询一条记录
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);

参数说明:

类型 参数名 描述
Serializable id 主键 ID
Wrapper<T> queryWrapper 实体对象封装操作类 QueryWrapper
boolean throwEx 有多个 result 是否抛出异常
T entity 实体对象
Function<? super Object, V> mapper 转换函数

 

 

 

 

 

 

 

List(查询列表)

// 查询所有
List<T> list();
// 查询列表
List<T> list(Wrapper<T> queryWrapper);
// 查询(根据ID 批量查询)
Collection<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
Collection<T> listByMap(Map<String, Object> columnMap);
// 查询所有列表
List<Map<String, Object>> listMaps();
// 查询列表
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);

参数说明:

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

 

 

 

 

 

 

Count(查询总记录数量)

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

参数说明:

类型 参数名 描述
Wrapper<T> queryWrapper 实体对象封装操作类 QueryWrapper

 

标签:queryWrapper,IService,MybatisPlus,CRUD,Collection,查询,boolean,Wrapper,ID
From: https://www.cnblogs.com/Life-QX/p/17206696.html

相关文章

  • MybatisPlus(五) 条件构造器
    wapper介绍:用于条件封装,生成sql的where条件|--Wrapper:条件构造抽象类,最顶端父类|--AbstractWrapper:用于查询条件封装,生成sql的where条件......
  • MybatisPlus查询时过滤不需要的字段~
    解释一下:乍一看标题可能有点懵~,其实就是想查询的时候过滤掉某些字段例如:selectname,email,passwordfromuser;--改为->selectname,emailfromuser;去掉password这......
  • MybatisPlus(四) BaseMapper层CRUD相关接口使用
    BaseMapper接口API:Insert(添加):/***插入一条记录**@paramentity实体对象*/intinsert(Tentity);参数说明:类型参数名描述......
  • 跟老杜从零入门MyBatis到架构思维(四)使用MyBatis完成CRUD- 下
    使用MyBatis完成CRUD配合视频教程观看,更易学习理解,课程讲解从Mybatis的一些核心要点与实战中的运用,一直过渡到MyBaits源码,由表及里的代入架构思维。一步一案例,一码一实操。......
  • MybatisPlus的逻辑删除
    在这里简述一下什么是逻辑删除,对于我们数据库表的设计,其中许多表会有一个status字段(就是标记当条数据是否显示在前端,也有叫做deleteflag),当他标记为0或者1即为删除,此仅仅表......
  • MybatisPlus多表连接查询
    一、(一)背景内容软件应用技术架构中DAO层最常见的选型组件为MyBatis,熟悉MyBatis的朋友都清楚,曾几何时MyBatis是多么的风光,使用XML文件解决了复杂的数据库访问的难题。时至......
  • SpringBoot+MybatisPlus+MySql 自动生成代码 自动分页
    SpringBoot+MybatisPlus+MySql自动生成代码自动分页一、配置<!--Mybatisplus--><dependency><groupId>com.baomidou</groupId>......
  • 九、MybatisPlus的多数据源
    场景适用于多种场景:纯粹多库、读写分离、一主多从、混合模式等目前我们就来模拟一个纯粹多库的一个场景,其他场景类似场景说明:我们创建两个库,分别为:mybatis_plus(以前......
  • 八、MybatisPlus的代码生成器示例
    引入依赖<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version></dependency><dependency> <......
  • 六、MybatisPlus实现乐观锁
    乐观锁场景一件商品,成本价是80元,售价是100元。老板先是通知小李,说你去把商品价格增加50元。小李正在玩游戏,耽搁了一个小时。正好一个小时后,老板觉得商品价格增加到150元,......