[JAVA] mybatis 使用 geometry

mybatis 使用 geometry

创建表

DROP TABLE IF EXISTS `geo`;
CREATE TABLE `geo`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `g` geometry NULL,
  `s` json NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

生成实体以及mapper

  • 实体我们对geometry字段使用String类型

public class Geo {
    private Integer id;

    private String g;

    private String s;

//  get&set
}
  • mapper.xml


<mapper namespace="com.example.demo.mapper.GeoMapper">
  <resultMap id="BaseResultMap" type="com.example.demo.entity.Geo">
    
    
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="g" jdbcType="OTHER" property="g" />
    <result column="s" jdbcType="OTHER" property="s" />
  resultMap>
  <sql id="Base_Column_List">
    
    id, `g`, s
  sql>

  <insert id="insert">
    insert into geo (g)  values ( geomfromtext(#{g,jdbcType=VARCHAR}) )
  insert>

  <select id="selectAll" resultMap="BaseResultMap">
    select id , astext(g) as g  from geo
  select>
mapper>
  • 这样就可以执行mapper的基本操作
Geo geo = new Geo();
geo.setG("POINT(1 1)");

geoMapper.insert(geo);

geo.setG("LINESTRING(2 1, 6 6)");
geoMapper.insert(geo);
geo.setG("POLYGON((0 5, 2 5, 2 7, 0 7, 0 5))");
geoMapper.insert(geo);
  • 以上这些基本使用已经满足,但是我们可能会需要对查询结果进行运算需要将String表示的字符串转换成GEOTOOLS支持的类型或者其他GIS框架

更新实体

public abstract class ParentGeo {
    private String g;

    /**
     * 通过字符串直接判断
     *
     * @return
     */
    private boolean isPoint() {
        return this.g.startsWith("point") || this.g.startsWith("POINT");

    }

    private boolean isLine() {
        return this.g.startsWith("linestring") || this.g.startsWith("LINESTRING");
    }

    private boolean isPolygon() {
        return this.g.startsWith("polygon") || this.g.startsWith("POLYGON");
    }

    public String getG() {
        return g;
    }

    public void setG(String g) {
        this.g = g;
    }
}
public class Geo extends ParentGeo {
    private Integer id;


    private String s;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }


    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }
}
  • 带有可操作的gis实体
public class RelGeo {
    private Geo geo;
    private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);

    public RelGeo(Geo geo) {
        this.geo = geo;
    }

    public Geo getGeo() {
        return geo;
    }

    public GeometryFactory getGeometryFactory() {
        return geometryFactory;
    }

    public LineString getLineString() throws ParseException {
        if (this.geo.isLine()) {
            WKTReader reader = new WKTReader(geometryFactory);
            return (LineString) reader.read(geo.getG());
        }
        return null;
    }

    public Point getPoint() throws ParseException {
        if (geo.isPoint()) {
            WKTReader reader = new WKTReader(geometryFactory);
            return (Point) reader.read(geo.getG());
        }
        return null;
    }

    public Polygon getPolygon() throws ParseException {
        if (geo.isPolygon()) {
            WKTReader reader = new WKTReader(geometryFactory);
            return (Polygon) reader.read(geo.getG());
        }
        return null;
    }
}
  • 使用
    List<Geo> geos = geoMapper.selectAll();
        for (Geo geo1 : geos) {
            RelGeo relGeo = new RelGeo();
            BeanUtils.copyProperties(geo1, relGeo);
            if (relGeo.getPoint() != null) {
                Point point = relGeo.getPoint();
                System.out.println(point);
            } else if (relGeo.getLineString() != null) {
                LineString lineString = relGeo.getLineString();
                System.out.println(lineString);
            } else {
                Polygon polygon = relGeo.getPolygon();
                System.out.println(polygon);
            }

        }
  • 转换过之后就可以对Point这些geom数据类型进行操作了

你可能感兴趣的:(map,JAVA,数据库)