【项目实战】使用Hutool-json工具包中的方法序列化,对象数据中值为null的属性被过滤的问题

时间:2025-02-16 08:04:03
/** * 存储到数据库, 将JSON对象转换成字符串; 从数据库获取数据, 将字符串转为JSON对象. */ @MappedTypes({JSONObject.class}) @MappedJdbcTypes({JdbcType.VARCHAR}) public class JsonTypeHandler extends BaseTypeHandler<JSONObject> { @Override public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException { //支持空值 JSONConfig jsonConfig = new JSONConfig(); jsonConfig.setIgnoreNullValue(Boolean.FALSE); ps.setString(i, JSONUtil.toJsonStr(parameter, jsonConfig)); } @Override public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException { return JSONUtil.parseObj(rs.getString(columnName)).toBean(JSONObject.class); } @Override public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return JSONUtil.parseObj(rs.getString(columnIndex)).toBean(JSONObject.class); } @Override public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return JSONUtil.parseObj(cs.getString(columnIndex)).toBean(JSONObject.class); } }