Commit 9e9420f4 authored by liaozan's avatar liaozan 🏀

getMapByBizIds supports querying only required fields

parent 9536bb99
......@@ -12,6 +12,7 @@ import com.schbrain.framework.autoconfigure.mybatis.annotation.BizId;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdColumnField;
import com.schbrain.framework.autoconfigure.mybatis.biz.BizIdHelper;
import com.schbrain.framework.autoconfigure.mybatis.exception.NoSuchRecordException;
import com.schbrain.framework.autoconfigure.mybatis.util.LambdaUtils;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.transaction.annotation.Transactional;
......@@ -123,8 +124,12 @@ public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> exte
if (isEmpty(bizIds)) {
return emptyMap();
}
// How to get the mapper column ?
return StreamUtils.toMap(listByBizIds(bizIds), entity -> getBidColumnField().getValue(entity), mapper);
String bizIdColumnName = getBidColumnField().getColumnName();
List<T> dataList = query()
.select(bizIdColumnName, LambdaUtils.getColumnName(mapper))
.in(bizIdColumnName, bizIds)
.list();
return StreamUtils.toMap(dataList, entity -> getBidColumnField().getValue(entity), mapper);
}
@Override
......
......@@ -34,9 +34,9 @@ public class BizIdColumnField {
this.fieldType = bizIdField.getType();
try {
Lookup lookup = privateLookupIn(entityClass, lookup());
this.getter = lookup.findGetter(entityClass, bizIdField.getName(), fieldType);
this.setter = lookup.findSetter(entityClass, bizIdField.getName(), fieldType);
} catch (NoSuchFieldException | IllegalAccessException e) {
this.getter = lookup.unreflectGetter(bizIdField);
this.setter = lookup.unreflectSetter(bizIdField);
} catch (IllegalAccessException e) {
throw new BaseException(e.getMessage(), e);
}
}
......
package com.schbrain.framework.autoconfigure.mybatis.util;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
/**
* @author liaozan
* @since 2023/8/29
*/
public class LambdaUtils {
public static <T> String getColumnName(SFunction<T, ?> column) {
LambdaQueryWrapperExt<T> query = new LambdaQueryWrapperExt<>();
return query.columnToString(column);
}
/**
* Extend LambdaQueryWrapper to expose columnToString method
*/
private static class LambdaQueryWrapperExt<T> extends LambdaQueryWrapper<T> {
private static final long serialVersionUID = 943345355018736972L;
@Override
protected String columnToString(SFunction<T, ?> column) {
return super.columnToString(column);
}
}
}
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