Postgresql中的jsonb数据类型学习使用

pgsql是在9.2版本中引入了对于json的支持
一般情况下使用到json数据格式的时候,99%都是查询,所以接下来看一下项目中对于json类型的数据是如何查询的

定义表

CREATE TABLE tv_sup_sl_dw_query (
  id varchar(200)  NOT NULL,
  sup_record_id varchar(100)  default NULL,
  create_org_id varchar(100)  default NULL,
  create_org_name varchar(100)  default NULL,
  create_time timestamp(6),
  party_id varchar(100)  default NULL,
  party_name varchar(100)  default NULL,
  party_sup_from varchar(100)  default NULL,
  party_sup_from_key varchar(100)  default NULL,
  //表中的数据类型就是jsonb
  party_sup_other_json jsonb
);

对应的实体模型

@TableDefine(name = "tv_sup_sl_dw_query", abstractCol = "_abstract_col")
public class TV_SUP_DW_QUERY_DS {
   

    @ColumnDefine(length=200,pk=true)
    protected String id;
    @ColumnDefine(length=100)
    protected String sup_record_id;
    protected Date create_time;
    @ColumnDefine(length=100)
    protected String create_org_id;
    @ColumnDefine(length=100)
    protected String create_org_name;
    @ColumnDefine(length = 100)
    protected String sp_type;
    @ColumnDefine(length=100)
    protected String party_id;
    @ColumnDefine(length=100)
    protected String party_name;
    @ColumnDefine(length=100)
    protected String party_org_type;
    @ColumnDefine(length=100)
    protected String party_sup_from;
    @ColumnDefine(length=100)
    protected String party_sup_from_key;
    @ColumnDefine(sqlType=DataSetDefine.TYPE_JSONB)
    protected String party_sup_other_json;
    //TODO 省略构造方法,get/set方法
}
//这个PartySupRecordOtherJsonDTO 就是party_sup_other_json存储的内容,将PartySupRecordOtherJsonDTO 对象转成json字符串赋值给party_sup_other_json即可
public class PartySupRecordOtherJsonDTO {
   
    
    private String process_type;
    private String q_process_type;
    private String content_abstract;
    private String comment;
    private String handle_opinion;
    private String disput_content;
    private String work_plan;
    private int need_blqkbg;
    private int l_uploadsj;
    private String childPartyIds;
    private String childPartyNames;
    private String hasMoreChildParties;
    private String childPartyBlqkbg;
    private List<Map<String,Object>> persons;
}

对于简单数据类型的查询

select id from tv_sup_sl_dw_query dw where dw.party_sup_other_json ->> 'childPartyBlqkbg'!='1'

一下这些sql 都是在Java代码中写的

对于复杂数据类型的查询

--jsonb_array_elements  这个函数就是将persons集合转成一个对象数组
select id from tv_sup_sl_dw_query dw where dw.party_sup_other_json ->> 'persons' is not null and dw.party_sup_other_json ->> 'persons' != '[]' and exists (select 1 from jsonb_array_elements ( dw.party_sup_other_json -> 'persons') ps where 1=1 and ps ->> 'sex' = '1' and ps ->> 'name' like '%立%')

--这里的jsonb_array_elements  也可以换成 jsonb_to_recordset函数
select id from tv_sup_sl_dw_query dw where dw.party_sup_other_json ->> 'persons' is not null and dw.party_sup_other_json ->> 'persons' != '[]' and exists (select 1 from jsonb_to_recordset( dw.party_sup_other_json -> 'persons') ps(sex text,name text) where 1=1 and ps ->> 'sex' = '1' and ps ->> 'name' like '%立%')
-- ps(sex text,name text) 这里必须要写,条件中使用到了那个字段,这里就必须要定义,可以定义多个,但不能少,比如ps(sex text,name text,phone text) 这样的。如果少定义了则会报错。这里的字段就是数据库表中的字段,text 代表字符串,integer 代表int

其他一些函数的使用

select id from tv_sup_sl_dw_query dw where dw.date_part('day',tv.create_time\:\:timestamp - :nowDate \:\:timestamp) >= 2
-- date_part这个函数是获取两个时间之间的天数

jsonb中的数据也可以获取,作为查询列

select id,dw.party_sup_other_json ->> 'comment',dw.party_sup_other_json ->> 'content_abstract' from tv_sup_sl_dw_query where dw.create_time >= '2024-01-01 00:00:00'

其他函数可以自行百度或问文心一言

相关推荐

  1. Postgresqljsonb数据类型学习使用

    2024-01-25 06:32:01       55 阅读
  2. PostgreSQLJson数据类型如何使用

    2024-01-25 06:32:01       22 阅读
  3. MyBatis:PostGreSQLjsonb类型处理器

    2024-01-25 06:32:01       28 阅读
  4. postgresqlgeometry类型数据迁移

    2024-01-25 06:32:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 06:32:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 06:32:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 06:32:01       82 阅读
  4. Python语言-面向对象

    2024-01-25 06:32:01       91 阅读

热门阅读

  1. 解释LoRA参数

    2024-01-25 06:32:01       56 阅读
  2. nlp文本主题提取算法总结

    2024-01-25 06:32:01       57 阅读
  3. 开源元数据管理平台Amundsen安装

    2024-01-25 06:32:01       60 阅读
  4. #Uniapp:map地图组件

    2024-01-25 06:32:01       49 阅读
  5. tomcat与Apache---一起学习吧之服务器

    2024-01-25 06:32:01       61 阅读
  6. 网络原理——应用层

    2024-01-25 06:32:01       43 阅读
  7. freeswitch中通过嵌入式脚本监听会议事件

    2024-01-25 06:32:01       50 阅读