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


    目录

    背景

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

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

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

    四、定义Mybatis-plus实体

    五、定义mapper查询器

    六、定义service业务类

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


    背景

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

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

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

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

    1. <dependency>
    2. <groupId>net.postgisgroupId>
    3. <artifactId>postgis-jdbcartifactId>
    4. <version>2.5.0version>
    5. dependency>

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

    1. package com.hngtghy.framework.handler;
    2. import java.sql.CallableStatement;
    3. import java.sql.PreparedStatement;
    4. import java.sql.ResultSet;
    5. import java.sql.SQLException;
    6. import org.apache.ibatis.type.BaseTypeHandler;
    7. import org.apache.ibatis.type.JdbcType;
    8. import org.apache.ibatis.type.MappedTypes;
    9. import org.postgis.Geometry;
    10. import org.postgis.PGgeometry;
    11. @MappedTypes({String.class})
    12. public class PgGeometryTypeHandler extends BaseTypeHandler {
    13. @Override
    14. public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
    15. PGgeometry pGgeometry = new PGgeometry(parameter);
    16. Geometry geometry = pGgeometry.getGeometry();
    17. geometry.setSrid(4326);
    18. ps.setObject(i, pGgeometry);
    19. }
    20. @Override
    21. public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
    22. String string = rs.getString(columnName);
    23. return getResult(string);
    24. }
    25. @Override
    26. public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    27. String string = rs.getString(columnIndex);
    28. return getResult(string);
    29. }
    30. @Override
    31. public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    32. String string = cs.getString(columnIndex);
    33. return getResult(string);
    34. }
    35. private String getResult(String string) throws SQLException {
    36. PGgeometry pGgeometry = new PGgeometry(string);
    37. String s = pGgeometry.toString();
    38. return s.replace("SRID=4326;", "");
    39. }
    40. }

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

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

    1. create table biz_point_test(
    2. id int8 primary key,
    3. name varchar(100),
    4. geom geometry(Point,4326)
    5. );

    四、定义Mybatis-plus实体

    1. package com.hngtghy.project.extend.student.domain;
    2. import com.baomidou.mybatisplus.annotation.TableField;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import com.baomidou.mybatisplus.annotation.TableName;
    5. import com.hngtghy.framework.handler.PgGeometryTypeHandler;
    6. import lombok.AllArgsConstructor;
    7. import lombok.Getter;
    8. import lombok.NoArgsConstructor;
    9. import lombok.Setter;
    10. import lombok.ToString;
    11. @TableName(value ="biz_point_test",autoResultMap = true)
    12. @NoArgsConstructor
    13. @AllArgsConstructor
    14. @Setter
    15. @Getter
    16. @ToString
    17. public class PointTest {
    18. @TableId
    19. private Long id;
    20. private String name;
    21. @TableField(typeHandler = PgGeometryTypeHandler.class)
    22. private String geom;
    23. @TableField(exist=false)
    24. private String geomJson;
    25. }

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

    五、定义mapper查询器

    1. package com.hngtghy.project.extend.student.mapper;
    2. import org.apache.ibatis.annotations.Param;
    3. import org.apache.ibatis.annotations.Select;
    4. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    5. import com.hngtghy.project.extend.student.domain.PointTest;
    6. public interface PointTestMapper extends BaseMapper{
    7. static final String FIND_GEOJSON_SQL="";
    8. @Select(FIND_GEOJSON_SQL)
    9. PointTest findGeoJsonById(@Param("id")Long id,@Param("name")String name);
    10. }

    六、定义service业务类

    1. package com.hngtghy.project.extend.student.service.impl;
    2. import java.util.List;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.stereotype.Service;
    5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    7. import com.hngtghy.project.extend.student.domain.PointTest;
    8. import com.hngtghy.project.extend.student.mapper.PointTestMapper;
    9. import com.hngtghy.project.extend.student.service.IPointTestService;
    10. @Service
    11. public class PointTestServcieImpl extends ServiceImpl implements IPointTestService{
    12. @Autowired
    13. private PointTestMapper pointMapper;
    14. @Override
    15. public PointTest selectById(Long id) {
    16. return pointMapper.selectById(id);
    17. }
    18. @Override
    19. public List selectList(PointTest point) {
    20. QueryWrapper queryWrapper = new QueryWrapper();
    21. queryWrapper.select("id,name");
    22. return this.getBaseMapper().selectList(queryWrapper);
    23. }
    24. @Override
    25. public int insertPointTest(PointTest point) {
    26. return pointMapper.insert(point);
    27. }
    28. @Override
    29. public int updatePoint(PointTest point) {
    30. return pointMapper.updateById(point);
    31. }
    32. @Override
    33. public PointTest selectGeomById(Long id) {
    34. QueryWrapper queryWrapper = new QueryWrapper();
    35. queryWrapper.select("geom","st_asgeojson(geom) as geomJson");
    36. queryWrapper.eq("id", id);
    37. return this.getBaseMapper().selectOne(queryWrapper);
    38. }
    39. @Override
    40. public PointTest findGeoJsonById(Long id) {
    41. return pointMapper.findGeoJsonById(id, null);
    42. }
    43. }

           这里添加了一个数据库不存在的字段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.262605 27.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可以查看到相应的点数据,如下图所示:

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

     

  • 相关阅读:
    Java高级---网络编程
    自定义函数
    oracle在where条件中关于索引字段的使用注意事项
    【JAVA】集合与背后的逻辑框架,包装类,List,Map,Set,静态内部类
    2022款Thinkphp家政上门预约系统-全开源系统源码
    android 如何使用aidl编译CPP接口
    【英语:基础高阶_全场景覆盖表达】K12.口语主题陈述——教育类
    一文讲清楚密评中的数据库存储加密 安当加密
    设计模式-策略模式
    从0开始学人工智能测试节选:Spark -- 结构化数据领域中测试人员的万金油技术(二)
  • 原文地址:https://blog.csdn.net/yelangkingwuzuhu/article/details/126364080