Java Mybatis映射到Mysql点对象
我正在基于空间功能的Spring引导服务器上工作 我被自定义对象的mybatis匹配卡住了 现在我已经创建了表和一列,这是点类型Java Mybatis映射到Mysql点对象,java,mysql,spring-boot,mybatis,Java,Mysql,Spring Boot,Mybatis,我正在基于空间功能的Spring引导服务器上工作 我被自定义对象的mybatis匹配卡住了 现在我已经创建了表和一列,这是点类型 CREATE TABLE `vehicle`.`route` ( `createtime` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP, `updatetime` TIMESTAMP NULL, `startLocation` POINT NULL, `id` INT(11) NOT NULL AUTO_INCRE
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作为双值
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>
但是它总是不返回类型处理程序异常,因为
Location
Location是一个复杂的类型,那么您必须指定如何映射
您可以将其分解为两个简单类型值:选择ST_X(起始点)作为X,ST_Y(起始点)作为Y
,然后映射关联:
请注意,如和中所述,您应该使用st_x/st_y,因为Mysql 5.7.6不推荐使用x/y
<?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>
并在映射中引用它:
我正在尝试嵌套的resultMap方法,但它总是返回No-typehandler异常,因为我已经更新了问题。不要选择*
,因为结果集随后包含一个名为startLocation的列,Mybatis尝试(自动)将其映射到属性startLocation,因为列名与属性名匹配。因此是否需要将“Location startpoint”作为路由对象的字段?异常总是从这里抛出的。你最终应该能够用这个模型填充你的对象。但是,您可以尝试将属性lat、long移动到Route类(无更多位置)以及Route resultMap(无更多关联)中。如果选择*,则查询将(在所有查询中)返回类型为(Mysql)点的列startLocation,此外:Route类具有类型为Location的属性startLocation。Mybatis尝试对此进行映射,因为列=>属性名称匹配(隐式映射)。但Mybatis不知道如何将Mysql点映射到Location对象,这就是为什么Location不会出现类型处理程序异常。然后选择id,T_X(起始点)作为X,ST_Y(起始点)作为Y
。或者,可以重命名表列或类属性,以断开名称匹配并观察效果。
<?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
}
}