代码拉取完成,页面将自动刷新
配置数据库链接
修改resources/mybatis-generator-config.xml数据表映射
运行 mbg.Generator
运行程序,调用接口
添加地理坐标类
import lombok.Data;
@Data
public class CPoint {
double[] coordinates;
}
@Data
public class CLine {
private List<double[]> coordinates;
}
添加地理坐标数据类型处理
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.postgis.PGgeometry;
import org.postgis.Point;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@MappedTypes({CPoint.class})
public class PointHandler extends BaseTypeHandler<CPoint> {
@Override
// 重写CPoint getter 方法, 其他方法可以为默认值
public CPoint getNullableResult(ResultSet resultSet, String s) throws SQLException {
PGgeometry pGgeometry = (PGgeometry) resultSet.getObject(s);
Point point = pGgeometry.getGeometry().getPoint(0);
CPoint cPoint = new CPoint();
cPoint.setCoordinates(new double[]{point.x, point.y});
return cPoint;
}
}
@MappedTypes(CLine.class)
public class LineHandler extends BaseTypeHandler<CLine> {
@Override
public CLine getNullableResult(ResultSet resultSet, String s) throws SQLException {
List<double[]> coordinates = new ArrayList<>();
PGgeometry pGgeometry = (PGgeometry) resultSet.getObject(s);
// 正常的数据类型应该为LineString的
// 因为数据库里面是MultiLineString, 所以我取了第一条多段线
MultiLineString multiLineString = (MultiLineString) pGgeometry.getGeometry();
if(multiLineString.getLines().length == 0) return null;
LineString firstLine = multiLineString.getLine(0);
for (Point point : firstLine.getPoints()){
double[] coord = new double[]{point.x, point.y};
coordinates.add(coord);
}
CLine cLine = new CLine();
cLine.setCoordinates(coordinates);
return cLine;
}
}
参考链接:
注:省略的配置数据:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://0.0.0.0:5432/database"/>
<property name="username" value=""/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
</configuration>
server.port=9000
# 数据库配置
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://0.0.0.0:5432/database
spring.datasource.username=
spring.datasource.password=
spring.jackson.serialization.fail-on-empty-beans=false
# Mybatis 配置
mybatis.mapperLocations=classpath:mapper/*.xml
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。