Mybatis plus:IService接口

06-26 1634阅读

一、介绍

        在MybatisPlus框架中,IService接口扮演着重要的角色。作为一个通用的服务接口,IService定义了一系列方法,包括查询、插入、更新、删除等。这些方法的定义使得在服务层进行数据库操作变得更为便捷和高效。

Mybatis plus:IService接口
(图片来源网络,侵删)
  • IService 接口是一个泛型接口,定义了一组通用的基础方法,包括常见的增删改查操作。例如,它提供了插入数据、根据主键更新数据、根据主键删除数据、根据主键查询数据等方法的签名。用户可以根据自己的需求和业务逻辑在自定义的服务接口中继承 IService 接口,并实现其中的方法。用法:
     public interface UserService extends IService {}
    • ServiceImpl 类是 IService 接口的默认实现类,提供了基本的增删改查操作的实现细节。它使用了泛型参数来规范实体类和主键类型,并实现了 IService 接口中定义的方法。用户可以继承 ServiceImpl 类,并在自己的实现类中添加或重写更具体的业务逻辑。用法:        
      @Service
      public class UserServiceImpl extends ServiceImpl implements UserService {}

      二、IService用法

      1、添加数据

      // 插入一条记录(选择字段,策略插入)
      boolean save(T entity);
      // 插入(批量)
      boolean saveBatch(Collection entityList);
      // 插入(批量,限制数量)
      boolean saveBatch(Collection entityList, int batchSize);
      // TableId 注解存在更新记录,否则插入一条记录
      boolean saveOrUpdate(T entity);
      // 根据 updateWrapper 尝试更新,否则继续执行 saveOrUpdate(T) 方法
      boolean saveOrUpdate(T entity, Wrapper updateWrapper);
      // 批量修改插入
      boolean saveOrUpdateBatch(Collection entityList);
      // 批量修改插入
      boolean saveOrUpdateBatch(Collection entityList, int batchSize);
      

              可以开启 rewriteBatchedStatements=true 参数,提高批处理的执行效率。

      2、删除数据

      // 根据 entity 条件,删除记录
      boolean remove(Wrapper queryWrapper);
      // 根据 ID 删除
      boolean removeById(Serializable id);
      // 根据 columnMap 条件,删除记录
      boolean removeByMap(Map columnMap);
      // 删除(根据ID 批量删除)
      boolean removeByIds(Collection
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]