Skip to content

Commit

Permalink
🔥 (mybatis-plus) 移除 BaseService 的 getBaseMapper() 方法,防止 mapper 逸出
Browse files Browse the repository at this point in the history
  • Loading branch information
Hccake committed Mar 6, 2024
1 parent e39f88f commit 1502533
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import java.util.Collection;
import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.transaction.annotation.Transactional;

/**
Expand All @@ -33,8 +30,6 @@
*/
public interface BaseService<T> {

// ======= Copy From com.baomidou.mybatisplus.extension.service.IService 开始 =======

/**
* 默认批次提交数量
*/
Expand All @@ -44,9 +39,7 @@ public interface BaseService<T> {
* 插入一条记录(选择字段,策略插入)
* @param entity 实体对象
*/
default boolean save(T entity) {
return SqlHelper.retBool(getBaseMapper().insert(entity));
}
boolean save(T entity);

/**
* 插入(批量)
Expand Down Expand Up @@ -84,9 +77,7 @@ default boolean saveOrUpdateBatch(Collection<T> entityList) {
* 根据 ID 删除
* @param id 主键ID
*/
default boolean removeById(Serializable id) {
return SqlHelper.retBool(getBaseMapper().deleteById(id));
}
boolean removeById(Serializable id);

/**
* 根据 ID 删除
Expand All @@ -105,21 +96,14 @@ default boolean removeById(Serializable id, boolean useFill) {
* @since 3.4.4
* @return 删除结果
*/
default boolean removeById(T entity) {
return SqlHelper.retBool(getBaseMapper().deleteById(entity));
}
boolean removeById(T entity);

/**
* 删除(根据ID 批量删除)
* @param list 主键ID或实体列表
* @return 删除结果
*/
default boolean removeByIds(Collection<?> list) {
if (CollectionUtils.isEmpty(list)) {
return false;
}
return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
}
boolean removeByIds(Collection<?> list);

/**
* 批量删除
Expand All @@ -128,16 +112,7 @@ default boolean removeByIds(Collection<?> list) {
* @return 删除结果
* @since 3.5.0
*/
@Transactional(rollbackFor = Exception.class)
default boolean removeByIds(Collection<?> list, boolean useFill) {
if (CollectionUtils.isEmpty(list)) {
return false;
}
if (useFill) {
return removeBatchByIds(list, true);
}
return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
}
boolean removeByIds(Collection<?> list, boolean useFill);

/**
* 批量删除(jdbc批量提交)
Expand Down Expand Up @@ -189,9 +164,7 @@ default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useF
* 根据 ID 选择修改
* @param entity 实体对象
*/
default boolean updateById(T entity) {
return SqlHelper.retBool(getBaseMapper().updateById(entity));
}
boolean updateById(T entity);

/**
* 根据ID 批量更新
Expand Down Expand Up @@ -219,38 +192,24 @@ default boolean updateBatchById(Collection<T> entityList) {
* 根据 ID 查询
* @param id 主键ID
*/
default T getById(Serializable id) {
return getBaseMapper().selectById(id);
}
T getById(Serializable id);

/**
* 查询(根据ID 批量查询)
* @param idList 主键ID列表
*/
default List<T> listByIds(Collection<? extends Serializable> idList) {
return getBaseMapper().selectBatchIds(idList);
}
List<T> listByIds(Collection<? extends Serializable> idList);

/**
* 查询所有
*
*/
default List<T> list() {
return getBaseMapper().selectList(null);
}

/**
* 获取对应 entity 的 BaseMapper
* @return BaseMapper
*/
BaseMapper<T> getBaseMapper();
List<T> list();

/**
* 获取 entity 的 class
* @return {@link Class<T>}
*/
Class<T> getEntityClass();

// ^^^^^^ Copy From com.baomidou.mybatisplus.extension.service.IService end ^^^^^^

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -48,19 +49,12 @@
@SuppressWarnings("unchecked")
public class BaseServiceImpl<M extends BaseMapper<T>, T> implements BaseService<T> {

// == Copy From com.baomidou.mybatisplus.extension.service.impl.ServiceImpl 开始 ===

protected Log log = LogFactory.getLog(getClass());

@Autowired
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
protected M baseMapper;

@Override
public M getBaseMapper() {
return this.baseMapper;
}

protected Class<T> entityClass = currentModelClass();

@Override
Expand All @@ -70,17 +64,6 @@ public Class<T> getEntityClass() {

protected Class<M> mapperClass = currentMapperClass();

/**
* 判断数据库操作是否成功
* @param result 数据库操作返回影响条数
* @return boolean
* @deprecated 3.3.1
*/
@Deprecated
protected boolean retBool(Integer result) {
return SqlHelper.retBool(result);
}

protected Class<M> currentMapperClass() {
return (Class<M>) ReflectionKit.getSuperClassGenericType(this.getClass(), ExtendServiceImpl.class, 0);
}
Expand All @@ -89,6 +72,15 @@ protected Class<T> currentModelClass() {
return (Class<T>) ReflectionKit.getSuperClassGenericType(this.getClass(), ExtendServiceImpl.class, 1);
}

/**
* 插入一条记录(选择字段,策略插入)
* @param entity 实体对象
*/
@Override
public boolean save(T entity) {
return SqlHelper.retBool(this.baseMapper.insert(entity));
}

/**
* 批量插入
* @param entityList ignore
Expand Down Expand Up @@ -162,6 +154,15 @@ public boolean updateBatchById(Collection<T> entityList, int batchSize) {
});
}

/**
* 根据 ID 选择修改
* @param entity 实体对象
*/
@Override
public boolean updateById(T entity) {
return SqlHelper.retBool(this.baseMapper.updateById(entity));
}

/**
* 执行批量操作
* @param list 数据集合
Expand All @@ -181,7 +182,7 @@ public boolean removeById(Serializable id) {
if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
return removeById(id, true);
}
return SqlHelper.retBool(getBaseMapper().deleteById(id));
return SqlHelper.retBool(this.baseMapper.deleteById(id));
}

@Override
Expand All @@ -194,21 +195,50 @@ public boolean removeByIds(Collection<?> list) {
if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
return removeBatchByIds(list, true);
}
return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
return SqlHelper.retBool(this.baseMapper.deleteBatchIds(list));
}

@Override
public boolean removeById(Serializable id, boolean useFill) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
if (useFill && tableInfo.isWithLogicDelete()) {
if (this.entityClass.isAssignableFrom(id.getClass())) {
return SqlHelper.retBool(getBaseMapper().deleteById(id));
return SqlHelper.retBool(this.baseMapper.deleteById(id));
}
T instance = tableInfo.newInstance();
tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), id);
return removeById(instance);
}
return SqlHelper.retBool(getBaseMapper().deleteById(id));
return SqlHelper.retBool(this.baseMapper.deleteById(id));
}

/**
* 根据实体(ID)删除
* @param entity 实体
* @return 删除结果
* @since 3.4.4
*/
public boolean removeById(T entity) {
return SqlHelper.retBool(this.baseMapper.deleteById(entity));
}

/**
* 批量删除
* @param list 主键ID或实体列表
* @param useFill 是否填充(为true的情况,会将入参转换实体进行delete删除)
* @return 删除结果
* @since 3.5.0
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean removeByIds(Collection<?> list, boolean useFill) {
if (CollectionUtils.isEmpty(list)) {
return false;
}
if (useFill) {
return removeBatchByIds(list, true);
}
return SqlHelper.retBool(this.baseMapper.deleteBatchIds(list));
}

@Override
Expand Down Expand Up @@ -240,6 +270,27 @@ public boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFi
});
}

// ^^^ Copy From com.baomidou.mybatisplus.extension.service.impl.ServiceImpl end ^^^
/**
* 根据 ID 查询
* @param id 主键ID
*/
public T getById(Serializable id) {
return this.baseMapper.selectById(id);
}

/**
* 查询(根据ID 批量查询)
* @param idList 主键ID列表
*/
public List<T> listByIds(Collection<? extends Serializable> idList) {
return this.baseMapper.selectBatchIds(idList);
}

/**
* 查询所有
*/
public List<T> list() {
return this.baseMapper.selectList(null);
}

}

0 comments on commit 1502533

Please sign in to comment.