达梦数据库使用记录

default

连接数据库

达梦客户端连接达梦数据库

  1. 新建连接

图 0

  1. 配置连接信息

图 1

dbeaver 连接达梦数据库

  1. 管理驱动

图 2

  1. 新增驱动

图 3

图 4

图 5

图 6

图 9

图 8

  1. 新建连接

图 10

验证是否开启空间数据支持

1
select dmgeo.ST_POINTFromText('POINT (104.651271 28.718874)' , 4490)

空间函数

官网空间函数文档:https://eco.dameng.com/document/dm/zh-cn/pm/dmgeo-package

达梦数据库优缺点

优点

  1. 国产

缺点

  1. 不适配主流数据库客户端(不可连接,或者需要额外配置环境),如:DBeaver、Navicat、IDEA等;

  2. 空间函数少,缺少常见空间函数支持,如:geojson转geometry,geography支持等;

  3. 整体笨重,创建难,迁移难,运维难;

  4. 公开学习资料少,社区活性低,疑难杂症多;

SpringBoot MyyBatisPlus添加支持达梦数据库

配置后,项目代码中空间几何可全部采用 Geometry 类型,包括 Controller 请求参数和响应内容、Entity中空间几何字段等。

依赖 pom.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<dependency>
    <groupId>org.locationtech.jts</groupId>
    <artifactId>jts-core</artifactId>
    <version>1.20.0<ersion>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson-core -->
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-geojson-core</artifactId>
    <version>27.0<ersion>
</dependency>

-- 仓库配置
<repositories>
    <repository>
        <id>osgeo</id>
        <name>OSGeo Release Repository</name>
        <url>https://repo.osgeo.org/repository/geotools-releases/</url>
    </repository>
</repositories>

支持 Geometry 的序列化和反序列化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@Slf4j
@Configuration
public class JsonConfig {
    /**
     * 创建Jackson对象映射器
     *
     * @param builder Jackson对象映射器构建器
     * @return 增强后的ObjectMapper
     */
    @Bean
    @Primary
    public ObjectMapper getJacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.registerModule(new JtsModule(11));
        //反序列化的时候如果多了其他属性,不抛出异常
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //日期格式处理
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return objectMapper;
    }
}

mybatis-plus支持Geometry字段的读写

 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;
    }
}
Licensed under CC BY-NC-SA 4.0
Comments
  • Latest
  • Oldest
  • Hottest
No comment yet.
Powered by Waline v2.15.8
Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……