我正在使用基于空间功能的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
返回任何类型处理程序异常。
答案 0 :(得分:0)
位置是一种复杂类型,然后您必须指定如何映射。
您可以将其分解为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"/>