配置mybatis自定义字段类型转换(TypeHandler)即可,可无mybatis plus,下面贴代码
引入包:
implementation "com.baomidou:mybatis-plus-boot-starter:3.3.1"
implementation "org.locationtech.jts:jts-core:1.16.1"
编写TypeHandler:
import cn.hutool.extra.spring.SpringUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.io.ByteOrderValues;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* mysql的geometry类型映射
* Created by huangyifeng on 2020-02-17.
*/
public class MysqlGeoLineTypeHandler extends BaseTypeHandler<LineString> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, LineString parameter, JdbcType jdbcType) throws SQLException {
try {
ps.setBytes(i, convertToBytes(parameter));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public LineString getNullableResult(ResultSet rs, String columnName) throws SQLException {
return convertToGeo(rs.getBytes(columnName));
}
@Override
public LineString getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return convertToGeo(rs.getBytes(columnIndex));
}
@Override
public LineString getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return convertToGeo(cs.getBytes(columnIndex));
}
/**
* bytes转Geo对象
* @param bytes
* @return
*/
private LineString convertToGeo(byte[] bytes) {
if (bytes == null) {
return null;
}
try {
byte[] geomBytes = ByteBuffer.allocate(bytes.length - 4).order(ByteOrder.LITTLE_ENDIAN)
.put(bytes, 4, bytes.length - 4).array();
Geometry geometry = SpringUtil.getBean(WKBReader.class).read(geomBytes);
return (LineString) geometry;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* geo转bytes
* @param geometry
* @return
* @throws IOException
*/
private byte[] convertToBytes(Geometry geometry) throws IOException {
byte[] geometryBytes = SpringUtil.getBean(WKBWriter.class).write(geometry);
byte[] wkb = new byte[geometryBytes.length + 4];
//设置SRID为4326
ByteOrderValues.putInt(4326, wkb, ByteOrderValues.LITTLE_ENDIAN);
System.arraycopy(geometryBytes, 0, wkb, 4, geometryBytes.length);
return wkb;
}
}
entity实体类:
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants;
import org.locationtech.jts.geom.LineString;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 道路状态vo
* Created by huangyifeng on 2020-02-13.
*/
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
@EqualsAndHashCode(of = { "name", "direction", "angle", "lcodes", "polyline"})
@TableName(value = "t_traffic_status")
public class TrafficStatus implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
@TableId(type = IdType.AUTO)
Long id;
/**
* 道路名称
*/
String name;
/**
* 路况:0:未知 1:畅通 2:缓行 3:拥堵 4:严重拥堵
*/
String status;
/**
* 方向描述
*/
String direction;
/**
* 车行角度,判断道路正反向使用。以正东方向为0度,逆时针方向为正,取值范围:[0,360]
*/
String angle;
/**
* 速度 单位:千米/小时
*/
String speed;
/**
* 即locationcode的集合,是道路中某一段的id,一条路包括多个locationcode。angle在[0-180]之间取正值,[181-359]之间取负值。
*/
String lcodes;
/**
* 道路坐标集,坐标集合
*/
String polyline;
/**
* 道路空间字段
*/
LineString linegeo;
/**
* 时间
*/
LocalDateTime time;
}
添加字段类型转换支持(有多种方式配置):
mybatis-plus:
## 自定义字段类型转换
type-handlers-package: com.yiring.bus.config.mybatis
有关jts的bean注入:
import cn.hutool.extra.spring.SpringUtil;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.ByteOrderValues;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;
import org.locationtech.jts.io.WKTReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
/**
* 有关jts的bean注入
* Created by huangyifeng on 2020-02-18.
*/
@Component
@ComponentScan(value = {"cn.hutool.extra.spring"})
@Import(SpringUtil.class)
public class JTSBean {
private GeometryFactory geometryFactory;
public JTSBean() {
geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
}
@Bean
public WKBWriter wkbWriter() {
return new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN);
}
@Bean
public WKBReader wkbReader() {
return new WKBReader(geometryFactory);
}
@Bean
public WKTReader wktReader() {
return new WKTReader(geometryFactory);
}
}