简体   繁体   English

如何使用MyBatis从PostGIS查询列的子集?

[英]How do I query subset of columns from PostGIS using MyBatis?

I'm trying to query data from a PostGIS database using MyBatis, ignoring the geospatial data. 我正在尝试使用MyBatis从PostGIS数据库查询数据,而忽略了地理空间数据。 I have the following table in the database: 我在数据库中有下表:

CREATE TABLE salesgeometry
(
  id bigint NOT NULL,
  label character varying(255),
  type character varying(255),
  geom geometry,
  CONSTRAINT salesgeometry_pkey PRIMARY KEY (id ),
  CONSTRAINT enforce_dims_geom CHECK (st_ndims(geom) = 2),
  CONSTRAINT enforce_srid_geom CHECK (st_srid(geom) = 4326)
)

I'm trying to map this with MyBatis using this annotation: 我正在尝试使用以下注释将其与MyBatis映射:

@Select("SELECT id, type, label FROM salesgeometry WHERE ST_Within(" +
        "ST_GeomFromText('POINT(#{longitude} #{latitude})', 4326), geom) " +
        "AND type = #{type}")
Geometry getGeometryAtLocation(
        @NotNull @Param("type") String geometryType,
        @NotNull @Param("longitude") BigDecimal longitude, 
        @NotNull @Param("latitude") BigDecimal latitude
);

And the target class has fields like this: 目标类具有如下字段:

public class Geometry {
    private long id;
    private String type;
    private String label;
    ...
}

Unfortunately this does not work, instead I get a 不幸的是,这不起作用,相反,我得到一个

org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.

How do I query just the subset of columns from the database? 如何仅查询数据库中的列子集?

The problem is that ST_GeomFromText('POINT(#{longitude} #{latitude})', 4326) is mapped by MyBatis to a prepared statement looking like this: ST_GeomFromText('POINT(? ?)', 4326) , which doesn't actually contain the expected parameters since the question marks are within quotes. 问题是ST_GeomFromText('POINT(#{longitude} #{latitude})', 4326)ST_GeomFromText('POINT(#{longitude} #{latitude})', 4326)映射到如下所示的准备好的语句: ST_GeomFromText('POINT(? ?)', 4326) ,它不会t实际上包含预期的参数,因为问号在引号内。

The solution is to either use string concatenation (as in ST_GeomFromText('POINT(' || #{longitude} || ' ' || #{latitude} || ')', 4326) or use string substitution instead: ST_GeomFromText('POINT(${longitude} ${latitude})', 4326) , which puts the values into the SQL statement directly instead of using parameters of prepared statements. 解决方案是使用字符串连接(如ST_GeomFromText('POINT(' || #{longitude} || ' ' || #{latitude} || ')', 4326)或使用字符串替换: ST_GeomFromText('POINT(${longitude} ${latitude})', 4326) ,它直接将值放入SQL语句中,而不使用准备好的语句的参数。

The following mapping works (note the two dollar symbols for longitude and latitude): 以下映射有效(注意经度和纬度的两个美元符号):

@Select("SELECT id, type, label FROM salesgeometry WHERE ST_Within(" +
        "ST_GeomFromText('POINT(${longitude} ${latitude})', 4326), geom) " +
        "AND type = #{type}")
Geometry getGeometryAtLocation(
        @NotNull @Param("type") String geometryType,
        @NotNull @Param("longitude") BigDecimal longitude, 
        @NotNull @Param("latitude") BigDecimal latitude
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM