基于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,结合前端可视化组件即可完成矢量数据的空间可视化。希望本文可以帮到你,欢迎交流。

目录
相关文章
|
3月前
|
关系型数据库 MySQL 数据库
往数据库的字段varchar 或datetime里存时间值
往数据库的字段varchar 或datetime里存时间值
118 0
|
3月前
|
SQL Java 数据库连接
Mybatis和MybatisPlus:数据库操作工具的对比
Mybatis和MybatisPlus:数据库操作工具的对比
150 0
|
15天前
|
SQL XML Java
【mybatis】第二篇:@Select注解中加入字段判断
【mybatis】第二篇:@Select注解中加入字段判断
|
21天前
|
存储 关系型数据库 MySQL
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
|
5天前
|
SQL 关系型数据库 MySQL
mysql 数据库查询 查询字段用逗号隔开 关联另一个表并显示
mysql 数据库查询 查询字段用逗号隔开 关联另一个表并显示
17 2
|
15天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
27天前
|
Java 数据库连接 mybatis
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
mybatis plus字段为null或空字符串把原来的数据也更新了,只需要注解
17 0
|
28天前
|
SQL 关系型数据库 MySQL
Mysql数据库一个表字段中存了id,并以逗号分隔,id对应的详细信息在另一个表中
Mysql数据库一个表字段中存了id,并以逗号分隔,id对应的详细信息在另一个表中
10 0
|
1月前
|
Oracle Java 关系型数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
SpringBoot整合Mybatis连接Oracle数据库
|
1月前
|
缓存 Java 数据库连接
mybatis 数据库缓存的原理
MyBatis 是一个流行的 Java 持久层框架,它封装了 JDBC,使数据库交互变得更简单、直观。MyBatis 支持两级缓存:一级缓存(Local Cache)和二级缓存(Global Cache),通过这两级缓存可以有效地减少数据库的访问次数,提高应用性能。
282 1

热门文章

最新文章