雖然很晚的答案(用於插入的部分),我希望這可能是有用別人:
取一個HashMap中的鍵/值對:
Map<String, String> hstoreMap = new HashMap<>();
hstoreMap.put("key1", "value1");
hstoreMap.put("key2", "value2");
PGobject jsonbObj = new PGobject();
jsonbObj.setType("json");
jsonbObj.setValue("{\"key\" : \"value\"}");
使用下列方法之一將它們插入到PostgreSQL:
1)
jdbcTemplate.update(conn -> {
PreparedStatement ps = conn.prepareStatement("INSERT INTO table (hstore_col, jsonb_col)");
ps.setObject(1, hstoreMap);
ps.setObject(2, jsonbObj);
});
2)
jdbcTemplate.update("INSERT INTO table (hstore_col, jsonb_col) VALUES(?,?)",
new Object[]{ hstoreMap, jsonbObj }, new int[]{Types.OTHER, Types.OTHER});
3) 集hstoreMap/jsonbObj在POJO (Map類型的hstoreCol和jsonbObjCol類型爲PGObject)
BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(POJO);
sqlParameterSource.registerSqlType("hstore_col", Types.OTHER);
sqlParameterSource.registerSqlType("jsonb_col", Types.OTHER);
namedJdbcTemplate.update("INSERT INTO table (hstore_col, jsonb_col) VALUES (:hstoreCol, :jsonbObjCol)", sqlParameterSource);
而得到的值:
(Map<String, String>) rs.getObject("hstore_col"));
((PGobject) rs.getObject("jsonb_col")).getValue();
奇怪的是,它仍然沒有爲我工作。升級到最新的postgres版本驅動程序修復了這個問題。 – linqu