2017-02-02 5 views
0

Ich arbeite an einem Spring Boot Server basierend auf räumlichen Funktionalitäten.Mybatis Mapper zu Mysql Point Objekt

Und ich bin von der mybatis Übereinstimmung mit benutzerdefinierten Objekt stecken.

Jetzt habe ich eine Tabelle erstellt, und eine Spalte startLocation, die ein Point-Typ ist.

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`)); 

Und mein Weg Java-Objekt ist

@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 
} 

Und mein Ort Objekt hält nur lat und lange als den doppelten Wert.

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; 
    } 

} 

Mein RouteMapper.xml ist

<?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> 

Und es gibt keine typehandler Ausnahme. No typehandler found for property startLocation Ich habe Tage darauf verbracht. Vielen Dank im Voraus.

UPDATE: Ich versuche, eine Zuordnung zwischen der verschachtelten Ergebniskarte zu erstellen. Die neue XML-Datei ist:

<?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> 

Aber es zurückgeben immer keine Art Ausnahmebehandler für Location startLocation.

Antwort

0

Ort ist ein komplexer Typ, dann müssen Sie angeben, wie zugeordnet werden soll.

können Sie entweder zersetzen als 2 einfache Art Wert: SELECT ST_X(startPoint) as x, ST_Y(startpoint) as y und dann eine Assoziation Karte: Beachten Sie, dass in this post und in den Mysql doc angegeben, wie sollst du ST_X/ST_Y verwenden, da x/y von Mysql 5.7 sind veraltet. 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> 

Oder Sie können einen Typ-Handler definieren:

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 
    } 
} 

Dies ist JDBC-Code, this post may provide some clues.

und es in der Mapping-Referenz: <result column="startpoint" jdbcType="OTHER" property="startLocation" typeHandler="PointTypeHandler"/>

+0

Ich bin der verschachtelten resultMap Ansatz versucht, aber es gibt immer die No typehandler Ausnahme ich die Frage aktualisiert haben. – Mix

+0

Wählen Sie nicht 'SELECT *', da die Ergebnismenge dann eine Spalte mit dem Namen _startLocation_ enthält und Mybatis versucht, sie (automatisch) der Eigenschaft _startLocation_ zuzuordnen, da der Spaltenname mit dem Eigenschaftsnamen übereinstimmt. – blackwizard

+0

also ist es notwendig, "Location Startpoint" als ein Feld von Route-Objekt? Die Ausnahme wird immer von hier geworfen. – Mix

Verwandte Themen