Java 如何使用MyBatis从PostGIS查询列子集?
我尝试使用MyBatis从PostGIS数据库查询数据,忽略地理空间数据。我在数据库中有下表:Java 如何使用MyBatis从PostGIS查询列子集?,java,ibatis,postgis,mybatis,Java,Ibatis,Postgis,Mybatis,我尝试使用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
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.
如何从数据库中查询列的子集?问题是,
ST\u GeomFromText('POINT({longitude}}}{latitude}),4326)
由MyBatis映射到一个准备好的语句,如下所示:ST\u GeomFromText('POINT(?),4326)
,它实际上不包含预期的参数,因为问号在引号内
解决方案是使用字符串连接(如ST|GeomFromText('POINT('124; |#{longitude}}| |'),'4326)
或使用字符串替换:ST|GeomFromText('POINT(${longitude}${latitude}'),4326)
,它直接将值放入SQL语句中,而不是使用准备语句的参数
以下映射工作(请注意经度和纬度的两个美元符号):
问题是MyBatis将
ST#u GeomFromText('POINT({longitude}}{latitude}),4326)
映射到一个准备好的语句,如下所示:ST#u GeomFromText('POINT(?),4326)
,该语句实际上不包含预期参数,因为问号在引号内
解决方案是使用字符串连接(如ST|GeomFromText('POINT('124; |#{longitude}}| |'),'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
);