将JSON的格式数据存储到数据库中

将JSON的格式数据存储到数据库中,在一些业务中需要存储一个设备的一些属性。比如 这样以键值对出现的形式,存储在数据库中。我们可以以JSON的格式存储。

数据库字段设计的时候选择JSON类型的。

对于需要存储的内容用Map集合进行存储

java 复制代码
Map<String, Object> jsonData = new HashMap<>();
for (int i = 0; i < attributes.size(); i++) {
    Attribute att = attributes.get(i);
    jsonData.put(att.getAttrKey(),att.getAttrValue());
}

使用FastJson将map转为JSON的字符串。然后插入到数据库中就可以了。

java 复制代码
String  json = (String) JSONObject.toJSONString(jsonData);

mapper.xml中的语句

java 复制代码
  <insert id="insertBatchRows" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into tb_voice_raw (device_id, device_type, raw_data_path, status, created_at, voice_name, source,parameters)
        values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.deviceId}, #{item.deviceType}, #{item.rawDataPath}, #{item.status}, #{item.createdAt}, #{item.voiceName}, #{item.source},#{item.paramsAsJson})
        </foreach>
    </insert>
相关推荐
may_一一6 分钟前
MongoDB数据查看(Studio 3T)
数据库·mongodb
一个天蝎座 白勺 程序猿1 小时前
SQL Server数据库迁移实测:金仓KES V9R4C019让存量T-SQL“少量修改”
数据库·sql·kingbasees
嘻哈baby1 小时前
FastAPI + SQLite 从 0 写一个真正能用的待办 API
数据库·sqlite·fastapi
judezh1 小时前
「可重放」不是形容词:我把自家 Agent 运行时的 run 账本翻了个底朝天
数据库·人工智能
用户9020876583761 小时前
从原生 PG 到 Vastbase、GaussDB:聊透“Patroni + ETCD”高可用架构演进与实战
数据库
落木萧萧8251 小时前
MyBatis 启动的时候都在干什么:从 MappedStatement 说起
java·数据库·后端
g10565591392 小时前
华为 OceanStor 基础使用入门指南
服务器·数据库·性能优化
潇凝子潇2 小时前
MySQL buffer pool 计算公式
数据库·mysql
广州灵眸科技有限公司2 小时前
瑞芯微(EASY EAI)RV1126B display
开发语言·数据库·人工智能·科技·嵌入式硬件
凌晨1682 小时前
MySQL:DML、DDL与TCL精讲
数据库·mysql·oracle