基于Mybatis-Plus实现Geometry字段在PostGis空间数据库中的使用

简介: 本文讲解在mybatis-plus中操作geometry空间字段,同时实现查询和插入操作​。通过geojson,结合前端可视化组件即可完成​矢量数据的空间可视化。

背景

      之前的一些个人文章介绍了空间数据库,以及Mybatis-Plus快速操作数据库组件,以及空间数据库PostGis的相关介绍。现在基于在空间数据库中已经定义了一张空间表,需要在应用程序中使用Mybatis-Plus来进行空间数据的查询、插入等常规操作。

      在OGC标准中,通常空间字段是由Geometry类型来表示。而一般编程语言中是没有这种数据类型的。以java为例,怎么操作这些数据,满足业务需求呢?跟着本文一起来学习吧。

     今天介绍基于postgis-jdbc的geometry属性的操作。

一、在pom.xml中引入postgis-jdbc相关jar包

<!-- PostgreSql 驱动包 add by wuzuhu on 2022-08-16 --><dependency><groupId>net.postgis</groupId><artifactId>postgis-jdbc</artifactId><version>2.5.0</version></dependency>

二、需要自定义Handler类来扩展字段支持。

packagecom.hngtghy.framework.handler;
importjava.sql.CallableStatement;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importorg.apache.ibatis.type.BaseTypeHandler;
importorg.apache.ibatis.type.JdbcType;
importorg.apache.ibatis.type.MappedTypes;
importorg.postgis.Geometry;
importorg.postgis.PGgeometry;
@MappedTypes({String.class})
publicclassPgGeometryTypeHandlerextendsBaseTypeHandler<String> {
@OverridepublicvoidsetNonNullParameter(PreparedStatementps, inti, Stringparameter, JdbcTypejdbcType) throwsSQLException {
PGgeometrypGgeometry=newPGgeometry(parameter);
Geometrygeometry=pGgeometry.getGeometry();
geometry.setSrid(4326);
ps.setObject(i, pGgeometry);
    }
@OverridepublicStringgetNullableResult(ResultSetrs, StringcolumnName) throwsSQLException {
Stringstring=rs.getString(columnName);
returngetResult(string);
    }
@OverridepublicStringgetNullableResult(ResultSetrs, intcolumnIndex) throwsSQLException {
Stringstring=rs.getString(columnIndex);
returngetResult(string);
    }
@OverridepublicStringgetNullableResult(CallableStatementcs, intcolumnIndex) throwsSQLException {
Stringstring=cs.getString(columnIndex);
returngetResult(string);
    }
privateStringgetResult(Stringstring) throwsSQLException {
PGgeometrypGgeometry=newPGgeometry(string);
Strings=pGgeometry.toString();
returns.replace("SRID=4326;", "");
    }
}

          注意,在getResult()中关于4326坐标系的定义,可以根据需要进行废弃。这里写上为了统一投影坐标系。

三、在数据中创建表,建表语句如下:

createtable biz_point_test(  id int8 primary key,  name varchar(100),  geom geometry(Point,4326));

四、定义Mybatis-plus实体

packagecom.hngtghy.project.extend.student.domain;
importcom.baomidou.mybatisplus.annotation.TableField;
importcom.baomidou.mybatisplus.annotation.TableId;
importcom.baomidou.mybatisplus.annotation.TableName;
importcom.hngtghy.framework.handler.PgGeometryTypeHandler;
importlombok.AllArgsConstructor;
importlombok.Getter;
importlombok.NoArgsConstructor;
importlombok.Setter;
importlombok.ToString;
@TableName(value="biz_point_test",autoResultMap=true)
@NoArgsConstructor@AllArgsConstructor@Setter@Getter@ToStringpublicclassPointTest {
@TableIdprivateLongid;
privateStringname;
@TableField(typeHandler=PgGeometryTypeHandler.class)
privateStringgeom;
@TableField(exist=false)
privateStringgeomJson;
}

     提醒:1、在属性上使用@TableField(typeHandler=xxx)来指定对应的类型转换器。2、需要在实体上定义autoResultMap=true。否则配置不一定生效。

五、定义mapper查询器

packagecom.hngtghy.project.extend.student.mapper;
importorg.apache.ibatis.annotations.Param;
importorg.apache.ibatis.annotations.Select;
importcom.baomidou.mybatisplus.core.mapper.BaseMapper;
importcom.hngtghy.project.extend.student.domain.PointTest;
publicinterfacePointTestMapperextendsBaseMapper<PointTest>{
staticfinalStringFIND_GEOJSON_SQL="<script>"+"select st_asgeojson(geom) as geomJson from biz_point_test "+"where id = #{id} "+"<if test='null != name'>and p.name like concat('%', #{name}, '%')</if>"+"</script>";
@Select(FIND_GEOJSON_SQL)
PointTestfindGeoJsonById(@Param("id")Longid,@Param("name")Stringname);
}

六、定义service业务类

packagecom.hngtghy.project.extend.student.service.impl;
importjava.util.List;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
importcom.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
importcom.hngtghy.project.extend.student.domain.PointTest;
importcom.hngtghy.project.extend.student.mapper.PointTestMapper;
importcom.hngtghy.project.extend.student.service.IPointTestService;
@ServicepublicclassPointTestServcieImplextendsServiceImpl<PointTestMapper, PointTest>implementsIPointTestService{
@AutowiredprivatePointTestMapperpointMapper;
@OverridepublicPointTestselectById(Longid) {
returnpointMapper.selectById(id);
    }
@OverridepublicList<PointTest>selectList(PointTestpoint) {
QueryWrapper<PointTest>queryWrapper=newQueryWrapper<PointTest>();
queryWrapper.select("id,name");
returnthis.getBaseMapper().selectList(queryWrapper);
    }
@OverridepublicintinsertPointTest(PointTestpoint) {
returnpointMapper.insert(point);
    }
@OverridepublicintupdatePoint(PointTestpoint) {
returnpointMapper.updateById(point);
    }
@OverridepublicPointTestselectGeomById(Longid) {
QueryWrapper<PointTest>queryWrapper=newQueryWrapper<PointTest>();
queryWrapper.select("geom","st_asgeojson(geom) as geomJson");
queryWrapper.eq("id", id);
returnthis.getBaseMapper().selectOne(queryWrapper);
    }
@OverridepublicPointTestfindGeoJsonById(Longid) {
returnpointMapper.findGeoJsonById(id, null);
    }
}

      这里添加了一个数据库不存在的字段geomJson,会将空间属性转变成geojson字段,方便于前台的如leaflet、openlayers、cesium等组件进行展示。所以使用postgis的st_asgeojson(xxx)进行函数转换。

七、相关方法调用

//1、列表查询
List<PointTest> pointList = pointService.selectList(null);System.out.println(pointList);[PointTest(id=1559371184090423297, name=中寨居委会, geom=null, geomJson=null), PointTest(id=2, name=禾滩村, geom=null, geomJson=null), PointTest(id=1559403683801796610, name=中寨居委会, geom=null, geomJson=null)]//2、插入
PointTest point = new PointTest();point.setName("中寨居委会");point.setGeom("POINT(109.262605 27.200669)");//POINT(lng,lat) 经度,纬度
pointService.insertPointTest(point);//3、查询数据
 PointTest point = pointService.selectGeomById(1559371184090423297L); PointTest json = pointService.findGeoJsonById(1559371184090423297L);PointTest(id=null, name=null, geom=POINT(109.26260527.200669), geomJson={"type":"Point","coordinates":[109.262605,27.200669]})PointTest(id=null, name=null, geom=null, geomJson={"type":"Point","coordinates":[109.262605,27.200669]})

八、使用pgadmin可以查看到相应的点数据,如下图所示:

image.png

image.png

      总结:通过以上步骤可以实现在mybatis-plus中操作geometry空间字段,同时实现查询和插入操作。通过geojson,结合前端可视化组件即可完成矢量数据的空间可视化。希望本文可以帮到你,欢迎交流。

目录
相关文章
|
24天前
|
SQL 关系型数据库 MySQL
解决:Mybatis-plus向数据库插入数据的时候 报You have an error in your SQL syntax
该博客文章讨论了在使用Mybatis-Plus向数据库插入数据时遇到的一个常见问题:SQL语法错误。作者发现错误是由于数据库字段中使用了MySQL的关键字,导致SQL语句执行失败。解决方法是将这些关键字替换为其他字段名称,以避免语法错误。文章通过截图展示了具体的操作步骤。
|
29天前
|
数据库
实体类的字段和数据库中的字段不一致、并且没有做中间替换、会发生什么
这篇文章讨论了实体类字段与数据库字段不一致时可能导致的问题,作者通过实际案例展示了字段不匹配时查询无法正确执行,并说明了修正字段匹配后查询可以成功执行的情况。
实体类的字段和数据库中的字段不一致、并且没有做中间替换、会发生什么
|
2月前
|
Java 数据库连接 数据库
mybatis plus 更新值为null的字段
mybatis plus 更新值为null的字段
41 7
|
27天前
|
存储 开发框架 .NET
ASP.NET Web Api 使用 EF 6,DateTime 字段如何取数据库服务器当前时间
ASP.NET Web Api 使用 EF 6,DateTime 字段如何取数据库服务器当前时间
|
27天前
|
druid Java 数据库连接
SpringBoot项目整合MybatisPlus持久层框架+Druid数据库连接池,以及实现增删改查功能
SpringBoot项目整合MybatisPlus和Druid数据库连接池,实现基本的增删改查功能。
140 0
|
2月前
|
数据库
MybatisPlus3---常用注解,驼峰转下滑线作为表明 cteateTime 数据表中的 cteate_time,@TableField,与数据库字段冲突要使用转义字符“`order`“,is
MybatisPlus3---常用注解,驼峰转下滑线作为表明 cteateTime 数据表中的 cteate_time,@TableField,与数据库字段冲突要使用转义字符“`order`“,is
|
13天前
|
Java 数据库连接 测试技术
SpringBoot 3.3.2 + ShardingSphere 5.5 + Mybatis-plus:轻松搞定数据加解密,支持字段级!
【8月更文挑战第30天】在数据驱动的时代,数据的安全性显得尤为重要。特别是在涉及用户隐私或敏感信息的应用中,如何确保数据在存储和传输过程中的安全性成为了开发者必须面对的问题。今天,我们将围绕SpringBoot 3.3.2、ShardingSphere 5.5以及Mybatis-plus的组合,探讨如何轻松实现数据的字段级加解密,为数据安全保驾护航。
50 1
|
22天前
|
Web App开发 前端开发 关系型数据库
基于SpringBoot+Vue+Redis+Mybatis的商城购物系统 【系统实现+系统源码+答辩PPT】
这篇文章介绍了一个基于SpringBoot+Vue+Redis+Mybatis技术栈开发的商城购物系统,包括系统功能、页面展示、前后端项目结构和核心代码,以及如何获取系统源码和答辩PPT的方法。
|
27天前
|
Java 关系型数据库 MySQL
1、Mybatis-Plus 创建SpringBoot项目
这篇文章是关于如何创建一个SpringBoot项目,包括在`pom.xml`文件中引入依赖、在`application.yml`文件中配置数据库连接,以及加入日志功能的详细步骤和示例代码。
|
6天前
|
Java 数据库连接 开发者
MyBatis-Plus整合SpringBoot及使用
MyBatis-Plus为MyBatis提供了强大的增强,使得在Spring Boot项目中的数据访问层开发变得更加快捷和简便。通过MyBatis-Plus提供的自动CRUD、灵活的查询构造器和简洁的配置,开发者
20 0