2016-06-16 41 views
0

如何使用mybatis在java中映射mysql點類型?現在是java.lang.Object。 這是我的表:如何在java中使用mybatis映射mysql點類型

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

回答

0

不確定模型Test中有哪些字段,並且不確定它是否是RKS你,但只是嘗試;)

<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> 
+0

以及如何選擇? – Tiina

+0

@Tiina更新回答,請檢查它。 – Blank

+0

這是一種方法。 – Tiina

1

經過數小時的挖掘。我一直在尋找這樣的:

<insert id="insert" parameterType="package.model.Test"> 
    insert into test (id, location) 
    values (#{id,jdbcType=INTEGER}, GeomFromText(#{location,jdbcType=OTHER})) 
</insert> 

,並在Java中,模型可以有一點類型爲對象,並用字符串「點(11)」,或者使用vividsolutions geometryObject.toText()方法分配。

它在開始時並沒有工作,因爲沒有GeomFromText(),mybatis認爲它是insert int test (id, location) values (1, 'point(1 1)'),這個值有一個引號。 選擇:

<select resultType="java.lang.String"> 
    select astext(location) from test 
</select>