Skip to content

Commit 9f62724

Browse files
committed
大数转 String 返回,避免前端/客户端拿到精度丢失甚至严重失真的值,支持重写 getNumVal 来自定义
1 parent 7403f0a commit 9f62724

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package apijson.orm;
77

88
import java.io.BufferedReader;
9+
import java.math.BigDecimal;
10+
import java.math.BigInteger;
911
import java.sql.Blob;
1012
import java.sql.Clob;
1113
import java.sql.Connection;
@@ -995,9 +997,12 @@ protected Object getValue(@NotNull SQLConfig<T> config, @NotNull ResultSet rs, @
995997
boolean castToJson = false;
996998

997999
//数据库查出来的null和empty值都有意义,去掉会导致 Moment:{ @column:"content" } 部分无结果及中断数组查询!
998-
if (value instanceof Boolean || value instanceof Number) {
1000+
if (value instanceof Boolean) {
9991001
//加快判断速度
10001002
}
1003+
else if (value instanceof Number) {
1004+
value = getNumVal((Number) value);
1005+
}
10011006
else if (value instanceof Timestamp) {
10021007
value = ((Timestamp) value).toString();
10031008
}
@@ -1058,6 +1063,27 @@ else if (value instanceof Clob) { //SQL Server TEXT 类型 居然走这个
10581063
return value;
10591064
}
10601065

1066+
public Object getNumVal(Number value) {
1067+
if (value == null) {
1068+
return null;
1069+
}
1070+
1071+
if (value instanceof BigInteger) {
1072+
return ((BigInteger) value).toString();
1073+
}
1074+
1075+
if (value instanceof BigDecimal) {
1076+
return ((BigDecimal) value).toString();
1077+
}
1078+
1079+
double v = value.doubleValue();
1080+
if (v > Integer.MAX_VALUE || v < Integer.MIN_VALUE) { // 避免前端/客户端拿到精度丢失甚至严重失真的值
1081+
return value.toString();
1082+
}
1083+
1084+
return value;
1085+
}
1086+
10611087

10621088
/**判断是否为JSON类型
10631089
* @param config

0 commit comments

Comments
 (0)