mybatis映射postgres数据库的geometry类型
一、问题背景和需求描述
- 前端保存的geometry数据类型是json格式({“type”:“Point”,“coordinates”:[119.9556356,30.5469975]}类似)
- 数据库存储数据是16进制(经过st_geomfromgeojson(json)函数转换之后再存储的数据 01010000002C39382229FD5D40677E3507088C3E40)
- 后端java对象用string接受,需要再存储之前将json转16进制对象后再存储
- 查询时需要16进制数据转成json,返回前端
二、解决步骤
- 导入java类型转换需要的包
- 自定义mybatis类型转换器(GeometryTypeHandler)
- 注册类型转换器
- 再实体类添加类型转换器注解
1、导入java类型转换需要的包
<dependency>
<groupId>net.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>2023.1.0</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>