跳到主要内容

JSON 序列化处理器

JSON 序列化处理器位于 net.hasor.dbvisitor.types.handler.json 包中。通过序列化处理器可以将 Java 对象以 JSON 格式存储在数据库的文本字段中。

在对象映射中使用
public class User {
@Column(typeHandler = JsonTypeHandler.class)
private UserExtInfo moreInfo;

// getters and setters omitted
}
在 SQL 语句中使用
update users
set more_info = #{arg1, typeHandler=net.hasor.dbvisitor.types.handler.json.JsonTypeHandler}
where id = #{arg0}
信息

使用 JSON 序列化处理器前,需在项目中引入对应的 JSON 库依赖。

内置实现

JSON 序列化

类型处理器说明
JsonTypeHandler自动检测,按 Jackson → Gson → Fastjson → Fastjson2 顺序寻找可用的 JSON 库
JsonUseForJacksonTypeHandler使用 Jackson
JsonUseForGsonTypeHandler使用 Gson
JsonUseForFastjsonTypeHandler使用 Fastjson
JsonUseForFastjson2TypeHandler使用 Fastjson2

BSON 序列化

类型处理器说明
BsonTypeHandler使用 MongoDB BSON 库进行序列化/反序列化
BsonListTypeHandler基于 BSON 处理 ListSet 等集合类型字段,可自动识别字段泛型