Skip to main content

postgresql基础

huhxLess than 1 minute

Array和Object类型的存储

需求:想要在数据库字段中存储对象或者是数组

postgresql表的语句

create table days_matter
(
    id    BIGINT PRIMARY KEY,
    cycle JSONB NOT NULL
);

spring中引入依赖

implementation 'io.hypersistence:hypersistence-utils-hibernate-62:3.6.0'

entity对象

public class DaysMatter {

    @Type(JsonType.class)
    private Cycle cycle;
}

public record Cycle(int every, CycleUnit unit) {

}

enum CycleUnit {
    NONE, DAY, WEEK, MONTH, YEAR
}

数据库中的数据

{"unit": "YEAR", "every": 1}

同理对于Set<Long>来说,如下:

sql
create table baby_resource 
(
    stars      JSONB,
)

将 varchar 类型转换成 bigint,而且数据做迁移

alter table family_album alter column id type bigint using id::bigint