首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mybatis映射器到Mysql点对象

Mybatis映射器到Mysql点对象
EN

Stack Overflow用户
提问于 2017-02-02 19:20:01
回答 1查看 1.4K关注 0票数 0

我正在开发一个基于空间功能的Spring boot服务器。

我被mybatis与自定义对象的匹配所困扰。

现在我已经创建了表和一个列startLocation,它是一个Point类型。

代码语言:javascript
复制
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对象是

代码语言:javascript
复制
@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两个值。

代码语言:javascript
复制
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是

代码语言:javascript
复制
<?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文件是:

代码语言:javascript
复制
<?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的类型处理程序异常。

EN

回答 1

Stack Overflow用户

发布于 2017-02-02 22:27:51

位置是一个复杂的类型,那么您必须指定如何映射。

您可以将其分解为两个简单的类型值:SELECT ST_X(startPoint) as x, ST_Y(startpoint) as y,然后映射一个关联:请注意,正如在this postMysql doc中指定的那样,您应该使用st_x/st_y,因为从MySQL5.7.6开始不推荐使用x/y。

代码语言:javascript
复制
<?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>

或者,您可以定义类型处理程序:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42000859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档