【问题标题】:Mybatis mapper to Mysql Point objectMybatis mapper 到 Mysql Point 对象
【发布时间】:2017-06-19 10:39:49
【问题描述】:

我正在开发基于空间功能的 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 对象只是将 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 的类型处理程序异常。

【问题讨论】:

    标签: java mysql spring-boot mybatis


    【解决方案1】:

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

    您可以将其分解为 2 个简单类型值:SELECT ST_X(startPoint) as x, ST_Y(startpoint) as y,然后映射关联: 请注意,正如 this postMysql 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

    并在映射中引用它: &lt;result column="startpoint" jdbcType="OTHER" property="startLocation" typeHandler="PointTypeHandler"/&gt;

    【讨论】:

    • 我正在尝试嵌套的 resultMap 方法,但它总是返回 No typehandler 异常我已经更新了问题。
    • 不要SELECT *,因为结果集随后包含一个名为 startLocation 的列,Mybatis 会尝试(自动)将其映射到属性 startLocation因为列名与属性名匹配。
    • 那么有必要把“Location startpoint”作为Route对象的一个​​字段吗?异常总是从这里抛出。
    • 您最终应该能够使用此模型填充您的对象。但是您可以尝试将属性 lat、long 移动到 Route 类(不再有 Location)以及 Route resultMap(不再有关联)中。
    • 如果您选择 *,则查询返回(其中)一个类型为 (Mysql) POINT 的列 startLocation,此外:Route 类有一个属性 startLocation i> 类型的位置。 Mybatis 尝试对此进行映射,因为 column=>property 名称匹配(隐式映射)。但是 Mybatis 不知道如何将 Mysql POINT 映射到 Location 对象这就是为什么你没有得到 Location startLocation 的类型处理程序异常的原因。然后select id, T_X(startPoint) as x, ST_Y(startpoint) as y。或者,您可以重命名表列或类属性以打破名称匹配并观察效果。
    猜你喜欢
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2020-03-14
    • 1970-01-01
    相关资源
    最近更新 更多