MyBatis Plus(简称MP)是一个MyBatis的增强工具,旨在简化开发、提高效率。它提供了许多便捷的功能,其中之一就是IService
接口。IService
接口是MyBatis Plus提供的一个服务层接口,封装了常用的CRUD操作,使得开发者可以更加便捷地进行数据库操作。本文将详细讲解IService
接口,包括其基本概念、常用方法以及实际应用场景。
1. 基本概念
IService
接口是MyBatis Plus提供的一个服务层接口,定义了一系列常用的CRUD操作方法。通过实现IService
接口,可以快速构建服务层,简化开发流程。
1.1 接口定义
IService
接口定义如下:
public interface IService<T> {
// 插入一条记录(选择字段,策略插入)
boolean save(T entity);
// 插入(批量)
boolean saveBatch(Collection<T> entityList);
// 插入(批量)
boolean saveBatch(Collection<T> entityList, int batchSize);
// 根据 ID 删除
boolean removeById(Serializable id);
// 根据实体(ID)删除
boolean removeById(T entity);
// 根据 columnMap 条件,删除记录
boolean removeByMap(Map<String, Object> columnMap);
// 根据 entity 条件,删除记录
boolean remove(Wrapper<T> queryWrapper);
// 删除(根据ID 批量删除)
boolean removeByIds(Collection<? extends Serializable> idList);
// 根据 ID 选择修改
boolean updateById(T entity);
// 根据 whereEntity 条件,更新记录
boolean update(T entity, Wrapper<T> updateWrapper);
// 根据 ID 查询
T getById(Serializable id);
// 查询(根据ID 批量查询)
List<T> listByIds(Collection<? extends Serializable> idList);
// 查询(根据 columnMap 条件)
List<T> listByMap(Map<String, Object> columnMap);
// 根据 entity 条件,查询一条记录
T getOne(Wrapper<T> queryWrapper);
// 根据 Wrapper 条件,查询全部记录
List<T> list(Wrapper<T> queryWrapper);
// 查询所有
List<T> list();
// 查询总记录数
int count();