如何在Java项目中使用Spring Boot快速连接达梦数据库(DM)

前言

在Java开发领域,Spring Boot凭借其简洁快速的特性成为现代应用开发的首选框架。本文将详细介绍如何在Spring Boot项目中整合JDBC以快速连接达梦数据库(DM),并提供一个简单的示例来验证连接是否成功。

一、环境准备与依赖配置

在开始之前,请确保你的开发环境满足以下条件:

  • 达梦数据库:版本8.0及以上
  • 开发工具:IntelliJ IDEA 2019.3.3 x64 或更高版本
  • JDK:JDK 8
  • Maven:apache-maven-3.5.4 或更新版本
  • Spring Boot:推荐使用2.4.0或更高版本
  • Spring Boot JDBC:与Spring Boot版本匹配
二、添加依赖

首先,在你的pom.xml文件中添加必要的依赖项,以集成Spring Boot、Web支持、JDBC以及达梦数据库的驱动程序。

xml 复制代码
<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <!-- Spring Boot JDBC Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    
    <!-- Spring Boot Web Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Spring Boot DevTools for development-time features -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    
    <!-- Test dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <!-- 达梦数据库驱动,注意根据实际情况调整版本号 -->
    <dependency>
        <groupId>com.dameng</groupId>
        <artifactId>Dm8JdbcDriver18</artifactId>
        <version>8.1.1.193</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/resources/libraries/DmJdbcDriver18-8.1.1.193.jar</systemPath>
    </dependency>
</dependencies>

注意 :你需要从达梦数据库的安装目录dmdbms8/drivers/jdbc下找到DmJdbcDriver18.jar,复制到项目的src/main/resources/libraries目录,并在Maven配置中通过systemPath指定路径。

三、配置数据库连接

接下来,在application.propertiesapplication.yml文件中配置数据库连接信息:

properties 复制代码
spring.datasource.url=jdbc:dm://localhost:5236
spring.datasource.username=SYSDBA
spring.datasource.password=SYSDBA
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
四、编写测试代码

为了验证配置是否正确,可以创建一个简单的控制器类来执行查询操作。

java 复制代码
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class DatabaseController {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @GetMapping("/testConnection")
    public String testDatabaseConnection() {
        List<String> result = jdbcTemplate.queryForList("SELECT 'Connected to DM database successfully!' FROM dual", String.class);
        return result.isEmpty() ? "Connection failed." : result.get(0);
    }
}
五、运行与验证

启动你的Spring Boot应用,然后在浏览器中访问http://localhost:8080/testConnection。如果看到消息"Connected to DM database successfully!",则表明你的Spring Boot应用已成功连接到达梦数据库。

相关推荐
RainbowSea1 天前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea1 天前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑1 天前
Jpa使用union all
java·spring boot·后端
用户3721574261351 天前
Java 实现 Excel 与 TXT 文本高效互转
java
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api
浮游本尊1 天前
Java学习第22天 - 云原生与容器化
java
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
渣哥1 天前
原来 Java 里线程安全集合有这么多种
java
间彧1 天前
Spring Boot集成Spring Security完整指南
java