[英]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.