在?Spring?Boot?中連接?MySQL?數(shù)據(jù)庫(kù)的詳細(xì)步驟
在 Spring Boot 中連接 MySQL 數(shù)據(jù)庫(kù)是一個(gè)常見(jiàn)的任務(wù)。Spring Boot 提供了自動(dòng)配置功能,使得連接 MySQL 數(shù)據(jù)庫(kù)變得非常簡(jiǎn)單。以下是詳細(xì)的步驟:
一、添加依賴(lài)
首先,確保你的pom.xml文件中包含了 Spring Boot 的 Starter Data JPA 和 MySQL 驅(qū)動(dòng)依賴(lài)。
<dependencies>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>二、配置數(shù)據(jù)庫(kù)連接
在application.properties或application.yml文件中配置數(shù)據(jù)庫(kù)連接信息。
1.使用application.properties
# 數(shù)據(jù)庫(kù)連接配置 spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC spring.datasource.username=your_username spring.datasource.password=your_password # JPA 配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true
2.使用application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC
username: your_username
password: your_password
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
format_sql: true三、創(chuàng)建實(shí)體類(lèi)
創(chuàng)建一個(gè)實(shí)體類(lèi)來(lái)映射數(shù)據(jù)庫(kù)表。例如,創(chuàng)建一個(gè)User實(shí)體類(lèi):
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}四、創(chuàng)建倉(cāng)庫(kù)接口
創(chuàng)建一個(gè)倉(cāng)庫(kù)接口來(lái)操作數(shù)據(jù)庫(kù)。Spring Data JPA 會(huì)自動(dòng)實(shí)現(xiàn)這個(gè)接口。
package com.example.demo.repository;
import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}五、創(chuàng)建服務(wù)類(lèi)
創(chuàng)建一個(gè)服務(wù)類(lèi)來(lái)處理業(yè)務(wù)邏輯。
package com.example.demo.service;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers() {
return userRepository.findAll();
}
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
public User saveUser(User user) {
return userRepository.save(user);
}
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
}六、創(chuàng)建控制器
創(chuàng)建一個(gè)控制器來(lái)處理 HTTP 請(qǐng)求。
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
@PostMapping
public User createUser(@RequestBody User user) {
return userService.saveUser(user);
}
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
return userService.saveUser(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
}
}七、運(yùn)行應(yīng)用程序
確保你的 MySQL 數(shù)據(jù)庫(kù)正在運(yùn)行,并且已經(jīng)創(chuàng)建了相應(yīng)的數(shù)據(jù)庫(kù)和表。然后運(yùn)行你的 Spring Boot 應(yīng)用程序。
mvn spring-boot:run
八、測(cè)試 API
使用 Postman 或其他工具測(cè)試你的 API。例如:
• 獲取所有用戶(hù):
• GEThttp://localhost:8080/users
• 獲取單個(gè)用戶(hù):
• GEThttp://localhost:8080/users/{id}
• 創(chuàng)建用戶(hù):
• POSThttp://localhost:8080/users
• Body:
{
"name": "John Doe",
"email": "john.doe@example.com"
}
```
? 更新用戶(hù):
? PUT`http://localhost:8080/users/{id}`
? Body:
```json
{
"name": "Jane Doe",
"email": "jane.doe@example.com"
}• 刪除用戶(hù):
• DELETEhttp://localhost:8080/users/{id}
九、常見(jiàn)問(wèn)題
1.數(shù)據(jù)庫(kù)連接失敗
• 確保 MySQL 服務(wù)正在運(yùn)行。
• 檢查application.properties或application.yml文件中的數(shù)據(jù)庫(kù)連接信息是否正確。
• 確保 MySQL 用戶(hù)具有訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)的權(quán)限。
2.數(shù)據(jù)庫(kù)表未自動(dòng)創(chuàng)建
• 確保spring.jpa.hibernate.ddl-auto配置正確。例如,update會(huì)自動(dòng)創(chuàng)建表,create會(huì)刪除現(xiàn)有表并重新創(chuàng)建。
• 確保實(shí)體類(lèi)的注解正確。
十、總結(jié)
通過(guò)上述步驟,你可以在 Spring Boot 中成功連接并操作 MySQL 數(shù)據(jù)庫(kù)。Spring Boot 的自動(dòng)配置功能使得連接數(shù)據(jù)庫(kù)變得非常簡(jiǎn)單,你只需要添加必要的依賴(lài)、配置數(shù)據(jù)庫(kù)連接信息、創(chuàng)建實(shí)體類(lèi)、倉(cāng)庫(kù)接口和服務(wù)類(lèi),即可實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的操作。
到此這篇關(guān)于在 Spring Boot 中連接 MySQL 數(shù)據(jù)庫(kù)的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)Spring Boot 連接 MySQL 數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解如何在SpringBoot中配置MySQL數(shù)據(jù)庫(kù)的連接數(shù)
- SpringBoot解決mysql連接8小時(shí)問(wèn)題
- SpringBoot連接MySql數(shù)據(jù)庫(kù)的原理及代碼示例
- SpringBoot項(xiàng)目如何連接MySQL8.0數(shù)據(jù)庫(kù)
- SpringBoot連接MySQL獲取數(shù)據(jù)寫(xiě)后端接口的操作方法
- 教你用springboot連接mysql并實(shí)現(xiàn)增刪改查
- springboot配置mysql連接的實(shí)例代碼
- 解決springboot 連接 mysql 時(shí)報(bào)錯(cuò) using password: NO的方案
相關(guān)文章
用Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字
這篇文章主要介紹了用Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字,需要的朋友可以參考下2017-02-02
BufferedWriter如何使用write方法實(shí)現(xiàn)換行
這篇文章主要介紹了BufferedWriter如何使用write方法實(shí)現(xiàn)換行的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java實(shí)現(xiàn)Treap樹(shù)的示例代碼
本文主要介紹了Java實(shí)現(xiàn)Treap樹(shù)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Java SpringBoot實(shí)現(xiàn)文件上傳功能的示例代碼
這篇文章主要介紹了如何利用Java SpringBoot實(shí)現(xiàn)文件上傳功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-03-03
Java Swing JList列表框的實(shí)現(xiàn)
這篇文章主要介紹了Java Swing JList列表框的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
基于java ssm springboot+mybatis酒莊內(nèi)部管理系統(tǒng)設(shè)計(jì)和實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了java ssm springboot+mybatis實(shí)現(xiàn)酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
java.lang.IncompatibleClassChangeError異常的問(wèn)題解決
本文主要介紹了java.lang.IncompatibleClassChangeError異常的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
SpringBoot訂單超時(shí)自動(dòng)取消的三種主流實(shí)現(xiàn)方案
在電商、外賣(mài)、票務(wù)等業(yè)務(wù)中,下單后若 30 分鐘未支付則自動(dòng)取消是一道經(jīng)典需求,實(shí)現(xiàn)方式既要保證 實(shí)時(shí)性,又要在 高并發(fā) 下保持 低成本、高可靠,本文基于 Spring Boot,給出 3 種生產(chǎn)級(jí)落地方案,并附完整代碼與選型對(duì)比,需要的朋友可以參考下2025-07-07

