1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import cjw.nic.niceasy.frame.common.util.SpringContextUtils;
import dm.jdbc.driver.DmdbBlob;
import dm.jdbc.driver.DmdbConnection;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ByteOrderValues;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;
import javax.sql.DataSource;
import java.sql.*;
/**
* geometry 类型处理程序
*
* @author guoyingdong
* @date 2025/05/13
*/
@MappedTypes({Geometry.class})
@MappedJdbcTypes({JdbcType.STRUCT})
@Slf4j
public class GeometryTypeHandler extends BaseTypeHandler<Geometry> {
private static DmdbConnection connection = null;
private static DmdbConnection getConnection() throws SQLException {
synchronized (GeometryTypeHandler.class) {
if (connection == null) {
DataSource dataSource = SpringContextUtils.getApplicationContext().getBean(DataSource.class);
connection = dataSource.getConnection().unwrap(DmdbConnection.class);
}
}
return connection;
}
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Geometry geom, JdbcType jdbcType) throws SQLException {
// Geometry转WKB
byte[] bytes = new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN, false).write(geom);
Object[] attrs = new Object[3];
attrs[0] = 4490;
DmdbConnection connection = getConnection();
attrs[1] = DmdbBlob.newInstanceOfLocal(bytes, connection);
attrs[2] = 6;
//通过指定空间数据类型名称和结构体成员创建java.sql.Struct类型对象
Struct struct = connection.createStruct("ST_GEOMETRY", attrs);
preparedStatement.setObject(i, struct);
}
@SneakyThrows
@Override
public Geometry getNullableResult(ResultSet resultSet, String s) throws SQLException {
Struct struct = (Struct) resultSet.getObject(s);
Object[] attrs = struct.getAttributes();
int srid = (Integer) attrs[0];
Blob blob = (Blob) attrs[1];
int type = (Integer) attrs[2];
WKBReader reader = new WKBReader();
Geometry geometry = reader.read(blob.getBytes(1, (int) blob.length()));
geometry.setSRID(srid);
return geometry;
}
@SneakyThrows
@Override
public Geometry getNullableResult(ResultSet resultSet, int i) throws SQLException {
Struct struct = (Struct) resultSet.getObject(i);
Object[] attrs = struct.getAttributes();
int srid = (Integer) attrs[0];
Blob blob = (Blob) attrs[1];
int type = (Integer) attrs[2];
WKBReader reader = new WKBReader();
Geometry geometry = reader.read(blob.getBytes(1, (int) blob.length()));
geometry.setSRID(srid);
return geometry;
}
@Override
public Geometry getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return null;
}
}
|
Preview: