java - Mybatis 映射器到 Mysql Point 对象

标签 java mysql spring-boot mybatis

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

我被 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 对象只保存纬度和经度作为 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> 

并且它不返回类型处理程序异常。 找不到属性 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 的类型处理程序异常。

最佳答案

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

您可以将其分解为 2 个简单类型值:SELECT ST_X(startPoint) as x, ST_Y(startpoint) as y然后映射关联: 请注意,如 this post 中所指定。并在 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 代码,this post may provide some clues .

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

关于java - Mybatis 映射器到 Mysql Point 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42000859/

相关文章:

java - Struts2 中的全局资源包 - 粒度最佳实践?

java - Spring Boot + Java 模块 = 不支持的类文件主要版本

.net - 如何以编程方式修改连接字符串?

java - 在特定gradle任务上设置调试JVM args

java - 如何在 Android 中从 yahoo 和其他人发送邮件

Java 和 C# 二进制字符格式差异

mySQL:截断所有表的命令

php - 在 PHP 中高效地缓存和分页大型 MySQL 结果集

java - 在 Spring Boot 中重定向到不同的主机(非 www 到 www URL)

java - 使用 Spring Security 手动实现登录