首页 > 数据库 >mybatis plus 将 mysql geometry类型字段进行解析

mybatis plus 将 mysql geometry类型字段进行解析

时间:2023-02-09 15:47:29浏览次数:45  
标签:GeoPoint return BigDecimal geometry mysql plus import new public

package org.jeecg.config.mybatis.interceptor;

import java.math.BigDecimal;

public class GeoPoint {

    public GeoPoint(BigDecimal lng, BigDecimal lat) {
        this.lng = lng;
        this.lat = lat;
    }

    /**
     * 经度
     */
    private BigDecimal lng;

    /**
     * 纬度
     */
    private BigDecimal lat;

    public BigDecimal getLng() {
        return lng;
    }

    public void setLng(BigDecimal lng) {
        this.lng = lng;
    }

    public BigDecimal getLat() {
        return lat;
    }

    public void setLat(BigDecimal lat) {
        this.lat = lat;
    }
}


package org.jeecg.config.mybatis.interceptor;

import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequenceFactory;
import com.vividsolutions.jts.io.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 地理空间转化工具
 *
 * @author 7788
 */
public class GeoPointConverter {

    /**
     * Little endian or Big endian
     */
    private int byteOrder = ByteOrderValues.LITTLE_ENDIAN;
    /**
     * Precision model
     */
    private PrecisionModel precisionModel = new PrecisionModel();
    /**
     * Coordinate sequence factory
     */
    private CoordinateSequenceFactory coordinateSequenceFactory = CoordinateArraySequenceFactory.instance();
    /**
     * Output dimension
     */
    private int outputDimension = 2;

    /**
     * Convert byte array containing SRID + WKB Geometry into Geometry object
     */
    public GeoPoint from(byte[] bytes) {
        if (bytes == null) {
            return null;
        }
        try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
            // Read SRID
            byte[] sridBytes = new byte[4];
            inputStream.read(sridBytes);
            int srid = ByteOrderValues.getInt(sridBytes, byteOrder);

            // Prepare Geometry factory
            GeometryFactory geometryFactory = new GeometryFactory(precisionModel, srid, coordinateSequenceFactory);

            // Read Geometry
            WKBReader wkbReader = new WKBReader(geometryFactory);
            Geometry geometry = wkbReader.read(new InputStreamInStream(inputStream));
            Point point = (Point) geometry;
            // convert to GeoPoint
            return new GeoPoint(new BigDecimal(String.valueOf(point.getX())), new BigDecimal(String.valueOf(point.getY())));
        } catch (IOException | ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * Convert Geometry object into byte array containing SRID + WKB Geometry
     */
    public byte[] to(GeoPoint geoPoint) {
        if (geoPoint == null) {
            return null;
        }
        Coordinate coordinate = new Coordinate(geoPoint.getLat().doubleValue(), geoPoint.getLng().doubleValue());
        CoordinateArraySequence coordinateArraySequence = new CoordinateArraySequence(new Coordinate[]{coordinate}, 2);
        Point point = new Point(coordinateArraySequence, new GeometryFactory());
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // Write SRID
            byte[] sridBytes = new byte[4];
            ByteOrderValues.putInt(point.getSRID(), sridBytes, byteOrder);
            outputStream.write(sridBytes);
            // Write Geometry
            WKBWriter wkbWriter = new WKBWriter(outputDimension, byteOrder);
            wkbWriter.write(point, new OutputStreamOutStream(outputStream));
            return outputStream.toByteArray();
        } catch (IOException ioe) {
            throw new IllegalArgumentException(ioe);
        }
    }
}


package org.jeecg.config.mybatis.interceptor;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * mybatis查询结果集中 mysql的geometry类型映射到GeoPoint对象
 * @author 7788
 */
@MappedTypes(value = {GeoPoint.class})
public class MysqlGeoPointTypeHandler extends BaseTypeHandler<GeoPoint> {

    GeoPointConverter converter = new GeoPointConverter();

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, GeoPoint parameter, JdbcType jdbcType) throws SQLException {
        ps.setBytes(i, converter.to(parameter));
    }

    @Override
    public GeoPoint getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return converter.from(rs.getBytes(columnName));
    }

    @Override
    public GeoPoint getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return converter.from(rs.getBytes(columnIndex));
    }

    @Override
    public GeoPoint getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return converter.from(cs.getBytes(columnIndex));
    }
}


//对应数据库字段 指定自定义的TypeHandler即可
@TableField(typeHandler = MysqlGeoPointTypeHandler.class)
private GeoPoint geom;

标签:GeoPoint,return,BigDecimal,geometry,mysql,plus,import,new,public
From: https://www.cnblogs.com/qwer78/p/17105473.html

相关文章

  • mybatisPlus条件构造器
      Wrapper:条件构造抽象类,最顶端父类AbstractWrapper:用于查询条件封装,生成sql的where条件QueryWrapper:Entity对象封装操作类,不是用lambda......
  • MySQL常用语句整理
    表:    查询语句--查询姓“猴”的学生名单SELECT*fromstudentWHERE姓名like"猴%";--查询姓“孟”老师的个数SELECTCOUNT(教师姓名)FROMteacherWH......
  • MYSQL——真实生产环境的数据库机器配置
    摘要介绍真实项目中数据库配置选型,机器压测指标等,以帮助项目设计构建MYSQL集群能够符合你的业务。以下是个人在真实生产环境总结的的相关的参数,仅仅供大家参考。一、生产环......
  • 9 k8s内运行mysql,jenkins
    一kubernetes基于StatefulSet运行MySQL一主多从我这里演示一主一从1.1镜像准备dockerpullregistry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0dockertag......
  • MySQL 如何实现表的创建、复制、修改与删除
    MySQL中如何利用代码完成表的创建、复制、修改和删除?下面总结了在创建表的时候各字段的含义以及注意哪些问题,复制和修改及删除常用的代码。一、创建表--创建新表,如果存在......
  • MySQL 常见约束
    MySQL常见约束​​前言​​​​非空约束(notnull)​​​​唯一约束(unique)​​​​1、设置编号是唯一的(列级约束:该约束只应用于相关的一列上):​​​​2、给两个列或者多个列添......
  • MySQL 设计与开发规范
    阅读目录​​规范背景与目的​​​​设计规范​​​​数据库设计​​​​库名​​​​表结构​​​​列数据类型优化​​​​索引设计​​​​分库分表、分区表​​​​字符......
  • MySQL设置当前默认时间
    ​​CURRENT_TIMESTAMP​​......
  • MySQL查询一年、一周、三个月的数据
    阅读目录DATE_SUB()函数从日期减去指定的时间间隔DATE_SUB()函数从日期减去指定的时间间隔语法:DATE_SUB(date,INTERVALexprtype)date参数是合法的日期表达......
  • MySQL中 any,some,all 的用法
    阅读目录一、any,some,allall逻辑运算符any和some运算符一、any,some,allany,some,all是MySQL当中的逻辑运算符,作用是将子查询返回的单列值的集合与查询......