From 15025337512123e1d049fc558d0cec229deede29 Mon Sep 17 00:00:00 2001 From: Hccake Date: Wed, 6 Mar 2024 21:51:15 +0800 Subject: [PATCH] =?UTF-8?q?:fire:=20(mybatis-plus)=20=E7=A7=BB=E9=99=A4=20?= =?UTF-8?q?BaseService=20=E7=9A=84=20getBaseMapper()=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=20mapper=20=E9=80=B8=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisplus/service/BaseService.java | 59 ++--------- .../service/impl/BaseServiceImpl.java | 97 ++++++++++++++----- 2 files changed, 83 insertions(+), 73 deletions(-) diff --git a/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/service/BaseService.java b/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/service/BaseService.java index 4ef93e187..f20531c1e 100644 --- a/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/service/BaseService.java +++ b/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/service/BaseService.java @@ -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; /** @@ -33,8 +30,6 @@ */ public interface BaseService { - // ======= Copy From com.baomidou.mybatisplus.extension.service.IService 开始 ======= - /** * 默认批次提交数量 */ @@ -44,9 +39,7 @@ public interface BaseService { * 插入一条记录(选择字段,策略插入) * @param entity 实体对象 */ - default boolean save(T entity) { - return SqlHelper.retBool(getBaseMapper().insert(entity)); - } + boolean save(T entity); /** * 插入(批量) @@ -84,9 +77,7 @@ default boolean saveOrUpdateBatch(Collection entityList) { * 根据 ID 删除 * @param id 主键ID */ - default boolean removeById(Serializable id) { - return SqlHelper.retBool(getBaseMapper().deleteById(id)); - } + boolean removeById(Serializable id); /** * 根据 ID 删除 @@ -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); /** * 批量删除 @@ -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批量提交) @@ -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 批量更新 @@ -219,31 +192,19 @@ default boolean updateBatchById(Collection entityList) { * 根据 ID 查询 * @param id 主键ID */ - default T getById(Serializable id) { - return getBaseMapper().selectById(id); - } + T getById(Serializable id); /** * 查询(根据ID 批量查询) * @param idList 主键ID列表 */ - default List listByIds(Collection idList) { - return getBaseMapper().selectBatchIds(idList); - } + List listByIds(Collection idList); /** * 查询所有 * */ - default List list() { - return getBaseMapper().selectList(null); - } - - /** - * 获取对应 entity 的 BaseMapper - * @return BaseMapper - */ - BaseMapper getBaseMapper(); + List list(); /** * 获取 entity 的 class @@ -251,6 +212,4 @@ default List list() { */ Class getEntityClass(); - // ^^^^^^ Copy From com.baomidou.mybatisplus.extension.service.IService end ^^^^^^ - } diff --git a/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/service/impl/BaseServiceImpl.java b/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/service/impl/BaseServiceImpl.java index 4652f1179..54a9b4d37 100644 --- a/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/service/impl/BaseServiceImpl.java +++ b/mybatis-plus/ballcat-mybatis-plus/src/main/java/org/ballcat/mybatisplus/service/impl/BaseServiceImpl.java @@ -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; @@ -48,19 +49,12 @@ @SuppressWarnings("unchecked") public class BaseServiceImpl, T> implements BaseService { - // == 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 entityClass = currentModelClass(); @Override @@ -70,17 +64,6 @@ public Class getEntityClass() { protected Class mapperClass = currentMapperClass(); - /** - * 判断数据库操作是否成功 - * @param result 数据库操作返回影响条数 - * @return boolean - * @deprecated 3.3.1 - */ - @Deprecated - protected boolean retBool(Integer result) { - return SqlHelper.retBool(result); - } - protected Class currentMapperClass() { return (Class) ReflectionKit.getSuperClassGenericType(this.getClass(), ExtendServiceImpl.class, 0); } @@ -89,6 +72,15 @@ protected Class currentModelClass() { return (Class) 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 @@ -162,6 +154,15 @@ public boolean updateBatchById(Collection entityList, int batchSize) { }); } + /** + * 根据 ID 选择修改 + * @param entity 实体对象 + */ + @Override + public boolean updateById(T entity) { + return SqlHelper.retBool(this.baseMapper.updateById(entity)); + } + /** * 执行批量操作 * @param list 数据集合 @@ -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 @@ -194,7 +195,7 @@ 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 @@ -202,13 +203,42 @@ 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 @@ -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 listByIds(Collection idList) { + return this.baseMapper.selectBatchIds(idList); + } + + /** + * 查询所有 + */ + public List list() { + return this.baseMapper.selectList(null); + } }