将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>
相关推荐
vigor51210 小时前
MySQL通过Mango实现分库分表
android·数据库·mysql
weixin1997010801610 小时前
[特殊字符]《京东订单API(jd.order.detail.get)对接ERP:企业认证+OAuth授权避坑指南》(附Python源码)
java·数据库·python
赵渝强老师10 小时前
【赵渝强老师】高斯数据库(openGauss)的模式
数据库·opengauss·国产数据库·高斯数据库
长不胖的路人甲10 小时前
Redis 数据删除策略
数据库·redis·spring
ClouGence10 小时前
SQL Server CDC 如何降低主库压力?Always On 备库读取实践
数据库·后端·sql·sqlserver
ShiXZ21311 小时前
PDF-OCR文件识别篇(七):数据入库
java·pdf·json·ocr·springboot
尽兴-11 小时前
Redis 为什么快?
数据库·redis·内存
林澈在路上11 小时前
最新版权清晰 AI音乐写歌工具软件App推荐 商用全场景实测指南
数据库·人工智能·ai·aigc·音频
Full Stack Developme11 小时前
正则表达式的使用教程
java·数据库·正则表达式
大郭鹏宇12 小时前
MongoDB快速实战与基本原理入门
数据库·mongodb