@Table('tbl_pojo')
public class Pojo{
@ColDefine(customType="jsonb",type=ColType.PS_JSON)
private String info;
}
问答
postgresql 如何定义pojo 其中类型有 jsonb和 bit(n)
10 回复
实际存储的数据结果是postgres=> select * from tbl_user;
id | name | password | _type | info
----------------------------------+------+----------+-------+-------------------------
e9e37abb85cc47b4a89a6188007c6f8e | name | password | 00001 | "{"a":1,"b":"b"}"
(1 row)
外面多了双引号,导致没法使用(pojo中用的是String)。
postgres=> select * from tbl_user;
id | name | password | _type | info
----------------------------------+------+----------+-------+-------------------------
e9e37abb85cc47b4a89a6188007c6f8e | name | password | 00001 | "{\"a\":1,\"b":\"b\"}"
(1 row)
示例,来源自 PsqlJsonAdaptor 的 javadoc
public class Pet {
public static Pet getInstance(ResultSet rs) throws SQLException {
// 需要把所有字段从 ResultSet 取出,不然该属性无法映射
Pet pet = new Pet();
pet.setId(rs.getInt("id"));
pet.setData(NutMap.WRAP(rs.getString("data")));
return pet;
}
@Id
private int id;
@ColDefine(customType = "json", type = ColType.PSQL_JSON)
private NutMap data;
// ... 省略后面代码,包括字段声明以及 getter 和 setter
}
public class Jone {
public static Jone(ResultSet rs) throws SQLException {
// 需要把所有字段从 ResultSet 取出,不然该属性无法映射
this.id = rs.getInt("id");
this.info = Json.fromJson(Information.class, rs.getString("info"));
}
@Id
private int id;
@ColDefine(customType = "json", type = ColType.PSQL_JSON)
private Information info;
// ... 省略后面代码,包括字段声明以及 getter 和 setter
}
1.r.54 支持字段自定义适配器了,这样任意类型都可以支持的
来自炫酷的 NutzCN
添加回复
请先登陆