[英]How to get Map<String, String> as return type using mybatis annotations
[英]How to map mysql point type in java using mybatis
如何使用mybatis在Java中映射mysql点类型? 现在是java.lang.Object。 这是我的桌子:
CREATE TABLE `test` (
`id` int(11) NOT NULL,
`location` point DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
这是生成器提供的xml:
<resultMap id="BaseResultMap" type="package.model.Test">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="location" jdbcType="OTHER" property="location" />
</resultMap>
<insert id="insert" parameterType="package.model.Test">
insert into test (id, location)
values (#{id,jdbcType=INTEGER}, #{location,jdbcType=OTHER})
</insert>
我努力了:
public void testPointType() {
GeometryFactory geometryFactory = new GeometryFactory();
com.vividsolutions.jts.geom.Point point = geometryFactory.createPoint(new Coordinate(1, 1));
package.model.Test record = new package.model.Test();
record.setLocation(point.toText());
testMapper.insertSelective(record);
}
但是得到: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Cannot get geometry object from data you send to the GEOMETRY field
经过数小时的挖掘。 我一直在寻找这种方式:
<insert id="insert" parameterType="package.model.Test">
insert into test (id, location)
values (#{id,jdbcType=INTEGER}, GeomFromText(#{location,jdbcType=OTHER}))
</insert>
在Java中,模型的点类型可以为Object,并分配有字符串“ point(1 1)”或使用生动的解决方案geometryObject.toText()
方法。
最初没有用是因为,如果没有GeomFromText()
, GeomFromText()
其视为insert int test (id, location) values (1, 'point(1 1)')
,该值周围有一个引号。 选择:
<select resultType="java.lang.String">
select astext(location) from test
</select>
不确定模型Test
哪些字段,也不确定它是否对您有用,请尝试一下;)
<insert id="insert" parameterType="package.model.Test">
insert into test (id, location)
values (#{id,jdbcType=INTEGER}, Point( #{location.x}, #{location.x} ))
</insert>
而且我认为您最好像下面这样定义Test
模型:
public class Test {
private String id;
private String locationX;
private String locationY;
... ...
getter and setter
... ...
}
然后您可以这样做:
<insert id="insert" parameterType="package.model.Test">
insert into test (id, location)
values (#{id,jdbcType=INTEGER}, Point( #{locationX}, #{locationY} ))
</insert>
对于这种模型Test
,您可以select
如下:
<select id="insert" resultType="package.model.Test">
select id, x(location) as locationX, y(location) as locationY from test
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.