如何使用 mybatis 在 java 中映射 mysql 点类型
How to map mysql point type in java using mybatis
如何使用mybatis映射mysql点类型到java?现在是 java.lang.Object。
这是我的 table:
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
不确定模型 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>
经过几个小时的挖掘。我一直在寻找这种方式:
<insert id="insert" parameterType="package.model.Test">
insert into test (id, location)
values (#{id,jdbcType=INTEGER}, GeomFromText(#{location,jdbcType=OTHER}))
</insert>
并且在 java 中,模型可以将点类型设为对象,并分配一个字符串 "point(1 1)" 或使用 vividsolutions geometryObject.toText()
方法。
一开始不行是因为没有GeomFromText()
,mybatis认为是insert int test (id, location) values (1, 'point(1 1)')
,这个值有引号。
到 select:
<select resultType="java.lang.String">
select astext(location) from test
</select>
如何使用mybatis映射mysql点类型到java?现在是 java.lang.Object。 这是我的 table:
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
不确定模型 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>
经过几个小时的挖掘。我一直在寻找这种方式:
<insert id="insert" parameterType="package.model.Test">
insert into test (id, location)
values (#{id,jdbcType=INTEGER}, GeomFromText(#{location,jdbcType=OTHER}))
</insert>
并且在 java 中,模型可以将点类型设为对象,并分配一个字符串 "point(1 1)" 或使用 vividsolutions geometryObject.toText()
方法。
一开始不行是因为没有GeomFromText()
,mybatis认为是insert int test (id, location) values (1, 'point(1 1)')
,这个值有引号。
到 select:
<select resultType="java.lang.String">
select astext(location) from test
</select>