fix: isSelect导致条件查询NPE

This commit is contained in:
yulichang 2024-06-17 19:03:09 +08:00
parent 266d71293f
commit 22ad7c4232
3 changed files with 19 additions and 12 deletions

View File

@ -1,6 +1,5 @@
package com.github.yulichang.toolkit.support;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.github.yulichang.adapter.AdapterHelper;
import com.github.yulichang.toolkit.FieldStringMap;
@ -33,10 +32,10 @@ public class ColumnCache {
List<SelectCache> list = new ArrayList<>();
if (AdapterHelper.getAdapter().mpjHasPK(tableInfo)) {
list.add(new SelectCache(clazz, true, tableInfo.getKeyColumn(), tableInfo.getKeyType(),
tableInfo.getKeyProperty(), null));
tableInfo.getKeyProperty(), true, null));
}
list.addAll(tableInfo.getFieldList().stream().filter(TableFieldInfo::isSelect).map(f ->
new SelectCache(clazz, false, f.getColumn(), f.getPropertyType(), f.getProperty(), f))
list.addAll(tableInfo.getFieldList().stream().map(f ->
new SelectCache(clazz, false, f.getColumn(), f.getPropertyType(), f.getProperty(), f.isSelect(), f))
.collect(Collectors.toList()));
return list;
});

View File

@ -52,7 +52,7 @@ public interface Query<Children> extends Serializable {
default <E> Children select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
TableInfo info = TableHelper.getAssert(entityClass);
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(entityClass);
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
info.getFieldList().stream().filter(TableFieldInfo::isSelect).filter(predicate).collect(Collectors.toList()).forEach(
i -> getSelectColum().add(new SelectNormal(cacheMap.get(i.getProperty()), getIndex(), isHasAlias(), getAlias())));
return getChildren();
}
@ -71,7 +71,7 @@ public interface Query<Children> extends Serializable {
default <E> Children selectFilter(Class<E> entityClass, Predicate<SelectCache> predicate) {
TableInfo info = TableHelper.getAssert(entityClass);
List<SelectCache> cacheList = ColumnCache.getListField(entityClass);
cacheList.stream().filter(predicate).collect(Collectors.toList()).forEach(
cacheList.stream().filter(SelectCache::isSelect).filter(predicate).collect(Collectors.toList()).forEach(
i -> getSelectColum().add(new SelectNormal(i, getIndex(), isHasAlias(), getAlias())));
return getChildren();
}
@ -130,7 +130,9 @@ public interface Query<Children> extends Serializable {
for (FieldCache cache : fieldList) {
if (normalMap.containsKey(cache.getField().getName())) {
SelectCache selectCache = normalMap.get(cache.getField().getName());
getSelectColum().add(new SelectNormal(selectCache, getIndex(), isHasAlias(), getAlias()));
if (selectCache.isSelect()) {
getSelectColum().add(new SelectNormal(selectCache, getIndex(), isHasAlias(), getAlias()));
}
}
}
return getChildren();
@ -158,7 +160,7 @@ public interface Query<Children> extends Serializable {
* 查询实体类全部字段
*/
default Children selectAll(Class<?> clazz) {
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().map(i ->
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().filter(SelectCache::isSelect).map(i ->
new SelectNormal(i, getIndex(), isHasAlias(), getAlias())).collect(Collectors.toList()));
return getChildren();
}
@ -167,7 +169,7 @@ public interface Query<Children> extends Serializable {
* 查询实体类全部字段
*/
default Children selectAll(Class<?> clazz, String prefix) {
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().map(i ->
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().filter(SelectCache::isSelect).map(i ->
new SelectNormal(i, getIndex(), true, prefix)).collect(Collectors.toList()));
return getChildren();
}
@ -182,7 +184,7 @@ public interface Query<Children> extends Serializable {
default <E> Children selectAll(Class<E> clazz, SFunction<E, ?>... exclude) {
Set<String> excludeSet = Arrays.stream(exclude).map(i ->
LambdaUtils.getName(i).toUpperCase(Locale.ENGLISH)).collect(Collectors.toSet());
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().filter(e ->
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().filter(e -> e.isSelect() &&
!excludeSet.contains(e.getColumProperty().toUpperCase(Locale.ENGLISH))).map(i ->
new SelectNormal(i, getIndex(), isHasAlias(), getAlias())).collect(Collectors.toList()));
return getChildren();
@ -198,7 +200,7 @@ public interface Query<Children> extends Serializable {
default <E> Children selectAll(Class<E> clazz, String prefix, SFunction<E, ?>... exclude) {
Set<String> excludeSet = Arrays.stream(exclude).map(i ->
LambdaUtils.getName(i).toUpperCase(Locale.ENGLISH)).collect(Collectors.toSet());
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().filter(e ->
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().filter(e -> e.isSelect() &&
!excludeSet.contains(e.getColumProperty().toUpperCase(Locale.ENGLISH))).map(e ->
new SelectNormal(e, getIndex(), true, prefix)).collect(Collectors.toList()));
return getChildren();

View File

@ -70,7 +70,12 @@ public class SelectCache {
*/
private final TypeHandler<?> typeHandler;
public SelectCache(Class<?> clazz, boolean isPk, String column, Class<?> columnType, String columProperty, TableFieldInfo tableFieldInfo) {
/**
* 是否查询
*/
private final boolean isSelect;
public SelectCache(Class<?> clazz, boolean isPk, String column, Class<?> columnType, String columProperty, boolean isSelect, TableFieldInfo tableFieldInfo) {
this.clazz = clazz;
this.isPk = isPk;
this.column = column;
@ -78,6 +83,7 @@ public class SelectCache {
this.columProperty = columProperty;
this.tagColumn = MPJStringUtils.getTargetColumn(column);
this.tableFieldInfo = tableFieldInfo;
this.isSelect = isSelect;
if (Objects.isNull(tableFieldInfo)) {
this.hasTypeHandle = false;
this.typeHandler = null;