关于做公司网站建设你应该知道的,青海最新消息今天,贵州定制型网站建设,中建装饰集团有限公司官网pgsql是在9.2版本中引入了对于json的支持 一般情况下使用到json数据格式的时候#xff0c;99%都是查询#xff0c;所以接下来看一下项目中对于json类型的数据是如何查询的
定义表
CREATE TABLE tv_sup_sl_dw_query (id varchar(200) NOT NULL,sup_record_id varchar(100) …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,//表中的数据类型就是jsonbparty_sup_other_json jsonb
);对应的实体模型
TableDefine(name tv_sup_sl_dw_query, abstractCol _abstract_col)
public class TV_SUP_DW_QUERY_DS {ColumnDefine(length200,pktrue)protected String id;ColumnDefine(length100)protected String sup_record_id;protected Date create_time;ColumnDefine(length100)protected String create_org_id;ColumnDefine(length100)protected String create_org_name;ColumnDefine(length 100)protected String sp_type;ColumnDefine(length100)protected String party_id;ColumnDefine(length100)protected String party_name;ColumnDefine(length100)protected String party_org_type;ColumnDefine(length100)protected String party_sup_from;ColumnDefine(length100)protected String party_sup_from_key;ColumnDefine(sqlTypeDataSetDefine.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 ListMapString,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 11 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 11 and ps - sex 1 and ps - name like %立%)
-- ps(sex text,name text) 这里必须要写条件中使用到了那个字段这里就必须要定义可以定义多个但不能少比如ps(sex text,name textphone 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其他函数可以自行百度或问文心一言