如何使用表和PostgreSQL JDBC驱动程序中的JSONB数据类型将JSON对象存储到PostgreSQL中 [英] How to store JSON object into PostgreSQL using JSONB data type inside table and PostgreSQL JDBC driver

查看:274
本文介绍了如何使用表和PostgreSQL JDBC驱动程序中的JSONB数据类型将JSON对象存储到PostgreSQL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下json对象保存为PostgreSQLb表 as jsonb

I want to save following json object into PostgreSQL db table as jsonb

{
  "fname":"john",
  "lname:"doe",
}

我正在使用PGObject创建对象并将类型设置为jsonb并将值作为json字符串传递给我

I am currenlty using PGObject to create object and set type to jsonb and pass value as json string

通过微导航数据和微导航寻找更好的方法 micronaut-data中是否支持任何本机数据类型,以将Java对象转换为JSON并存储在db中? 如何使用postgres jdbc驱动程序保存数据

Looking for a better approach with micronaut-data and micronaut Is there any native data type supported in micronaut-data to convert the Java object to JSON and store in db? How to save the data using postgres jdbc driver

如果使用:jsonb在查询中进行类型转换,如果与micronaut-data/predator一起使用时,已经尝试过使用原始的jdbc?

推荐答案

我们可以使用PGObject和Jackson对象映射器将json保存在Postgres中

We can use PGObject and Jackson Object mapper to save the json in Postgres

val person=PGobject()
person.type="jsonb"
person.value=ObjectMapper.writeValueAsString("{"fname":"john","lname":"doe"}")

现在可以将person对象传递给JDBC驱动程序以将数据存储为jsonb

now person object can be passed to JDBC driver for storing data as jsonb

这篇关于如何使用表和PostgreSQL JDBC驱动程序中的JSONB数据类型将JSON对象存储到PostgreSQL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆