"...git@gitlab.schbrain.com:panwangnan/schbrain-parent.git" did not exist on "6db1a7125b7472505c696c5097fd8fe18e7be303"
Commit 64312418 authored by liaozan's avatar liaozan 🏀

Add more shortcut method for BaseService

parent 37c03985
......@@ -22,6 +22,25 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
*/
T getById(Long id, Supplier<? extends RuntimeException> notFoundSupplier);
/**
* 根据 id 获取记录
*/
<V> V getById(Long id, SFunction<T, V> column);
/**
* 根据 id 获取记录
*
* @param throwIfNotFound 未获取到记录时是否抛异常
*/
<V> V getById(Long id, SFunction<T, V> column, boolean throwIfNotFound);
/**
* 根据 id 获取记录
*
* @param notFoundSupplier 未获取到记录时的异常处理
*/
<V> V getById(Long id, SFunction<T, V> column, Supplier<? extends RuntimeException> notFoundSupplier);
/**
* 根据 id 获取
*/
......@@ -30,7 +49,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据 id 获取
*/
<V> Map<Long, V> getMapByIds(Collection<Long> ids, SFunction<T, V> mapper);
<V> Map<Long, V> getMapByIds(Collection<Long> ids, SFunction<T, V> column);
/**
* 根据业务主键获取记录
......@@ -40,22 +59,51 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据业务主键获取记录
*
* @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<? extends RuntimeException> notFoundSupplier);
/**
* 根据业务主键获取记录
*/
<V> V getByBizId(Object bizId, SFunction<T, V> column);
/**
* 根据业务主键获取记录
*
* @param throwsIfNotFound 未获取到记录时是否抛异常
*/
<V> V getByBizId(Object bizId, SFunction<T, V> column, boolean throwsIfNotFound);
/**
* 根据业务主键获取记录
*
* @param notFoundSupplier 未获取到记录时的异常处理
*/
<V> V getByBizId(Object bizId, SFunction<T, V> column, Supplier<? extends RuntimeException> notFoundSupplier);
/**
* 根据业务主键获取
*/
<V> List<V> listByIds(Collection<Long> ids, SFunction<T, V> column);
/**
* 根据业务主键获取
*/
<K> List<T> listByBizIds(Collection<K> bizIds);
/**
* 根据业务主键获取
*/
<K, V> List<V> listByBizIds(Collection<K> bizIds, SFunction<T, V> column);
/**
* 根据业务主键获取
*/
......@@ -64,7 +112,7 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
/**
* 根据业务主键获取
*/
<K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, SFunction<T, V> mapper);
<K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, SFunction<T, V> column);
/**
* 根据 id 更新,null 会被更新为 null
......
......@@ -58,9 +58,32 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
return entity;
}
@Override
public <V> V getById(Long id, SFunction<T, V> column) {
return getById(id, column, false);
}
@Override
public <V> V getById(Long id, SFunction<T, V> column, boolean throwIfNotFound) {
Supplier<? extends RuntimeException> notFoundSupplier = null;
if (throwIfNotFound) {
notFoundSupplier = () -> new NoSuchRecordException(entityClass);
}
return getById(id, column, notFoundSupplier);
}
@Override
public <V> V getById(Long id, SFunction<T, V> column, Supplier<? extends RuntimeException> 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<Long, T> getMapByIds(Collection<Long> 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<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public <V> Map<Long, V> getMapByIds(Collection<Long> ids, SFunction<T, V> mapper) {
public <V> Map<Long, V> getMapByIds(Collection<Long> ids, SFunction<T, V> column) {
if (isEmpty(ids)) {
return emptyMap();
}
// noinspection unchecked
List<T> dataList = lambdaQuery().select(T::getId, mapper).in(T::getId, ids).list();
return StreamUtils.toMap(dataList, T::getId, mapper);
List<T> 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<M extends BaseMapper<T>, T extends BaseEntity> exte
return entity;
}
@Override
public <V> V getByBizId(Object bizId, SFunction<T, V> column) {
return getByBizId(bizId, column, false);
}
@Override
public <V> V getByBizId(Object bizId, SFunction<T, V> column, boolean throwIfNotFound) {
Supplier<? extends RuntimeException> notFoundSupplier = null;
if (throwIfNotFound) {
notFoundSupplier = () -> new NoSuchRecordException(entityClass);
}
return getByBizId(bizId, column, notFoundSupplier);
}
@Override
public <V> V getByBizId(Object bizId, SFunction<T, V> column, Supplier<? extends RuntimeException> 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 <V> List<V> listByIds(Collection<Long> ids, SFunction<T, V> column) {
if (isEmpty(ids)) {
return emptyList();
}
List<T> dataList = lambdaQuery().select(column).in(T::getId, ids).list();
return StreamUtils.toList(dataList, column);
}
@Override
public <K> List<T> listByBizIds(Collection<K> 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 <K, V> List<V> listByBizIds(Collection<K> bizIds, SFunction<T, V> column) {
if (isEmpty(bizIds)) {
return emptyList();
}
List<T> dataList = query()
.select(LambdaUtils.getColumnName(column))
.in(getBidColumnField().getColumnName(), bizIds)
.list();
return StreamUtils.toList(dataList, column);
}
@Override
public <K> Map<K, T> getMapByBizIds(Collection<K> 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<M extends BaseMapper<T>, T extends BaseEntity> exte
}
@Override
public <K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, SFunction<T, V> mapper) {
public <K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, SFunction<T, V> column) {
if (isEmpty(bizIds)) {
return emptyMap();
}
String bizIdColumnName = getBidColumnField().getColumnName();
List<T> 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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment