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

发布于 2024-12-09 17:54:14 字数 1188 浏览 3 评论 0原文

我正在尝试使用 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.

How do I query just the subset of columns从数据库中?

I'm trying to query data from a PostGIS database using MyBatis, ignoring the geospatial data. 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:

@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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2024-12-16 17:54:14

问题是 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
);

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.

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.

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
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文