利用反射,将为null的并且数据类型为Integer和int 的字段设置为0
import java.lang.reflect.Field;
public class EntityUtils {
public static void setNullPropertiesToZero(Object entity) {
Field[] fields = entity.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
if (field.get(entity) == null) {
if (field.getType() == Integer.class || field.getType() == int.class) {
field.set(entity, 0);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}