一、 操作json数据
1、fastjson
jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
// list转成json集合
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
// 对象转成json对象
JSONObject.parseObject(JSONObject.toJSONString(request))
// json集合转成list集合
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
// json对象转成javabean
WarehouseRegisterApiRequest registerApiRequest = JSONObject.toJavaObject(jsonObject, WarehouseRegisterApiRequest.class);
// 字符串转List
String str = "";
List<T> list = JSONObject.parseArray(str,T.class);
2、json-lib
jar包
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
// 对象转JSONObeject
JSONObject jsonObject = JSONObject.fromObject(对象);
// JSONObject 转对象
WarehouseRegisterInfo info = (WarehouseRegisterInfo)JSONObject.toBean(JSONObject, WarehouseRegisterInfo.class);
如果对象中存在date类型,需要做转换,此时需要用json-lib
// 对象转JsonObject时,对象中date类型进行处理
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
JSONObject jsonObject = JSONObject.fromObject(request, jsonConfig);
warehouseRegisterApplication.setWarehouseRegisterApiRequest(jsonObject);
public class JsonDateValueProcessor implements JsonValueProcessor {
private String datePattern = "yyyy-MM-dd";//默认样式,可以在构造方法中修改
public JsonDateValueProcessor() {
super();
}
public JsonDateValueProcessor(String datePattern) {
super();
this.datePattern = datePattern;
}
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
try {
if (value instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
Date date = (Date) value;
return sdf.format(date);
}
return value == null ? null : value.toString();
} catch (Exception e) {
return null;
}
}
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
return processArrayValue(value, jsonConfig);
}
public String getDatePattern() {
return datePattern;
}
public void setDatePattern(String datePattern) {
this.datePattern = datePattern;
}
}
// 但是使用json-lib时,如果JsonObject 转对象过程中,对象中同时又存在list<对象>,就必须采用这种形式,而fastjson不需要
Map classMap = new HashMap();
classMap.put("attachments", Attachment.class);
WarehouseRegisterInfo warehouseRegisterInfo = (WarehouseRegisterInfo) JSONObject.toBean(JSONObject, WarehouseRegisterInfo.class, classMap);
- 将对象当作字段存进数据库
数据库:postgresql 存储为jsonb数据
json类型存储快,使用慢,jsonb类型存储稍慢,使用较快。
实体
/**
* 变更请求
*/
@ApiModelProperty(name = "变更请求", notes = "变更请求", example = "变更请求")
@TableField("warehouse_change_api_request")
private JSONObject warehouseChangeApiRequest;
自定义的 TypeHandler (这里用的是json-lib)
@MappedJdbcTypes({JdbcType.OTHER})
@MappedTypes({JSONObject.class})
public class JsonObjectTypeHandler extends BaseTypeHandler<JSONObject> {
private static final PGobject jsonObject = new PGobject();
@Override
public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException {
jsonObject.setType("jsonb");
jsonObject.setValue(parameter.toString());
ps.setObject(i, jsonObject);
}
@Override
public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {
return JSONObject.fromObject(rs.getString(columnName));
}
@Override
public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return JSONObject.fromObject(rs.getString(columnIndex));
}
@Override
public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return JSONObject.fromObject(cs.getString(columnIndex));
}
}
自定义新增sql
<insert id="*****"
parameterType="******">
insert into *** (org_id ,warehouse_register_api_request)
values (#{orgId,jdbcType=VARCHAR},
#{warehouseRegisterApiRequest,jdbcType=OTHER,typeHandler=com.evisible.storage.cloud.api.typehandlers.JsonObjectTypeHandler}
)
</insert>
自定义 更新sql
<update id="****" parameterType="****">
update ***
set last_update_time = #{lastUpdateTime,jdbcType=VARCHAR}
<if test="warehouseRegisterApiRequest != null">
,warehouse_register_api_request = #{warehouseRegisterApiRequest,jdbcType=OTHER,
typeHandler=com.evisible.storage.cloud.api.typehandlers.JsonObjectTypeHandler}
</if>
where id = #{id,jdbcType=INTEGER}
</update>
自定义 查询结果
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="***">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="orgId" column="org_id" jdbcType="VARCHAR"/>
<result property="warehouseRegisterApiRequest" column="warehouse_register_api_request" jdbcType="OTHER"
typeHandler="com.evisible.storage.cloud.api.typehandlers.JsonObjectTypeHandler"/>
</resultMap>
4、将集合作为字段存进数据库
数据库:postgresql 存储为jsonb数据
实体
/**
* 仓库附件信息
*/
@ApiModelProperty(name = "仓库附件信息", notes = "仓库附件信息", example = "仓库附件信息")
@TableField("attachments")
private JSONArray attachments;
自定义的 TypeHandler (这里用的是fastjson)
@MappedJdbcTypes({JdbcType.OTHER})
@MappedTypes({JSONArray.class})
public class JsonArrayTypeHandler extends BaseTypeHandler<JSONArray> {
private static final PGobject jsonObject = new PGobject();
@Override
public void setNonNullParameter(PreparedStatement ps, int i, JSONArray parameter, JdbcType jdbcType) throws SQLException {
jsonObject.setType("jsonb");
jsonObject.setValue(parameter.toString());
ps.setObject(i, jsonObject);
}
@Override
public JSONArray getNullableResult(ResultSet rs, String columnName) throws SQLException {
return JSONArray.parseArray(rs.getString(columnName));
}
@Override
public JSONArray getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return JSONArray.parseArray(rs.getString(columnIndex));
}
@Override
public JSONArray getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return JSONArray.parseArray(cs.getString(columnIndex));
}
}
自定义新增sql、自定义更新sql、自定义查询结果 和对象操作一致