spring boot+vue3+达梦数据库(国产)第一次尝试
我目前已经使用过spring boot,和vue3,但以前一直使用的是MySQL数据库。现在在国产化浪潮中,有必要尝试学会使用国产的数据库,这次我选择使用的是达梦数据库(DMDB)。
依旧是使用api,spring boot+vue的前后端分离。
大部分与连接MySQL时相似。具体不同体现在:
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver11</artifactId>
<version>11.0.0</version>
</dependency>
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmDialect</artifactId>
<version>6.6.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.6.0.Final</version>
</dependency>
其中两项是要自己导入maven仓库的关于达梦数据库的驱动以及方言。
我只做了十分简单的操作来验证我的技术栈是否可行:
后端
ApiController:
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.Entity.User;
import com.example.demo.Repository.UserRepository;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.http.ResponseEntity;
@RestController
@RequestMapping("/api")
public class ApiController {
@Autowired
private DataSource dataSource;
@Autowired
private UserRepository userRepository;
// 完整路径将是 /api/test
@GetMapping("/test")
public Map<String, Object> testConnection() {
Map<String, Object> response = new HashMap<>();
response.put("status", "success");
response.put("message", "后端API已成功连接!");
response.put("timestamp", System.currentTimeMillis());
return response;
}
@Autowired
private JdbcTemplate jdbcTemplate;
// 新增数据库连接测试API
@GetMapping("/db-test")
public Map<String, Object> testDbConnection() {
Map<String, Object> response = new HashMap<>();
try (Connection conn = dataSource.getConnection()) {
response.put("status", "success");
response.put("message", "达梦数据库连接成功"); // 文本内容封装为JSON字段
} catch (SQLException e) {
response.put("status", "error");
response.put("message", "连接失败: " + e.getMessage());
}
return response; // Spring Boot 自动转换为JSON
}
}
User:
package com.example.demo.Entity;
import jakarta.persistence.*;
@Entity
@Table(name = "\"USER\"", schema = "TEST") // 达梦需用双引号处理大小写敏感
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
private Integer userId;
@Column(name = "USERNAME", length = 50, nullable = false)
private String username;
@Column(name = "PASSWORD", length = 100, nullable = false)
private String password;
@Column(name = "ROLE_CODE", length = 20)
private String roleCode;
public User() {
}
public User(String username, String password, String roleCode) {
this.username = username;
this.password = password;
this.roleCode = roleCode;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRoleCode() {
return roleCode;
}
public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", username='" + username + '\'' +
", roleCode='" + roleCode + '\'' +
'}';
}
}
UserRepository:
package com.example.demo.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.example.demo.Entity.User;
public interface UserRepository extends JpaRepository<User, Integer> {
User findByUsername(String username);
@Query(value = "SELECT version FROM v$version WHERE id = 1", nativeQuery = true)
String getDatabaseVersion();
}
前端:(css文件不显示其中)
App.vue:
<template>
<div id="app">
<h1>Vue3 + Spring Boot 连通性测试</h1>
<ApiTest />
</div>
</template>
<script>
import ApiTest from './components/ApiTest.vue'
export default {
name: 'App',
components: {
ApiTest
}
}
</script>
ApiTest.vue:
<template>
<div class="api-test">
<!-- 测试按钮 -->
<div class="button-group">
<button @click="testApi">测试API连通性</button>
<button @click="testDbConnection">测试数据库连接</button>
</div>
<!-- 加载状态 -->
<div v-if="loading" class="loading">请求中...</div>
<!-- 连接状态 -->
<div v-if="status" :class="['status-box', statusClass]">
{{ statusMessage }}
</div>
<!-- 后端响应显示 -->
<div v-if="response" class="response-box">
<h3>后端响应:</h3>
<pre>{{ JSON.stringify(response, null, 2) }}</pre>
</div>
<!-- 错误信息显示 -->
<div v-if="error" class="error-box">
<h3>错误信息:</h3>
<pre>{{ error }}</pre>
</div>
</div>
</template>
<script>
export default {
data() {
return {
loading: false,
response: null,
error: null,
status: null, // 连接状态(success/error)
statusMessage: "", // 状态消息文本
statusClass: "" // 状态框样式类
};
},
methods: {
async testApi() {
this.resetState();
this.loading = true;
try {
const res = await fetch('/api/test');
if (!res.ok) {
const errorText = await res.text();
throw new Error(`HTTP错误! 状态码: ${res.status} - ${errorText}`);
}
this.response = await res.json();
this.status = "success";
this.statusMessage = "API连接成功";
this.statusClass = "success";
} catch (err) {
this.status = "error";
this.statusMessage = "API连接失败";
this.statusClass = "error";
this.error = err.message;
} finally {
this.loading = false;
}
},
async testDbConnection() {
this.resetState();
this.loading = true;
try {
const res = await fetch('/api/db-test');
if (!res.ok) {
const errorText = await res.text();
throw new Error(`HTTP错误! 状态码: ${res.status} - ${errorText}`);
}
this.response = await res.json();
this.status = "success";
this.statusMessage = "数据库连接成功";
this.statusClass = "success";
} catch (err) {
this.status = "error";
this.statusMessage = "数据库连接失败";
this.statusClass = "error";
this.error = err.message;
} finally {
this.loading = false;
}
},
// 重置所有状态
resetState() {
this.loading = false;
this.response = null;
this.error = null;
this.status = null;
this.statusMessage = "";
this.statusClass = "";
}
}
};
</script>
这样就能简单地验证是否能使用这些技术栈,能作为我自己第一次使用国产数据库的尝试: