当前位置: 首页 > article >正文

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

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

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

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

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的字符串。然后插入到数据库中就可以了。

String  json = (String) JSONObject.toJSONString(jsonData);

mapper.xml中的语句

  <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>

http://www.kler.cn/news/343405.html

相关文章:

  • Jetpack Compose 页面跳转 - 导航Navigation使用和封装
  • 共识算法Raft
  • 【新书】使用生成式人工智能和Python开始数据分析
  • 回归涉及的函数
  • C语言-了解程序环境和预处理看这一篇(超详解)
  • std::future::then的概念和使用方法
  • Java SSL使用Openssl ECC加密生成证书遇到的坑
  • Python和C++及MATLAB低温磁态机器学习模型
  • 【解决办法】git clone报错unable to access ‘xxx‘: SSL certificate problem
  • 第19周JavaWeb编程实战-MyBatis实现OA系统 面试题解析
  • Go语言学习代码记录
  • C++继承深度剖析:从理论到实践的跨越
  • rzszscp端对端文件传输整理
  • 【SpringSecurity】基本流程
  • 职场上的人情世故你知多少
  • VARCHAR(50) 与 CHAR(50) 的区别
  • 活动预告|博睿数据将受邀出席GOPS全球运维大会上海站!
  • KVM虚拟化技术
  • 启动hadoop后没有 NodeManager和 ResourceManager
  • Spring Boot RESTful API开发教程