init
This commit is contained in:
commit
02c6a5ee70
50
.gitignore
vendored
Normal file
50
.gitignore
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### Vue.js ###
|
||||
.DS_Store
|
||||
node_modules/
|
||||
dist/
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
package-lock.json
|
||||
tests/**/coverage/
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/
|
||||
.mvn/
|
||||
/src/main/resources/static/
|
||||
/files/
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
11
README.md
Normal file
11
README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# handler
|
||||
|
||||
适用于 Mybatis 的 GIS 对象处理
|
||||
|
||||
## PostGIS
|
||||
|
||||
Mybatis 处添加以下配置
|
||||
|
||||
```yaml
|
||||
type-handlers-package: ltd.llvy.postgis.handler
|
||||
```
|
67
pom.xml
Normal file
67
pom.xml
Normal file
@ -0,0 +1,67 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ltd.llvy</groupId>
|
||||
<artifactId>handler</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>3.5.16</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.17.2</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.17.2</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.locationtech.jts/jts-core -->
|
||||
<dependency>
|
||||
<groupId>org.locationtech.jts</groupId>
|
||||
<artifactId>jts-core</artifactId>
|
||||
<version>1.19.0</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/net.postgis/postgis-jdbc -->
|
||||
<dependency>
|
||||
<groupId>net.postgis</groupId>
|
||||
<artifactId>postgis-jdbc</artifactId>
|
||||
<version>2.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>gitea</id>
|
||||
<url>https://www.llvy.ltd/api/packages/llvy.ltd/maven</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>gitea</id>
|
||||
<url>https://www.llvy.ltd/api/packages/llvy.ltd/maven</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>gitea</id>
|
||||
<url>https://www.llvy.ltd/api/packages/llvy.ltd/maven</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
</project>
|
@ -0,0 +1,71 @@
|
||||
package ltd.llvy.postgis.handler;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.locationtech.jts.geom.Geometry;
|
||||
import org.locationtech.jts.io.WKTReader;
|
||||
import org.locationtech.jts.io.WKTWriter;
|
||||
import org.postgis.PGgeometry;
|
||||
|
||||
/**
|
||||
* (AbstractGeometryTypeHandler)
|
||||
*
|
||||
* @author zweiandlen
|
||||
* @since 2024/6/4 下午5:13
|
||||
*/
|
||||
public abstract class AbstractGeometryTypeHandler<T extends Geometry> extends BaseTypeHandler<T> {
|
||||
|
||||
/** WKTReader非线程安全 */
|
||||
private static final ThreadLocal<WKTReader> READER_POOL = ThreadLocal.withInitial(WKTReader::new);
|
||||
|
||||
/** WKTWriter非线程安全 */
|
||||
private static final ThreadLocal<WKTWriter> WRITER_POOL = ThreadLocal.withInitial(WKTWriter::new);
|
||||
|
||||
/** 与数据库中几何列的空间坐标系保持一致,要不然写入会报错 */
|
||||
private static final int SRID_IN_DB = 4490;
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType)
|
||||
throws SQLException {
|
||||
PGgeometry pGgeometry = new PGgeometry(WRITER_POOL.get().write(parameter));
|
||||
org.postgis.Geometry geometry = pGgeometry.getGeometry();
|
||||
geometry.setSrid(SRID_IN_DB);
|
||||
ps.setObject(i, pGgeometry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
PGgeometry pgGeometry = (PGgeometry) rs.getObject(columnName);
|
||||
return getResult(pgGeometry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
PGgeometry pgGeometry = (PGgeometry) rs.getObject(columnIndex);
|
||||
return getResult(pgGeometry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
PGgeometry string = (PGgeometry) cs.getObject(columnIndex);
|
||||
return getResult(string);
|
||||
}
|
||||
|
||||
private T getResult(PGgeometry pGgeometry) {
|
||||
if (pGgeometry == null) {
|
||||
return null;
|
||||
}
|
||||
String pgWkt = pGgeometry.toString();
|
||||
String target = String.format("SRID=%s;", SRID_IN_DB);
|
||||
String wkt = pgWkt.replace(target, "");
|
||||
try {
|
||||
return (T) READER_POOL.get().read(wkt);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("解析wkt失败:" + wkt, e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package ltd.llvy.postgis.handler;
|
||||
|
||||
import com.fasterxml.jackson.core.JacksonException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import java.io.IOException;
|
||||
import org.locationtech.jts.geom.Geometry;
|
||||
import org.locationtech.jts.io.ParseException;
|
||||
import org.locationtech.jts.io.WKTReader;
|
||||
|
||||
/**
|
||||
* (GeometryDeserializer)
|
||||
*
|
||||
* @author zweiandlen
|
||||
* @since 2024/6/5 下午4:38
|
||||
*/
|
||||
public class GeometryDeserializer<T extends Geometry> extends JsonDeserializer<T> {
|
||||
@Override
|
||||
public T deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
|
||||
throws IOException, JacksonException {
|
||||
String wkt = jsonParser.getValueAsString();
|
||||
WKTReader wktReader = new WKTReader();
|
||||
try {
|
||||
Geometry geometry = wktReader.read(wkt);
|
||||
return (T) geometry;
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException("解析wkt失败:" + wkt, e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package ltd.llvy.postgis.handler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import java.io.IOException;
|
||||
import org.locationtech.jts.geom.Geometry;
|
||||
import org.locationtech.jts.io.WKTWriter;
|
||||
|
||||
/**
|
||||
* (GeometrySerializer)
|
||||
*
|
||||
* @author zweiandlen
|
||||
* @since 2024/6/5 下午4:40
|
||||
*/
|
||||
public class GeometrySerializer extends JsonSerializer<Geometry> {
|
||||
|
||||
@Override
|
||||
public void serialize(
|
||||
Geometry geometry, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
|
||||
throws IOException {
|
||||
jsonGenerator.writeObject(new WKTWriter().write(geometry));
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package ltd.llvy.postgis.handler;
|
||||
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
import org.locationtech.jts.geom.MultiPolygon;
|
||||
|
||||
/**
|
||||
* (MultiPolygonTypeHandler)
|
||||
*
|
||||
* @author zweiandlen
|
||||
* @since 2024/6/4 下午5:22
|
||||
*/
|
||||
@MappedJdbcTypes(JdbcType.OTHER)
|
||||
@MappedTypes(value = MultiPolygon.class)
|
||||
public class MultiPolygonTypeHandler extends AbstractGeometryTypeHandler<MultiPolygon> {}
|
16
src/main/java/ltd/llvy/postgis/handler/PointTypeHandler.java
Normal file
16
src/main/java/ltd/llvy/postgis/handler/PointTypeHandler.java
Normal file
@ -0,0 +1,16 @@
|
||||
package ltd.llvy.postgis.handler;
|
||||
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
import org.locationtech.jts.geom.Point;
|
||||
|
||||
/**
|
||||
* (PointTypeHandler)
|
||||
*
|
||||
* @author zweiandlen
|
||||
* @since 2024/6/4 下午5:24
|
||||
*/
|
||||
@MappedJdbcTypes(JdbcType.OTHER)
|
||||
@MappedTypes(value = Point.class)
|
||||
public class PointTypeHandler extends AbstractGeometryTypeHandler<Point> {}
|
Loading…
x
Reference in New Issue
Block a user