如何在 java 中同时获取 null 和 0 作为小数
How to get both null and 0 for decimal in java
我正在尝试从数据库中将 sugar_per_serving 的 null 和 0.00 检索到 api 中。数据类型为 BigDecimal。现在我在数据库中输入空白,但在 api 上它显示为 0。有没有办法将 0 更改为 null?下面是我的代码:
WeightManagement.java
public BigDecimal sugar_per_serving;
public BigDecimal getSugar_per_serving() {
return sugar_per_serving;
}
public void setSugar_per_serving(BigDecimal sugar_per_serving) {
this.sugar_per_serving = sugar_per_serving;
}
PortalDBImpl.java
food.sugar_per_serving=rs.getBigDecimal("sugar_per_serving");
仅为 select 更改:
public BigDecimal getSugar_per_serving() {
return sugar_per_serving == null ? 0 : sugar_per_serving;
}
如果需要 update/insert,请更改:
public void setSugar_per_serving(BigDecimal sugar_per_serving) {
this.sugar_per_serving = sugar_per_serving == null ? 0 : sugar_per_serving;
}
我正在尝试从数据库中将 sugar_per_serving 的 null 和 0.00 检索到 api 中。数据类型为 BigDecimal。现在我在数据库中输入空白,但在 api 上它显示为 0。有没有办法将 0 更改为 null?下面是我的代码:
WeightManagement.java
public BigDecimal sugar_per_serving;
public BigDecimal getSugar_per_serving() {
return sugar_per_serving;
}
public void setSugar_per_serving(BigDecimal sugar_per_serving) {
this.sugar_per_serving = sugar_per_serving;
}
PortalDBImpl.java
food.sugar_per_serving=rs.getBigDecimal("sugar_per_serving");
仅为 select 更改:
public BigDecimal getSugar_per_serving() {
return sugar_per_serving == null ? 0 : sugar_per_serving;
}
如果需要 update/insert,请更改:
public void setSugar_per_serving(BigDecimal sugar_per_serving) {
this.sugar_per_serving = sugar_per_serving == null ? 0 : sugar_per_serving;
}