简体   繁体   English

Mybatis映射器到Mysql Point对象

[英]Mybatis mapper to Mysql Point object

I am working on a Spring boot server based on spatial functionalities. 我正在基于空间功能的Spring引导服务器上工作。

And I am stuck by the mybatis match to customized object. 我被mybatis匹配到定制对象所困扰。

Now I have created table, and one column startLocation, which is a Point type. 现在,我已经创建了表和一列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`));

And my Route java object is 我的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
}

And my Location object just holds lat and long as double value. 而且我的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;
    }

}

My RouteMapper.xml is 我的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> 

And it returns no typehandler exception. 并且它不返回任何类型处理程序异常。 No typehandler found for property startLocation I have spent days on it. 我没有花几天时间No typehandler found for property startLocation处理程序。 Thank you in advance. 先感谢您。

UPDATE: I am trying to create association between the nested result map. 更新:我正在尝试在嵌套结果图之间创建关联。 The new xml file is : 新的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>

But it always return no type handler exception for Location startLocation . 但是它始终不为Location startLocation返回任何类型处理程序异常。

Location is a complex type, then you have to specify how to map. 位置是一种复杂的类型,因此您必须指定如何映射。

You can either decompose it as 2 simple type values: SELECT ST_X(startPoint) as x, ST_Y(startpoint) as y and then map an association: Note that as specified in this post and in the Mysql doc , you shall use st_x/st_y since x/y are deprecated from Mysql 5.7.6. 您可以将其分解为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>

Or you can define a type handler: 或者,您可以定义类型处理程序:

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

This is JDBC code, this post may provide some clues . 这是JDBC代码, 本文可能会提供一些线索

and reference it in the mapping: <result column="startpoint" jdbcType="OTHER" property="startLocation" typeHandler="PointTypeHandler"/> 并在映射中引用它: <result column="startpoint" jdbcType="OTHER" property="startLocation" typeHandler="PointTypeHandler"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM