EntityFullFunc

This commit is contained in:
redkale
2024-09-22 22:03:38 +08:00
parent f207999117
commit 5474cd846d
6 changed files with 334 additions and 14 deletions

View File

@@ -220,15 +220,6 @@ public interface DataResultSet extends DataResultSetRow {
return (byte[]) (index > 0 ? row.getObject(index) : row.getObject(column));
} else {
Serializable o = (Serializable) (index > 0 ? row.getObject(index) : row.getObject(column));
if (o != null) {
if (t == String.class && o instanceof String) {
return o;
} else if ((t == Integer.class || t == int.class) && o instanceof Integer) {
return o;
} else if ((t == Long.class || t == long.class) && o instanceof Long) {
return o;
}
}
return formatColumnValue(t, attr.genericType(), o);
}
}

View File

@@ -6,6 +6,7 @@ package org.redkale.source;
import java.io.Serializable;
import java.util.List;
import org.redkale.annotation.ClassDepends;
import org.redkale.annotation.Nullable;
import org.redkale.util.Attribute;
@@ -13,6 +14,7 @@ import org.redkale.util.Attribute;
*
* @author zhangjx
*/
@ClassDepends
public interface DataResultSetRow {
// 可以为空
@@ -29,106 +31,134 @@ public interface DataResultSetRow {
}
// columnIdex从1开始
@ClassDepends
public String getString(int columnIdex);
@ClassDepends
public String getString(String columnLabel);
// columnIdex从1开始
@ClassDepends
public byte[] getBytes(int columnIdex);
@ClassDepends
public byte[] getBytes(String columnLabel);
// columnIdex从1开始
@ClassDepends
public Boolean getBoolean(int columnIdex);
@ClassDepends
public Boolean getBoolean(String columnLabel);
// columnIdex从1开始
@ClassDepends
public Short getShort(int columnIdex);
@ClassDepends
public Short getShort(String columnLabel);
// columnIdex从1开始
@ClassDepends
public Integer getInteger(int columnIdex);
@ClassDepends
public Integer getInteger(String columnLabel);
// columnIdex从1开始
@ClassDepends
public Float getFloat(int columnIdex);
@ClassDepends
public Float getFloat(String columnLabel);
// columnIdex从1开始
@ClassDepends
public Long getLong(int columnIdex);
@ClassDepends
public Long getLong(String columnLabel);
// columnIdex从1开始
@ClassDepends
public Double getDouble(int columnIdex);
@ClassDepends
public Double getDouble(String columnLabel);
// columnIdex从1开始
@ClassDepends
default boolean getBoolean(int columnIdex, boolean defValue) {
Boolean val = getBoolean(columnIdex);
return val == null ? defValue : val;
}
@ClassDepends
default boolean getBoolean(String columnLabel, boolean defValue) {
Boolean val = getBoolean(columnLabel);
return val == null ? defValue : val;
}
// columnIdex从1开始
@ClassDepends
default short getShort(int columnIdex, short defValue) {
Short val = getShort(columnIdex);
return val == null ? defValue : val;
}
@ClassDepends
default short getShort(String columnLabel, short defValue) {
Short val = getShort(columnLabel);
return val == null ? defValue : val;
}
// columnIdex从1开始
@ClassDepends
default int getInteger(int columnIdex, int defValue) {
Integer val = getInteger(columnIdex);
return val == null ? defValue : val;
}
@ClassDepends
default int getInteger(String columnLabel, int defValue) {
Integer val = getInteger(columnLabel);
return val == null ? defValue : val;
}
// columnIdex从1开始
@ClassDepends
default float getFloat(int columnIdex, float defValue) {
Float val = getFloat(columnIdex);
return val == null ? defValue : val;
}
@ClassDepends
default float getFloat(String columnLabel, float defValue) {
Float val = getFloat(columnLabel);
return val == null ? defValue : val;
}
// columnIdex从1开始
@ClassDepends
default long getLong(int columnIdex, long defValue) {
Long val = getLong(columnIdex);
return val == null ? defValue : val;
}
@ClassDepends
default long getLong(String columnLabel, long defValue) {
Long val = getLong(columnLabel);
return val == null ? defValue : val;
}
// columnIdex从1开始
@ClassDepends
default double getDouble(int columnIdex, double defValue) {
Double val = getDouble(columnIdex);
return val == null ? defValue : val;
}
@ClassDepends
default double getDouble(String columnLabel, double defValue) {
Double val = getDouble(columnLabel);
return val == null ? defValue : val;
@@ -139,6 +169,7 @@ public interface DataResultSetRow {
*
* @return boolean
*/
@ClassDepends
public boolean wasNull();
/**

View File

@@ -67,6 +67,9 @@ public class EntityBuilder<T> {
// 数据库中所有字段, 顺序必须与querySqlColumns、querySqlColumnSequence一致
private final Attribute<T, Serializable>[] attributes;
// 创建全字段对应对象的函数
private final EntityFullFunc<T> fullFunc;
EntityBuilder(
Class<T> type,
Creator<T> creator,
@@ -92,6 +95,11 @@ public class EntityBuilder<T> {
sqlAttrMap.put(col, v);
sqlLowerAttrMap.put(lowerCaseColumn(col), v);
});
if (constructorAttributes == null && !entityIsMap) {
this.fullFunc = EntityFullFunc.create(type, creator, unconstructorAttributes);
} else {
this.fullFunc = null;
}
}
public static boolean isSimpleType(Class type) {
@@ -444,6 +452,9 @@ public class EntityBuilder<T> {
}
public T getFullEntityValue(final DataResultSetRow row) {
if (this.fullFunc != null) {
return this.fullFunc.getObject(row);
}
return getEntityValue(
constructorAttributes, constructorAttributes == null ? attributes : unconstructorAttributes, row);
}
@@ -476,7 +487,7 @@ public class EntityBuilder<T> {
return (T) map;
}
T obj;
int index = 0;
int columnIndex = 0;
if (this.constructorParameters == null) {
obj = creator.create();
} else {
@@ -484,7 +495,7 @@ public class EntityBuilder<T> {
for (int i = 0; i < constructorAttrs.length; i++) {
Attribute<T, Serializable> attr = constructorAttrs[i];
if (attr != null) {
cps[i] = getFieldValue(row, attr, ++index);
cps[i] = getFieldValue(row, attr, ++columnIndex);
}
}
obj = creator.create(cps);
@@ -492,7 +503,7 @@ public class EntityBuilder<T> {
if (unconstructorAttrs != null) {
for (Attribute<T, Serializable> attr : unconstructorAttrs) {
if (attr != null) {
attr.set(obj, getFieldValue(row, attr, ++index));
attr.set(obj, getFieldValue(row, attr, ++columnIndex));
}
}
}
@@ -542,8 +553,8 @@ public class EntityBuilder<T> {
return (Serializable) row.getObject(sqlColumn);
}
protected Serializable getFieldValue(final DataResultSetRow row, Attribute<T, Serializable> attr, int index) {
return row.getObject(attr, index, index > 0 ? null : this.getSQLColumn(null, attr.field()));
protected Serializable getFieldValue(DataResultSetRow row, Attribute<T, Serializable> attr, int columnIndex) {
return row.getObject(attr, columnIndex, columnIndex > 0 ? null : this.getSQLColumn(null, attr.field()));
}
/**

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2016-2116 Redkale
* All rights reserved.
*/
package org.redkale.source;
import java.io.Serializable;
import org.redkale.util.Attribute;
import org.redkale.util.Creator;
/**
* 可以是实体类也可以是查询结果的JavaBean类
*
* @author zhangjx
* @param <T> T
* @since 2.8.0
*/
public abstract class EntityFullFunc<T> {
protected final Class<T> type;
protected final Creator<T> creator;
protected final Attribute<T, Serializable>[] attrs;
protected EntityFullFunc(Class<T> type, Creator<T> creator, Attribute<T, Serializable>[] attrs) {
this.type = type;
this.creator = creator;
this.attrs = attrs;
}
public abstract T getObject(DataResultSetRow row);
protected void setFieldValue(int attrIndex, DataResultSetRow row, T obj) {
Attribute<T, Serializable> attr = attrs[attrIndex];
if (attr != null) {
attr.set(obj, row.getObject(attr, attrIndex + 1, null));
}
}
public Class<T> getType() {
return type;
}
public Creator<T> getCreator() {
return creator;
}
public Attribute<T, Serializable>[] getAttrs() {
return attrs;
}
public static <T> EntityFullFunc<T> create(Class<T> type, Creator<T> creator, Attribute<T, Serializable>[] attrs) {
return null;
}
}