2016-06-16 16 views
0

mybatisを使用してJavaでmysqlポイントタイプをマッピングする方法は?今はjava.lang.Objectです。 これは私のテーブルです:mybatisを使用してJavaで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で、モデルはオブジェクトとして小数点型を持つことができ、および文字列「のポイント(1 1)」に割り当てられたか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> 
関連する問題