Mybatis plus中的BaseMapper与ServiceImpl

BaseMapper接口方法与ServiceImpl类方法的区别与联系

什么是BaseMapper?什么是ServiceImpl?

BaseMapper 是 MyBatis-Plus 提供的一个基础 Mapper 接口,封装了常用的 CRUD 操作方法,如 selectByIdinsertupdateByIddeleteById 等。

ServiceImpl 是 MyBatis-Plus 提供的一个服务层实现类,它实现了 IService 接口。ServiceImpl 类内部自动注入了 BaseMapper 的动态代理类实例,并通过方法代理机制,将 IService 接口中定义的方法转发到 BaseMapper 的实现类中。

什么时候使用BaseMapper?什么时候使用ServiceImpl
  • 使用 BaseMapper:当你需要直接对数据库进行简单的 CRUD 操作,且不需要复杂的业务逻辑处理时,直接使用 BaseMapper 中的方法。
  • 使用 ServiceImpl:当你需要在业务逻辑层中处理复杂的业务逻辑,如事务管理、数据校验、组合多个数据库操作等时,使用 ServiceImpl 中的方法,并在其中调用 BaseMapper 的方法。
BaseMapper 的实现类在哪里?

在 MyBatis-Plus 中,BaseMapper 是一个接口,它定义了一系列常用的 CRUD 操作方法。这些方法的具体实现是由 MyBatis-Plus 框架在运行时动态生成的。因此,你不会在 MyBatis-Plus 的源码中找到一个具体的 BaseMapper 实现类。

动态代理机制

MyBatis-Plus 使用 MyBatis 的动态代理机制来生成 BaseMapper 的实现类。具体来说,MyBatis 会根据 BaseMapper 接口定义的方法,动态生成一个代理类,并在运行时调用这些方法。这个代理类会自动生成相应的 SQL 语句并执行数据库操作。

关于BaseMapper和ServiceImpl的关系 、动态代理机制还有补充,未完待续…


以下是BaseMapper中的方法和ServiceImpl中的方法对照关系:BaseMapper接口中的方法有一个特征是都以insertupdateselectdelete开头。

  1. save(T entity) ~ insert(entity)
public boolean save(T entity) {
    return this.retBool(this.baseMapper.insert(entity));
}
  1. removeById(Serializable id) ~ deleteById(id)
public boolean removeById(Serializable id) {
    return SqlHelper.retBool(this.baseMapper.deleteById(id));
}
  1. removeByMap(Map<String, Object> columnMap) ~ deleteById(id)
public boolean removeByMap(Map<String, Object> columnMap) {
    Assert.notEmpty(columnMap, "error: columnMap must not be empty", new Object[0]);
    return SqlHelper.retBool(this.baseMapper.deleteByMap(columnMap));
}
  1. remove(Wrapper wrapper) ~ delete(wrapper)
public boolean remove(Wrapper<T> wrapper) {
    return SqlHelper.retBool(this.baseMapper.delete(wrapper));
}
  1. removeBuIds(Collection<? extends Serializable> idList) ~ deleteBatchIds(idList)
public boolean removeByIds(Collection<? extends Serializable> idList) {
    return SqlHelper.retBool(this.baseMapper.deleteBatchIds(idList));
}
  1. updateById(T entity) ~ updateById(entity)
public boolean updateById(T entity) {
    return this.retBool(this.baseMapper.updateById(entity));
}
  1. update<T entity, Wrapper updateWrapper ~ update(entity,updateWrapper)
public boolean update(T entity, Wrapper<T> updateWrapper) {
    return this.retBool(this.baseMapper.update(entity, updateWrapper));
}
  1. getById(Serializable id) ~ selectById(id)
public T getById(Serializable id) {
    return this.baseMapper.selectById(id);
}
  1. listByIds(Collection<? extends Serializable> idList) ~ selectBatchIds(idList)
public Collection<T> listByIds(Collection<? extends Serializable> idList) {
    return this.baseMapper.selectBatchIds(idList);
}
  1. listByMap(Map<String,Object> columnMap) ~ selectByMap(columnMap)
public Collection<T> listByMap(Map<String, Object> columnMap) {
    return this.baseMapper.selectByMap(columnMap);
}
  1. getOne(Wrapper queryWrapper,boolean throwEx) ~ selectList(queryWrapper)
public T getOne(Wrapper<T> queryWrapper, boolean throwEx) {
    return throwEx ? this.baseMapper.selectOne(queryWrapper) : SqlHelper.getObject(this.log, this.baseMapper.selectList(queryWrapper));
}
  1. getMap(Wrapper queryWrapper) ~ selectMaps(queryWrapper)
public Map<String, Object> getMap(Wrapper<T> queryWrapper) {
    return (Map)SqlHelper.getObject(this.log, this.baseMapper.selectMaps(queryWrapper));
}
  1. count(Wrapper queryWrapper) ~ selectCount(queryWrapper)
public int count(Wrapper<T> queryWrapper) {
    return SqlHelper.retCount(this.baseMapper.selectCount(queryWrapper));
}
  1. list(Wrapper queryWrapper) ~ selectList(queryWrapper)
public List<T> list(Wrapper<T> queryWrapper) {
    return this.baseMapper.selectList(queryWrapper);
}
  1. page(IPage page, Wrapper queryWrapper) ~ selectPage(page, queryWrapper)
public IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper) {
    return this.baseMapper.selectPage(page, queryWrapper);
}
  1. listMaps(Wrapper queryWrapper) ~ selectMaps(queryWrapper)
public List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
    return this.baseMapper.selectMaps(queryWrapper);
}
  1. listObjs(Wrapper queryWrapper, Function<? super Object, V> mapper) ~ selectObjs(queryWrapper)…
public <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
    return (List)this.baseMapper.selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
}
  1. pageMaps(IPage page, Wrapper queryWrapper) ~ selectMapsPage(page, queryWrapper)
public IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper) {
    return this.baseMapper.selectMapsPage(page, queryWrapper);
}

以下ServiceImpl中的方法没有使用到BaseMapper中的方法:

saveBatch  saveOrUpdate saveOrUpdateBatch  updateBatchById getObj
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值