feat: 用户可以自定义是否在source中存储id列

This commit is contained in:
jaime 2025-02-27 14:58:11 +08:00
parent bdc279fe54
commit aa7c1cf4a6
6 changed files with 35 additions and 9 deletions

View File

@ -29,4 +29,9 @@ public @interface IndexId {
* @return 默认为未设置
*/
IdType type() default IdType.NONE;
/**
* 是否将主键写入到source中
*/
boolean writeToSource() default false;
}

View File

@ -71,6 +71,10 @@ public class GlobalConfig {
* es id generate type. es id生成类型 默认由es自动生成
*/
private IdType idType = IdType.NONE;
/**
* es id是否写入到source中
*/
private boolean id2Source = false;
/**
* Field update strategy default nonNull 字段更新策略,默认非null
*/

View File

@ -39,6 +39,10 @@ public class EntityInfo {
* id数据类型 如Long.class String.class
*/
private Class<?> idClass;
/**
* id列是否需要写入到source
*/
private boolean id2Source;
/**
* 索引名称(原索引名)
*/

View File

@ -13,6 +13,7 @@ import org.dromara.easyes.core.toolkit.FieldUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -36,9 +37,7 @@ public class JacksonCache {
// 当前类
if (!JacksonCustomConfig.jacksonConfigMap.containsKey(clz)) {
JacksonCustomConfig config = init(clz, e.getMappingColumnMap(), e.getClassDateFormatMap().get(clz), e.getFieldList());
config.includeMap.put(e.getKeyProperty(), NON_NULL);
config.includeMap.put(getterMethod(clz, e.getKeyProperty()), NON_NULL);
JacksonCustomConfig config = init(clz, e.isId2Source(), e.getKeyProperty(), e.getMappingColumnMap(), e.getClassDateFormatMap().get(clz), e.getFieldList());
JacksonCustomConfig.jacksonConfigMap.put(clz, config);
}
@ -47,7 +46,7 @@ public class JacksonCache {
if (JacksonCustomConfig.jacksonConfigMap.containsKey(clz)) {
return;
}
JacksonCustomConfig c = init(nestedClz, nestedMappingColumnMap,
JacksonCustomConfig c = init(nestedClz, null, null, nestedMappingColumnMap,
e.getClassDateFormatMap().get(nestedClz),
e.getNestedOrObjectFieldListMap().get(nestedClz)
);
@ -58,6 +57,8 @@ public class JacksonCache {
private static JacksonCustomConfig init(
Class<?> clz,
Boolean id2Source,
String idJavaFieldName,
Map<String, String> mappingColumnMap,
Map<String, String> formatMap,
List<EntityFieldInfo> fieldList
@ -69,7 +70,11 @@ public class JacksonCache {
config.allJsonField = JsonIncludeProperties.Value.from(new JsonIncludeProperties() {
@Override
public String[] value() {
return config.javaJsonFieldNameMap.values().toArray(new String[0]);
Collection<String> values = config.javaJsonFieldNameMap.values();
if (id2Source != null && !id2Source) {
values.remove(idJavaFieldName);
}
return values.toArray(new String[0]);
}
@Override
@ -112,6 +117,10 @@ public class JacksonCache {
break;
}
});
if (id2Source != null && id2Source) {
config.includeMap.put(idJavaFieldName, NON_NULL);
config.includeMap.put(getterMethod(clz, idJavaFieldName), NON_NULL);
}
return config;
}

View File

@ -620,6 +620,7 @@ public class EntityInfoHelper {
} else {
entityInfo.setIdType(tableId.type());
}
entityInfo.setId2Source(tableId.writeToSource());
// 字段
field.setAccessible(Boolean.TRUE);
entityInfo.setClazz(field.getDeclaringClass())
@ -660,6 +661,7 @@ public class EntityInfoHelper {
String mappingColumn = getMappingColumn(dbConfig, field);
entityInfo.getMappingColumnMap().putIfAbsent(column, mappingColumn);
entityInfo.getColumnMappingMap().putIfAbsent(mappingColumn, column);
entityInfo.setId2Source(dbConfig.isId2Source());
return true;
}
return false;

View File

@ -437,11 +437,13 @@ public class IndexUtils {
private static Map<String, Property> initInfo(EntityInfo entityInfo, GlobalConfig.DbConfig dbConfig,
Map<String, Property> properties, List<EsIndexParam> indexParamList) {
// 主键
if (entityInfo.isId2Source()) {
String idFieldName = entityInfo.getKeyProperty();
if (dbConfig.isMapUnderscoreToCamelCase()) {
idFieldName = StringUtils.camelToUnderline(idFieldName);
}
properties.put(idFieldName, KeywordProperty.of(a -> a)._toProperty());
}
// 其他字段
indexParamList.forEach(indexParam -> {