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'

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

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值