From 64312418f05bce13d40c31b5ec86cbe5e48da2cb Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Sun, 22 Oct 2023 14:19:10 +0800 Subject: [PATCH] Add more shortcut method for BaseService --- .../mybatis/base/BaseService.java | 58 +++++++++++-- .../mybatis/base/BaseServiceImpl.java | 87 +++++++++++++++++-- 2 files changed, 132 insertions(+), 13 deletions(-) diff --git a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java index 17f85c0..d62ae86 100644 --- a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java +++ b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java @@ -22,6 +22,25 @@ public interface BaseService extends IService { */ T getById(Long id, Supplier notFoundSupplier); + /** + * 根据 id 获取记录 + */ + V getById(Long id, SFunction column); + + /** + * 根据 id 获取记录 + * + * @param throwIfNotFound 未获取到记录时是否抛异常 + */ + V getById(Long id, SFunction column, boolean throwIfNotFound); + + /** + * 根据 id 获取记录 + * + * @param notFoundSupplier 未获取到记录时的异常处理 + */ + V getById(Long id, SFunction column, Supplier notFoundSupplier); + /** * 根据 id 获取 */ @@ -30,7 +49,7 @@ public interface BaseService extends IService { /** * 根据 id 获取 */ - Map getMapByIds(Collection ids, SFunction mapper); + Map getMapByIds(Collection ids, SFunction column); /** * 根据业务主键获取记录 @@ -40,22 +59,51 @@ public interface BaseService extends IService { /** * 根据业务主键获取记录 * - * @param throwIfNotFound 未获取到记录时是否抛异常 + * @param throwsIfNotFound 未获取到记录时是否抛异常 */ - T getByBizId(Object bizId, boolean throwIfNotFound); + T getByBizId(Object bizId, boolean throwsIfNotFound); /** * 根据业务主键获取记录 * - * @param notFoundSupplier 未获取到记录时是否抛异常 + * @param notFoundSupplier 未获取到记录时的异常处理 */ T getByBizId(Object bizId, Supplier notFoundSupplier); + /** + * 根据业务主键获取记录 + */ + V getByBizId(Object bizId, SFunction column); + + /** + * 根据业务主键获取记录 + * + * @param throwsIfNotFound 未获取到记录时是否抛异常 + */ + V getByBizId(Object bizId, SFunction column, boolean throwsIfNotFound); + + /** + * 根据业务主键获取记录 + * + * @param notFoundSupplier 未获取到记录时的异常处理 + */ + V getByBizId(Object bizId, SFunction column, Supplier notFoundSupplier); + + /** + * 根据业务主键获取 + */ + List listByIds(Collection ids, SFunction column); + /** * 根据业务主键获取 */ List listByBizIds(Collection bizIds); + /** + * 根据业务主键获取 + */ + List listByBizIds(Collection bizIds, SFunction column); + /** * 根据业务主键获取 */ @@ -64,7 +112,7 @@ public interface BaseService extends IService { /** * 根据业务主键获取 */ - Map getMapByBizIds(Collection bizIds, SFunction mapper); + Map getMapByBizIds(Collection bizIds, SFunction column); /** * 根据 id 更新,null 会被更新为 null diff --git a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java index 753ef34..402f805 100644 --- a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java +++ b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java @@ -58,9 +58,32 @@ public class BaseServiceImpl, T extends BaseEntity> exte return entity; } + @Override + public V getById(Long id, SFunction column) { + return getById(id, column, false); + } + + @Override + public V getById(Long id, SFunction column, boolean throwIfNotFound) { + Supplier notFoundSupplier = null; + if (throwIfNotFound) { + notFoundSupplier = () -> new NoSuchRecordException(entityClass); + } + return getById(id, column, notFoundSupplier); + } + + @Override + public V getById(Long id, SFunction column, Supplier notFoundSupplier) { + T entity = lambdaQuery().select(column).eq(T::getId, id).one(); + if (entity == null && notFoundSupplier != null) { + throw notFoundSupplier.get(); + } + return entity == null ? null : column.apply(entity); + } + @Override public Map getMapByIds(Collection ids) { - // Cannot call the override method here, because override method use mapper to judge the fields to select + // Cannot call the override method here, because override method use column to judge the fields to select if (isEmpty(ids)) { return emptyMap(); } @@ -68,13 +91,13 @@ public class BaseServiceImpl, T extends BaseEntity> exte } @Override - public Map getMapByIds(Collection ids, SFunction mapper) { + public Map getMapByIds(Collection ids, SFunction column) { if (isEmpty(ids)) { return emptyMap(); } // noinspection unchecked - List dataList = lambdaQuery().select(T::getId, mapper).in(T::getId, ids).list(); - return StreamUtils.toMap(dataList, T::getId, mapper); + List dataList = lambdaQuery().select(T::getId, column).in(T::getId, ids).list(); + return StreamUtils.toMap(dataList, T::getId, column); } @Override @@ -100,17 +123,65 @@ public class BaseServiceImpl, T extends BaseEntity> exte return entity; } + @Override + public V getByBizId(Object bizId, SFunction column) { + return getByBizId(bizId, column, false); + } + + @Override + public V getByBizId(Object bizId, SFunction column, boolean throwIfNotFound) { + Supplier notFoundSupplier = null; + if (throwIfNotFound) { + notFoundSupplier = () -> new NoSuchRecordException(entityClass); + } + return getByBizId(bizId, column, notFoundSupplier); + } + + @Override + public V getByBizId(Object bizId, SFunction column, Supplier notFoundSupplier) { + T entity = query() + .select(LambdaUtils.getColumnName(column)) + .eq(getBidColumnField().getColumnName(), bizId) + .one(); + if (entity == null && notFoundSupplier != null) { + throw notFoundSupplier.get(); + } + return entity == null ? null : column.apply(entity); + } + + @Override + public List listByIds(Collection ids, SFunction column) { + if (isEmpty(ids)) { + return emptyList(); + } + List dataList = lambdaQuery().select(column).in(T::getId, ids).list(); + return StreamUtils.toList(dataList, column); + } + @Override public List listByBizIds(Collection bizIds) { + // Cannot call the override method here, because override method use column to judge the fields to select if (isEmpty(bizIds)) { return emptyList(); } return query().in(getBidColumnField().getColumnName(), bizIds).list(); } + @Override + public List listByBizIds(Collection bizIds, SFunction column) { + if (isEmpty(bizIds)) { + return emptyList(); + } + List dataList = query() + .select(LambdaUtils.getColumnName(column)) + .in(getBidColumnField().getColumnName(), bizIds) + .list(); + return StreamUtils.toList(dataList, column); + } + @Override public Map getMapByBizIds(Collection bizIds) { - // Cannot call the override method here, because override method use mapper to judge the fields to select + // Cannot call the override method here, because override method use column to judge the fields to select if (isEmpty(bizIds)) { return emptyMap(); } @@ -118,16 +189,16 @@ public class BaseServiceImpl, T extends BaseEntity> exte } @Override - public Map getMapByBizIds(Collection bizIds, SFunction mapper) { + public Map getMapByBizIds(Collection bizIds, SFunction column) { if (isEmpty(bizIds)) { return emptyMap(); } String bizIdColumnName = getBidColumnField().getColumnName(); List dataList = query() - .select(bizIdColumnName, LambdaUtils.getColumnName(mapper)) + .select(bizIdColumnName, LambdaUtils.getColumnName(column)) .in(bizIdColumnName, bizIds) .list(); - return StreamUtils.toMap(dataList, entity -> getBidColumnField().getValue(entity), mapper); + return StreamUtils.toMap(dataList, entity -> getBidColumnField().getValue(entity), column); } @Override -- GitLab