最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IDEA中為SpringBoot項(xiàng)目接入MySQL數(shù)據(jù)庫的詳細(xì)指南

 更新時(shí)間:2025年05月26日 10:13:49   作者:一切皆有跡可循  
MySQL作為最流行的開源關(guān)系型數(shù)據(jù)庫,與Spring Boot的整合是企業(yè)級(jí)開發(fā)的標(biāo)配,本文將手把手教你?在IntelliJ IDEA中為Spring Boot項(xiàng)目接入MySQL數(shù)據(jù)庫?,有需要的可以了解下

‌前言

MySQL作為最流行的開源關(guān)系型數(shù)據(jù)庫,與Spring Boot的整合是企業(yè)級(jí)開發(fā)的標(biāo)配。本文將手把手教你‌在IntelliJ IDEA中為Spring Boot項(xiàng)目接入MySQL數(shù)據(jù)庫‌,涵蓋‌依賴配置‌、‌實(shí)體類映射‌、‌JPA操作‌及‌常見避坑指南‌,助你快速實(shí)現(xiàn)數(shù)據(jù)持久化!

‌一、環(huán)境準(zhǔn)備

1. ‌基礎(chǔ)環(huán)境

已安裝IntelliJ IDEA并創(chuàng)建Spring Boot項(xiàng)目(參考文章)。

本地安裝MySQL 5.7+(推薦8.0),并創(chuàng)建數(shù)據(jù)庫(如springboot_db)。

2. ‌檢查依賴

確保項(xiàng)目包含Spring Web、Spring Data JPA和MySQL Driver依賴(可通過pom.xml添加)。

‌二、添加MySQL依賴

修改pom.xml

在<dependencies>中添加以下依賴:

<!-- Spring Data JPA -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- MySQL驅(qū)動(dòng)(版本需與本地MySQL一致) -->
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

<!-- 可選:Lombok簡化代碼 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

‌注意‌:Spring Boot 3.x默認(rèn)使用MySQL 8.x驅(qū)動(dòng),若使用MySQL 5.x需指定驅(qū)動(dòng)版本(如5.1.49)。

‌三、配置MySQL連接

‌1. 修改application.properties

在src/main/resources/application.properties中添加數(shù)據(jù)庫配置:

# 數(shù)據(jù)源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

‌關(guān)鍵參數(shù)解釋‌:

  • spring.jpa.hibernate.ddl-auto=update:啟動(dòng)時(shí)自動(dòng)更新表結(jié)構(gòu)(可選create、none)。
  • useSSL=false:禁用SSL(本地開發(fā)可關(guān)閉)。
  • serverTimezone=UTC:統(tǒng)一時(shí)區(qū),避免時(shí)間差問題。

‌2. 驗(yàn)證配置

啟動(dòng)項(xiàng)目,若控制臺(tái)輸出以下日志,說明數(shù)據(jù)庫連接成功:

HikariPool-1 - Start completed

‌四、創(chuàng)建實(shí)體類與Repository

‌1. 定義實(shí)體類(User)

package com.example.demo.entity;

import jakarta.persistence.*;
import lombok.Data;

@Data
@Entity
@Table(name = "user") // 指定表名
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(nullable = false, unique = true)
    private String username;
    
    @Column(nullable = false)
    private String password;
    
    private String email;
}

‌注解說明‌:

  • @Entity:標(biāo)記為JPA實(shí)體。
  • @Table:指定映射的表名。
  • @Data:Lombok注解,自動(dòng)生成getter/setter。

‌2. 創(chuàng)建Repository接口

package com.example.demo.repository;

import com.example.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
    // 自定義查詢方法(按用戶名查找)
    User findByUsername(String username);
}

五、編寫Service與Controller

‌1. 實(shí)現(xiàn)Service層

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public User saveUser(User user) {
        return userRepository.save(user);
    }

    public User findUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }
}

‌2. 編寫RESTful Controller

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.saveUser(user);
    }

    @GetMapping("/{username}")
    public User getUser(@PathVariable String username) {
        return userService.findUserByUsername(username);
    }
}

‌六、測(cè)試與驗(yàn)證

‌1. 啟動(dòng)應(yīng)用

運(yùn)行啟動(dòng)類DemoApplication,觀察控制臺(tái)是否生成建表SQL:

create table user (
    id bigint not null auto_increment,
    email varchar(255),
    password varchar(255) not null,
    username varchar(255) not null unique,
    primary key (id)
);

‌2. 使用Postman測(cè)試API

‌新增用戶‌(POST請(qǐng)求):URL:http://localhost:8080/api/users

Body(JSON):

{
  "username": "csdn_user",
  "password": "123456",
  "email": "csdn@example.com"
}

‌查詢用戶‌(GET請(qǐng)求):URL:http://localhost:8080/api/users/csdn_user

‌七、常見問題與解決方案

‌Q1:數(shù)據(jù)庫連接失?。ˋccess denied)

原因‌:用戶名/密碼錯(cuò)誤,或用戶無權(quán)限訪問數(shù)據(jù)庫。

‌解決‌:

檢查application.properties中的username和password。

在MySQL中授權(quán)用戶:

GRANT ALL PRIVILEGES ON springboot_db.* TO 'root'@'localhost';
FLUSH PRIVILEGES;

‌Q2:驅(qū)動(dòng)類未找到(Driver class not found)

原因‌:MySQL驅(qū)動(dòng)版本與配置不匹配。

‌解決‌:

檢查spring.datasource.driver-class-name是否為com.mysql.cj.jdbc.Driver(MySQL 8.x)。

確認(rèn)pom.xml中MySQL依賴未沖突。

Q3:時(shí)區(qū)錯(cuò)誤(ServerTimezone not configured)

‌解決‌:在JDBC URL中添加&serverTimezone=Asia/Shanghai(或UTC)。

‌Q4:表不存在(Table ‘springboot_db.user’ doesn’t exist)

解決‌:

確保spring.jpa.hibernate.ddl-auto=update。

檢查實(shí)體類@Table(name="user")是否與數(shù)據(jù)庫表名一致。

總結(jié)

通過Spring Data JPA,開發(fā)者無需編寫SQL即可實(shí)現(xiàn)MySQL數(shù)據(jù)庫的CRUD操作。本文從配置到實(shí)戰(zhàn)演示了完整的接入流程,并針對(duì)常見錯(cuò)誤提供解決方案。

到此這篇關(guān)于IDEA中為SpringBoot項(xiàng)目接入MySQL數(shù)據(jù)庫的詳細(xì)指南的文章就介紹到這了,更多相關(guān)IDEA SpringBoot接入MySQL數(shù)據(jù)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java打亂數(shù)組元素簡單代碼例子

    Java打亂數(shù)組元素簡單代碼例子

    在Java編程中,我們經(jīng)常需要對(duì)數(shù)組進(jìn)行亂序操作(即將數(shù)組中的元素隨機(jī)打亂順序),這篇文章主要給大家介紹了關(guān)于Java打亂數(shù)組元素的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • SpringAI+Ollama本地模型實(shí)現(xiàn)快速對(duì)話的實(shí)戰(zhàn)指南

    SpringAI+Ollama本地模型實(shí)現(xiàn)快速對(duì)話的實(shí)戰(zhàn)指南

    SpringAI是Spring官方推出的AI應(yīng)用開發(fā)框架,旨在統(tǒng)一封裝多種大模型,提供一套代碼無縫切換不同模型的功能,下面我們就來看看SpringAI如何結(jié)合Ollama實(shí)現(xiàn)快速對(duì)話功能吧
    2026-05-05
  • 在mybatis中使用mapper進(jìn)行if條件判斷

    在mybatis中使用mapper進(jìn)行if條件判斷

    這篇文章主要介紹了在mybatis中使用mapper進(jìn)行if條件判斷,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Java實(shí)現(xiàn)克魯斯卡爾算法的示例代碼

    Java實(shí)現(xiàn)克魯斯卡爾算法的示例代碼

    克魯斯卡爾算法是一種用于求解最小生成樹問題的貪心算法。這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)克魯斯卡爾算法的方法,需要的可以參考一下
    2023-04-04
  • Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法

    Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法

    這篇文章主要介紹了Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05
  • springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)

    springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)

    這篇文章主要為大家詳細(xì)介紹了springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能,比較基礎(chǔ)的學(xué)習(xí)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • IDEA的下載和使用安裝詳細(xì)圖文教程

    IDEA的下載和使用安裝詳細(xì)圖文教程

    這篇文章主要介紹了IDEA的下載和使用安裝,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 關(guān)于mybatis3中@SelectProvider的使用問題

    關(guān)于mybatis3中@SelectProvider的使用問題

    這篇文章主要介紹了mybatis3中@SelectProvider的使用技巧,@SelectProvide指定一個(gè)Class及其方法,并且通過調(diào)用Class上的這個(gè)方法來獲得sql語句,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-12-12
  • SpringBoot實(shí)現(xiàn)動(dòng)態(tài)多線程并發(fā)定時(shí)任務(wù)

    SpringBoot實(shí)現(xiàn)動(dòng)態(tài)多線程并發(fā)定時(shí)任務(wù)

    這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)動(dòng)態(tài)多線程并發(fā)定時(shí)任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • SpringMVC整合SSM實(shí)現(xiàn)表現(xiàn)層數(shù)據(jù)封裝詳解

    SpringMVC整合SSM實(shí)現(xiàn)表現(xiàn)層數(shù)據(jù)封裝詳解

    這篇文章主要介紹了SpringMVC整合SSM實(shí)現(xiàn)表現(xiàn)層數(shù)據(jù)封裝,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10

最新評(píng)論

盐边县| 大渡口区| 新化县| 宜昌市| 略阳县| 佳木斯市| 盐亭县| 江孜县| 柏乡县| 信阳市| 库尔勒市| 黄骅市| 安西县| 云林县| 保靖县| 玉山县| 军事| 彭山县| 星子县| 安康市| 武冈市| 盘山县| 蕲春县| 海林市| 古丈县| 彰化县| 上栗县| 湖南省| 松溪县| 汝阳县| 齐齐哈尔市| 封开县| 祁连县| 缙云县| 新丰县| 康乐县| 屏东市| 阿图什市| 桐梓县| 崇州市| 荔浦县|