Commit 56814af5 authored by liaozan's avatar liaozan 🏀

BaseService#getMapBy* support value mapper

parent 6f357f4e
...@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
public interface BaseService<T extends BaseEntity> extends IService<T> { public interface BaseService<T extends BaseEntity> extends IService<T> {
...@@ -28,6 +29,11 @@ public interface BaseService<T extends BaseEntity> extends IService<T> { ...@@ -28,6 +29,11 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
*/ */
Map<Long, T> getMapByIds(Collection<Long> ids); Map<Long, T> getMapByIds(Collection<Long> ids);
/**
* 根据 id 获取
*/
<V> Map<Long, V> getMapByIds(Collection<Long> ids, Function<T, V> mapper);
/** /**
* 根据业务主键获取记录 * 根据业务主键获取记录
*/ */
...@@ -57,6 +63,11 @@ public interface BaseService<T extends BaseEntity> extends IService<T> { ...@@ -57,6 +63,11 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
*/ */
<K> Map<K, T> getMapByBizIds(Collection<K> bizIds); <K> Map<K, T> getMapByBizIds(Collection<K> bizIds);
/**
* 根据业务主键获取
*/
<K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, Function<T, V> mapper);
/** /**
* 根据 id 更新,null 会被更新为 null * 根据 id 更新,null 会被更新为 null
*/ */
...@@ -72,4 +83,4 @@ public interface BaseService<T extends BaseEntity> extends IService<T> { ...@@ -72,4 +83,4 @@ public interface BaseService<T extends BaseEntity> extends IService<T> {
*/ */
boolean updateBatchByIdsWithNull(Collection<T> entityList, int batchSize); boolean updateBatchByIdsWithNull(Collection<T> entityList, int batchSize);
} }
\ No newline at end of file
...@@ -16,11 +16,13 @@ import org.springframework.beans.factory.InitializingBean; ...@@ -16,11 +16,13 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import javax.annotation.Nullable;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
...@@ -29,6 +31,7 @@ import java.util.function.Supplier; ...@@ -29,6 +31,7 @@ import java.util.function.Supplier;
*/ */
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T>, ValidateSupport, InitializingBean { public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T>, ValidateSupport, InitializingBean {
@Nullable
private BizIdColumnField bizIdColumnField; private BizIdColumnField bizIdColumnField;
@Override @Override
...@@ -56,10 +59,15 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -56,10 +59,15 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override @Override
public Map<Long, T> getMapByIds(Collection<Long> ids) { public Map<Long, T> getMapByIds(Collection<Long> ids) {
return getMapByIds(ids, Function.identity());
}
@Override
public <V> Map<Long, V> getMapByIds(Collection<Long> ids, Function<T, V> mapper) {
if (isEmpty(ids)) { if (isEmpty(ids)) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
return StreamUtils.toMap(super.listByIds(ids), T::getId); return StreamUtils.toMap(listByIds(ids), T::getId, mapper);
} }
@Override @Override
...@@ -97,11 +105,16 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -97,11 +105,16 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
@Override @Override
public <K> Map<K, T> getMapByBizIds(Collection<K> bizIds) { public <K> Map<K, T> getMapByBizIds(Collection<K> bizIds) {
return getMapByBizIds(bizIds, Function.identity());
}
@Override
public <K, V> Map<K, V> getMapByBizIds(Collection<K> bizIds, Function<T, V> mapper) {
assertBidColumnFieldExist(); assertBidColumnFieldExist();
if (isEmpty(bizIds)) { if (isEmpty(bizIds)) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
return StreamUtils.toMap(listByBizIds(bizIds), entity -> bizIdColumnField.getValue(entity)); return StreamUtils.toMap(listByBizIds(bizIds), entity -> bizIdColumnField.getValue(entity), mapper);
} }
@Override @Override
...@@ -147,4 +160,4 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte ...@@ -147,4 +160,4 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
return mapperClass.getName() + StringPool.DOT + "alwaysUpdateSomeColumnById"; return mapperClass.getName() + StringPool.DOT + "alwaysUpdateSomeColumnById";
} }
} }
\ No newline at end of file
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