GeometryCollection 的类型映射器(TypeHandler)

打印 上一主题 下一主题

主题 871|帖子 871|积分 2613

by emanjusaka from  https://www.emanjusaka.top/2024/05/mybatis-typeHandler-geometryCollection 彼岸花开可奈何
本文欢迎分享与聚合,全文转载请留下原文地址。
GeometryCollection 是 GeoJSON 数据模型中的一个类型,用于表示一个几何对象的聚集。MySQL8 中支持了 GeometryCollection 类型,在对数据库和实体类举行对象映射时需要我们自己编写类型映射器来完成映射。java 本身不支持 GeometryCollection 类型,我们需要引入第三方包来获得支持。
引入geotools工具包

该依赖在 maven 中心堆栈中没有,需要另外配置下堆栈
geotools库支持 jdk8 的最高版本是 28.2。
  1. <dependency>
  2.             <groupId>org.geotools</groupId>
  3.             <artifactId>gt-geojson-core</artifactId>
  4.             <version>28.2</version>
  5.         </dependency>
  6. <repositories>
  7.         
  8.         <repository>
  9.             <id>osgeo</id>
  10.             <name>OSGeo Release Repository</name>
  11.             <url>https://repo.osgeo.org/repository/release/</url>
  12.             <snapshots>
  13.                 <enabled>false</enabled>
  14.             </snapshots>
  15.             <releases>
  16.                 <enabled>true</enabled>
  17.             </releases>
  18.         </repository>
  19.         <repository>
  20.             <id>osgeo-snapshot</id>
  21.             <name>OSGeo Snapshot Repository</name>
  22.             <url>https://repo.osgeo.org/repository/snapshot/</url>
  23.             <snapshots>
  24.                 <enabled>true</enabled>
  25.             </snapshots>
  26.             <releases>
  27.                 <enabled>false</enabled>
  28.             </releases>
  29.         </repository>
  30.         
  31.     </repositories>
复制代码
实体类

本示例的 mybatis 增加框架利用的是 tkmapper,如果是利用的 MyBatisPlus 注解切换成对应的注解,大同小异。为了演示方便我去除了无关字段,重点关注 GeometryCollection 类型的 areaGeom 字段即可。
  1. package top.emanjusaka.test;
  2. import com.iles.handler.GeometryCollectionTypeHandler;
  3. import lombok.Data;
  4. import lombok.EqualsAndHashCode;
  5. import org.locationtech.jts.geom.GeometryCollection;
  6. import tk.mybatis.mapper.annotation.ColumnType;
  7. import javax.persistence.Column;
  8. import javax.persistence.GeneratedValue;
  9. import javax.persistence.Id;
  10. import javax.persistence.Table;
  11. @Data
  12. @EqualsAndHashCode(callSuper = false)
  13. @Table(name = "car_work_area")
  14. public class CarWorkAreaDO {
  15.     private static final long serialVersionUID = 1L;
  16.     /**
  17.      * 自增 ID
  18.      */
  19.     @Id
  20.     @GeneratedValue(generator = "JDBC")
  21.     private Long id;
  22.     @Column(name = "area_geom")
  23.     @ColumnType(typeHandler = GeometryCollectionTypeHandler.class)
  24.     private GeometryCollection areaGeom;
  25. }
复制代码
TypeHandler 映射器

大多数支持地理空间数据的数据库系统(如 PostgreSQL/PostGIS、MySQL、SQL Server 等)都支持以 WKT 或 WKB 格式存储和检索 GeometryCollection 类型的数据。具体利用哪种格式取决于应用场景的需求,好比对存储空间的敏感度、数据交换的便捷性以及性能要求等。本文存储的是 WKT 格式。
  1. package top.emanjusaka.handler;
  2. import org.apache.ibatis.type.BaseTypeHandler;
  3. import org.apache.ibatis.type.JdbcType;
  4. import org.locationtech.jts.geom.GeometryCollection;
  5. import org.locationtech.jts.io.ParseException;
  6. import org.locationtech.jts.io.WKTReader;
  7. import org.locationtech.jts.io.WKTWriter;
  8. import java.sql.CallableStatement;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. /**
  13. * @Author emanjusaka
  14. */
  15. public class GeometryCollectionTypeHandler extends BaseTypeHandler<GeometryCollection> {
  16.     private static final WKTReader wktReader = new WKTReader();
  17.     @Override
  18.     public void setNonNullParameter(PreparedStatement ps, int i, GeometryCollection parameter, JdbcType jdbcType) throws SQLException {
  19.         ps.setString(i, parameter.toText());
  20.     }
  21.     @Override
  22.     public GeometryCollection getNullableResult(ResultSet rs, String columnName) throws SQLException {
  23.         String wkt = rs.getString(columnName);
  24.         try {
  25.             return wkt != null ? (GeometryCollection) wktReader.read(wkt) : null;
  26.         } catch (ParseException e) {
  27.             throw new RuntimeException(e);
  28.         }
  29.     }
  30.     @Override
  31.     public GeometryCollection getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  32.         String wkt = rs.getString(columnIndex);
  33.         try {
  34.             return wkt != null ? (GeometryCollection) wktReader.read(wkt) : null;
  35.         } catch (ParseException e) {
  36.             throw new RuntimeException(e);
  37.         }
  38.     }
  39.     @Override
  40.     public GeometryCollection getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  41.         String wkt = cs.getString(columnIndex);
  42.         try {
  43.             return wkt != null ? (GeometryCollection) wktReader.read(wkt) : null;
  44.         } catch (ParseException e) {
  45.             throw new RuntimeException(e);
  46.         }
  47.     }
  48. }
复制代码
mapper 文件
  1. package top.emanjusaka.mapper;
  2. import top.emanjusaka.test.CarWorkAreaDO;
  3. import org.apache.ibatis.annotations.Param;
  4. import tk.mybatis.mapper.common.Mapper;
  5. import java.util.List;
  6. public interface CarWorkAreaMapper extends Mapper<CarWorkAreaDO> {
  7.     /**
  8.      * 插入数据
  9.      *
  10.      * @param carWorkArea
  11.      * @return:
  12.      */
  13.     int insertCustom(CarWorkAreaDO carWorkArea);
  14. }
复制代码
注意事项:

  • resultMap 中 area_geom 字段注意配置 TypeHandler,否则无法举行映射导致报错
  • 插入的 sql 语句中,area_geom 字段也需要指定 typeHandler,并且需要调用 ST_GeomCollFrom()函数读取数据。
  • 查询时可以调用函数 AsTex()
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="top.emanjusaka.mapper.CarWorkAreaMapper">
  4.     <resultMap type="com.iles.models.car.CarWorkAreaDO" id="carWorkAreaResult">
  5.         <result property="id" column="id"/>
  6.         <result property="areaGeom" column="area_geom" javaType="org.locationtech.jts.geom.GeometryCollection"
  7.                 jdbcType="OTHER"
  8.                 typeHandler="top.emanjusaka.handler.GeometryCollectionTypeHandler"/>
  9.     </resultMap>
  10.     <insert id="insertCustom" parameterType="com.iles.models.car.CarWorkAreaDO">
  11.         insert into
  12.         car_work_area(id,area_geom)
  13.         values
  14.         (#{id},ST_GeomCollFromText(#{areaGeom,typeHandler=top.emanjusaka.handler.GeometryCollectionTypeHandler}))
  15.     </insert>
  16. </mapper>
复制代码
在技能的星河中遨游,我们互为引路星辰,共同追逐发展的光芒。愿本文的洞见能触动您的思绪,如有所共鸣,请以点赞之手,轻抚赞同的弦。
原文地址: https://www.emanjusaka.top/2024/05/mybatis-typeHandler-geometryCollection
微信公众号:emanjusaka的编程栈

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

没腿的鸟

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表