建表

-- ----------------------------
-- Table structure for file_attribute_info
-- ----------------------------
DROP TABLE IF EXISTS "public"."file_attribute_info";
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
CREATE TABLE "public"."file_attribute_info" (
"id" int8 NOT NULL GENERATED ALWAYS AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
),
"file_key" varchar(500) COLLATE "pg_catalog"."default",
"attribute" json,
"geometry" geometry,
"time" timestamp(6)
)
;

-- ----------------------------
-- Primary Key structure for table file_attribute_info
-- ----------------------------
ALTER TABLE "public"."file_attribute_info" ADD CONSTRAINT "file_attribute_info_pkey" PRIMARY KEY ("id");

注意先安装插件配置
  1. 引入postgis-jdbc包
    <dependency>
<groupId>net.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>2.5.0</version>
</dependency>

  1. 创建类型转换类MyGeometryTypeHandler

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.postgis.PGgeometry;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes({String.class})
public class MyGeometryTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
PGgeometry pGgeometry = new PGgeometry(parameter);
ps.setObject(i, pGgeometry);
}

@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
PGgeometry pGgeometry = new PGgeometry(rs.getString(columnName));
if (pGgeometry == null) {
return null;
}
return pGgeometry.toString();
}

@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
PGgeometry pGgeometry = new PGgeometry(rs.getString(columnIndex));
if (pGgeometry == null) {
return null;
}
return pGgeometry.toString();
}

@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {

PGgeometry pGgeometry = new PGgeometry(cs.getString(columnIndex));
if (pGgeometry == null) {
return null;
}
return pGgeometry.toString();
}
}

  1. 在实体类配置typeHandler
 @TableField(typeHandler = MyGeometryTypeHandler.class)
private String wkb;

查询语句:

SELECT ST_AsEWKT(file_attribute_info.geometry) from file_attribute_info

Mybatis-plus读取和保存geometry数据_postgresql 

 带json的查询

select id, file_key, "attribute", geometry, "time" from "file_attribute_info" WHERE file_key='de4768ce814f4ac9a46678e50e178e0d' and ATTRIBUTE #>>'{KIND_CODE }'='11'