【问题标题】:How do I query subset of columns from PostGIS using MyBatis?如何使用 MyBatis 从 PostGIS 中查询列的子集?
【发布时间】:2011-12-08 07:28:11
【问题描述】:

我正在尝试使用 MyBatis 从 PostGIS 数据库中查询数据,而忽略了地理空间数据。我在数据库中有下表:

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)
)

我正在尝试使用此注释将其与 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
);

目标类有这样的字段:

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

不幸的是,这不起作用,而是我得到了一个

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

如何仅从数据库中查询列的子集?

【问题讨论】:

    标签: java ibatis postgis mybatis


    【解决方案1】:

    问题是 ST_GeomFromText('POINT(#{longitude} #{latitude})', 4326) 被 MyBatis 映射到一个准备好的语句,看起来像这样:ST_GeomFromText('POINT(? ?)', 4326),它实际上不包含预期的参数,因为问号在引号内。

    解决方案是使用字符串连接(如ST_GeomFromText('POINT(' || #{longitude} || ' ' || #{latitude} || ')', 4326) 或使用字符串替换:ST_GeomFromText('POINT(${longitude} ${latitude})', 4326),将值直接放入 SQL 语句,而不是使用预准备语句的参数。

    以下映射有效(注意经度和纬度的两个美元符号):

    @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
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      相关资源
      最近更新 更多