Mybatis-plus的CRUD方法

2,047 阅读3分钟

一、简介

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

我们可以通过MP提供的两种类对数据库表进行CRUD操作;

  • ServiceImpl类:可以进行save,saveOrUpdate,Remove,Update,get,list,page,count,chain操作;
  • Mapper类:可以进行insert,delete,update,select操作;

二、函数列表

ServiceImpl中实现的方法:
操作函数
save//保存一个实体对象
1.boolean save(T entity);
//批量保存一组实体对象
2.boolean saveBatch(Collection entityList)
//批量保存一组实体对象中的batchSize个3.boolean saveBatch(collection entityList,int batchSize);
saveOrUpdate1.boolean saveOrUpdate(T entity);
2.boolean saveOrUpdate(T entity,Wrapper updateWrapper)
3.boolean saveOrUpdateBatch(Collection entityList);
4.boolean saveOrUpdateBatch(Collection entityList, int batchSize);
注:saveOrUpdate操作根据entity对象中定义"主键"属性,查找数据库中对应的数据记录;
- 如果entity对象中的”主键“,找到对应的数据库记录,则更新;
- 如果entity对象中的”主键“,没有找到对应的数据库记录,则保存该记录;
Remove//根据queryWrapper进行删除;(queryWrapper会在下一节中介绍)
1.boolean remove(Wrapper queryWrapper);
//根据指定id进行删除;
2.boolean removeById(Serializable id);
//根据 columnMap 条件,删除记录
3.boolean removeByMap(Map<String, Object> columnMap);
//根据idList列表中指定的多个Id主键进行删除;4.boolean removeByIds(Collection<? extends Serializable> idList);
注:使用columnMap时,应使用数据库列名,而不是实体类的属性名,例如: {"station_no":"10010","sex":"0"}
Update//根据updateWrapper进行更新;(updateWrapper在下一节中介绍);
1.boolean update(Wrapper updateWrapper);
//根据whereWrapper进行更新;(whereWrapper在下一节中介绍)
2.boolean update(T updateEntity, Wrapper whereWrapper);
//根据主键id进行更新;
3.boolean updateById(T entity);
//根据entityList中实例的”主键属性“,进行更新;
4.boolean updateBatchById(Collection entityList);
//根据entityList中实例的”主键属性“,更新batchSize数量的数据;
5.boolean updateBatchById(Collection entityList, int batchSize);
Get//根据主键id进行查找;
1.T getById(Serializable id);
//根据queryWrapper进行查询
2.T getOne(Wrapper queryWrapper);
//在函数2的基础上,可以选择是否抛出异常;
3.T getOne(Wrapper queryWrapper, boolean throwEx);
//根据queryWrapper进行查询,返回Map中String为属性,Object为值
4.Map<String, Object> getMap(Wrapper queryWrapper);
//根据queryWrapper进行查询,并对查询到的结果执行参数2定义的方法
5. V getObj(Wrapper queryWrapper, Function<? super Object, V> mapper);
注:
在使用getOne()时,如果一条都没有查到,则不会抛出异常,返回null;
getObj(queryWrapper, obj -> "用户Id:" + obj.toString());
List查询所有
1.List list();
根据queryWrapper查询;
2.List list(Wrapper queryWrapper);
.根据id列表查询;
3.Collection listByIds(Collection<? extends Serializable> idList);
根据map中的属性和值进行查处
4.Collection listByMap(Map<String, Object> columnMap);
查询所有,返回map
5.List<Map<String, Object>> listMaps();
根据queryWrapper查询,返回map
6.List<Map<String, Object>> listMaps(Wrapper queryWrapper);
查询所有,返回Object
7.List listObjs();
查询所有,返回第一个属性字段,并支持function方法;
8. List listObjs(Function<? super Object, V> mapper);
根据queryWrapper查询,返回object
9.List listObjs(Wrapper queryWrapper);
根据queryWrapper查询,返回第一个属性字段,并执行function方法;
10. List listObjs(Wrapper queryWrapper, Function<? super Object, V> mapper);
Page1.IPage page(IPage page);
2.IPage page(IPage page, Wrapper queryWrapper);
3.IPage<Map<String, Object>> pageMaps(IPage page);
4.IPage<Map<String, Object>> pageMaps(IPage page, Wrapper queryWrapper);
Countint count();
int count(Wrapper queryWrapper);
Chain1.QueryChainWrapper query();
query().eq("column", value).one();
2.LambdaQueryChainWrapper lambdaQuery();
lambdaQuery().eq(Entity::getId, value).list();
3.UpdateChainWrapper update();
update().eq("column", value).remove();
4.LambdaUpdateChainWrapper lambdaUpdate();
lambdaUpdate().eq(Entity::getId, value).update(entity);
Mapper中定义的方法
操作函数
insertint insert(T entity);
Deleteint delete(@Param(Constants.WRAPPER) Wrapper wrapper);
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
int deleteById(Serializable id);
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
Updateint update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper whereWrapper);
int updateById(@Param(Constants.ENTITY) T entity);
SelectT selectById(Serializable id);
T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);
List selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);
List selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper queryWrapper);
List selectObjs(@Param(Constants.WRAPPER) Wrapper queryWrapper);
IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
IPage<Map<String, Object>> selectMapsPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);