簡體   English   中英

Mybatis映射器到Mysql Point對象

[英]Mybatis mapper to Mysql Point object

我正在基於空間功能的Spring引導服務器上工作。

我被mybatis匹配到定制對象所困擾。

現在,我已經創建了表和一列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`));

我的Route 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對象只保存lat和long的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> 

並且它不返回任何類型處理程序異常。 我沒有花幾天時間No typehandler found for property startLocation處理程序。 先感謝您。

更新:我正在嘗試在嵌套結果圖之間創建關聯。 新的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">
    <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返回任何類型處理程序異常。

位置是一種復雜的類型,因此您必須指定如何映射。

您可以將其分解為2個簡單類型值: SELECT ST_X(startPoint) as x, ST_Y(startpoint) as y ,然后映射一個關聯:請注意,如本文Mysql doc中所指定,您應使用st_x / st_y因為x / y已從Mysql 5.7.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代碼, 本文可能會提供一些線索

並在映射中引用它: <result column="startpoint" jdbcType="OTHER" property="startLocation" typeHandler="PointTypeHandler"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM