EntityFullFunc
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
/**
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
56
src/main/java/org/redkale/source/EntityFullFunc.java
Normal file
56
src/main/java/org/redkale/source/EntityFullFunc.java
Normal 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;
|
||||
}
|
||||
}
|
||||
181
src/test/java/org/redkale/test/source/FullBean.java
Normal file
181
src/test/java/org/redkale/test/source/FullBean.java
Normal file
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.test.source;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.persistence.Id;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class FullBean {
|
||||
@Id
|
||||
private long seqid;
|
||||
|
||||
private String name;
|
||||
|
||||
private byte[] img;
|
||||
|
||||
private BigInteger number;
|
||||
|
||||
private boolean flag;
|
||||
|
||||
private short status;
|
||||
|
||||
private int id;
|
||||
|
||||
private long createTime;
|
||||
|
||||
private float point;
|
||||
|
||||
private double money;
|
||||
|
||||
private Boolean flag2;
|
||||
|
||||
private Short status2;
|
||||
|
||||
private Integer id2;
|
||||
|
||||
private Long createTime2;
|
||||
|
||||
private Float point2;
|
||||
|
||||
private Double money2;
|
||||
|
||||
public long getSeqid() {
|
||||
return seqid;
|
||||
}
|
||||
|
||||
public void setSeqid(long seqid) {
|
||||
this.seqid = seqid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public byte[] getImg() {
|
||||
return img;
|
||||
}
|
||||
|
||||
public void setImg(byte[] img) {
|
||||
this.img = img;
|
||||
}
|
||||
|
||||
public BigInteger getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(BigInteger number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public boolean isFlag() {
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void setFlag(boolean flag) {
|
||||
this.flag = flag;
|
||||
}
|
||||
|
||||
public short getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(short status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(long createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public float getPoint() {
|
||||
return point;
|
||||
}
|
||||
|
||||
public void setPoint(float point) {
|
||||
this.point = point;
|
||||
}
|
||||
|
||||
public double getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public void setMoney(double money) {
|
||||
this.money = money;
|
||||
}
|
||||
|
||||
public Boolean getFlag2() {
|
||||
return flag2;
|
||||
}
|
||||
|
||||
public void setFlag2(Boolean flag2) {
|
||||
this.flag2 = flag2;
|
||||
}
|
||||
|
||||
public Short getStatus2() {
|
||||
return status2;
|
||||
}
|
||||
|
||||
public void setStatus2(Short status2) {
|
||||
this.status2 = status2;
|
||||
}
|
||||
|
||||
public Integer getId2() {
|
||||
return id2;
|
||||
}
|
||||
|
||||
public void setId2(Integer id2) {
|
||||
this.id2 = id2;
|
||||
}
|
||||
|
||||
public Long getCreateTime2() {
|
||||
return createTime2;
|
||||
}
|
||||
|
||||
public void setCreateTime2(Long createTime2) {
|
||||
this.createTime2 = createTime2;
|
||||
}
|
||||
|
||||
public Float getPoint2() {
|
||||
return point2;
|
||||
}
|
||||
|
||||
public void setPoint2(Float point2) {
|
||||
this.point2 = point2;
|
||||
}
|
||||
|
||||
public Double getMoney2() {
|
||||
return money2;
|
||||
}
|
||||
|
||||
public void setMoney2(Double money2) {
|
||||
this.money2 = money2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
50
src/test/java/org/redkale/test/source/FullBeanDynFunc.java
Normal file
50
src/test/java/org/redkale/test/source/FullBeanDynFunc.java
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.test.source;
|
||||
|
||||
import java.io.Serializable;
|
||||
import org.redkale.source.DataResultSetRow;
|
||||
import org.redkale.source.EntityFullFunc;
|
||||
import org.redkale.util.Attribute;
|
||||
import org.redkale.util.Creator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class FullBeanDynFunc extends EntityFullFunc<FullBean> {
|
||||
|
||||
public FullBeanDynFunc(Class<FullBean> type, Creator<FullBean> creator, Attribute<FullBean, Serializable>[] attrs) {
|
||||
super(type, creator, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FullBean getObject(DataResultSetRow row) {
|
||||
if (row.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
FullBean rs = creator.create();
|
||||
rs.setSeqid(row.getLong(1, 0));
|
||||
rs.setName(row.getString(2));
|
||||
rs.setImg(row.getBytes(3));
|
||||
|
||||
setFieldValue(4, row, rs); // number: BigInteger
|
||||
|
||||
rs.setFlag(row.getBoolean(5, false));
|
||||
rs.setStatus(row.getShort(6, (short) 0));
|
||||
rs.setId(row.getInteger(7, 0));
|
||||
rs.setCreateTime(row.getLong(8, 0));
|
||||
rs.setPoint(row.getFloat(9, 0f));
|
||||
rs.setMoney(row.getDouble(10, 0d));
|
||||
|
||||
rs.setFlag2(row.getBoolean(5));
|
||||
rs.setStatus2(row.getShort(6));
|
||||
rs.setId2(row.getInteger(7));
|
||||
rs.setCreateTime2(row.getLong(8));
|
||||
rs.setPoint2(row.getFloat(9));
|
||||
rs.setMoney2(row.getDouble(10));
|
||||
return rs;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user