2017-02-02 20 views
0

私は、空間機能に基づいてSpringブートサーバーを開発しています。Mybatis mapper to Mysql Pointオブジェクト

そして私はカスタマイズされたオブジェクトにmybatisのマッチによって立ち往生しています。

ここでは、テーブルと1つの列startLocationを作成しました。これはPoint型です。

CREATE TABLE `vehicle`.`route` (
    `createtime` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, 
    `updatetime` TIMESTAMP NULL, 
    `startLocation` POINT NULL, 
    `id` INT(11) NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`id`)); 

そして、私のルートJavaオブジェクトは、

@Table(name = "route") 
public class Route extends Base { 
    Point startLocation; 

    public Location getStartLocation() { 
     return startLocation; 
    } 

    public void setStartLocation(Location startLocation) { 
     this.startLocation = startLocation; 
    } 

    ....other fields 
} 

ですそして、私のLocationオブジェクトは、ちょうどdouble値として緯度と長い保持しています。

package com.supplyplatform.pojo; 

public class Location { 
    double Lat; 
    double Long; 

    public double getLat() { 
     return Lat; 
    } 
    public void setLat(double lat) { 
     Lat = lat; 
    } 
    public double getLong() { 
     return Long; 
    } 
    public void setLong(double l) { 
     Long = l; 
    } 

} 

私RouteMapper.xmlは

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="com.supplyplatform.mapper.RouteMapper"> 
    <resultMap type = "com.supplyplatform.pojo.Route" id="Route"> 
     <id column="id" jdbcType="INTEGER" property="id" /> 
     <result column="startpoint" jdbcType="OTHER" property="startLocation" /> 
    </resultMap> 

</mapper> 

あり、それにはtypehandler例外を返しません。 No typehandler found for property startLocation 私はそれに数日間を費やしました。前もって感謝します。

更新: ネストされた結果マップの間の関連付けを作成しようとしています。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="com.supplyplatform.mapper.RouteMapper"> 
    <select id="selectRoute" resultMap="Route"> 
     SELECT *, X(startpoint) as x, Y(startpoint) as y FROM vehicle.vc_route 
    </select> 
    <resultMap type = "com.supplyplatform.pojo.Route" id="Route"> 
     <id column="id" jdbcType="INTEGER" property="id" /> 
     <association property="startLocation" resultMap="Location" /> 
    </resultMap> 
    <resultMap type = "com.supplyplatform.pojo.Location" id="Location"> 
     <result column="y" property="lat" /> 
     <result column="x" property="long" /> 
    </resultMap> 
</mapper> 

しかし、それは常にLocation startLocationのためのタイプハンドラー例外を返さない:新しいXMLファイルです。

答えて

0

ロケーションは複雑なタイプなので、マップする方法を指定する必要があります。

あなたは2つの単純型の値として、それを分解することができ、次のいずれかSELECT ST_X(startPoint) as x, ST_Y(startpoint) as y、その後、関連マップ: 注this postにし、Mysql docに指定されているX/Yは、MySQLの5.7から非推奨されているので、あなたがST_X/ST_Yを使用しなければならないということを。 6。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="com.supplyplatform.mapper.RouteMapper"> 
    <resultMap type = "com.supplyplatform.pojo.Route" id="Route"> 
     <id column="id" jdbcType="INTEGER" property="id" /> 
     <association property="startLocation" resultMap="Location" /> 
    </resultMap> 
    <resultMap type = "com.supplyplatform.pojo.Location" id="Location"> 
     <result column="y" property="lat" /> 
     <result column="x" property="long" /> 
    </resultMap> 
</mapper> 

それとも、タイプハンドラを定義することができます。

public class PointTypeHandler extends org.apache.ibatis.type.BaseTypeHandler<Location> { 

    Location getNullableResult(ResultSet rs, String columnName) { 
    Location location = new Location(); 
    Object point = rs.getObject(columnName); 
    /* whatever is required to fill Location object */ 
    return location 
    } 
} 

これは、JDBCコード、this post may provide some cluesです。

とマッピングで参照: <result column="startpoint" jdbcType="OTHER" property="startLocation" typeHandler="PointTypeHandler"/>

+0

私は、ネストされたこのresultMapのアプローチをしようとしていますが、それは常に私が質問を更新したんtypehandler例外を返します。 – Mix

+0

結果セットに_startLocation_という名前の列が含まれているため、Mybatisは列名がプロパティ名と一致するため、_startLocation_プロパティに自動的にマップしようとします。 – blackwizard

+0

Routeオブジェクトのフィールドとして "Location startpoint"を入れる必要がありますか?例外は常にここからスローされます。 – Mix

関連する問題