当前位置: 首页 > wzjs >正文

青龙建站教程免费网站提交入口

青龙建站教程,免费网站提交入口,郑州市最新疫情,军事新闻今日‌前言 MySQL作为最流行的开源关系型数据库,与Spring Boot的整合是企业级开发的标配。本文将手把手教你‌在IntelliJ IDEA中为Spring Boot项目接入MySQL数据库‌,涵盖‌依赖配置‌、‌实体类映射‌、‌JPA操作‌及‌常见避坑指南‌,助你快速…

‌前言

MySQL作为最流行的开源关系型数据库,与Spring Boot的整合是企业级开发的标配。本文将手把手教你‌在IntelliJ IDEA中为Spring Boot项目接入MySQL数据库‌,涵盖‌依赖配置‌、‌实体类映射‌、‌JPA操作‌及‌常见避坑指南‌,助你快速实现数据持久化!


‌一、环境准备

1. ‌基础环境

  • 已安装IntelliJ IDEA并创建Spring Boot项目(参考前文)。
  • 本地安装MySQL 5.7+(推荐8.0),并创建数据库(如springboot_db)。

2. ‌检查依赖

  • 确保项目包含Spring WebSpring Data JPAMySQL Driver依赖(可通过pom.xml添加)。

‌二、添加MySQL依赖

‌1. 修改pom.xml

<dependencies>中添加以下依赖:

<!-- Spring Data JPA -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency><!-- MySQL驱动(版本需与本地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默认使用MySQL 8.x驱动,若使用MySQL 5.x需指定驱动版本(如5.1.49)。


‌三、配置MySQL连接

‌1. 修改application.properties

src/main/resources/application.properties中添加数据库配置:

# 数据源配置
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
  • 关键参数解释‌
    • spring.jpa.hibernate.ddl-auto=update:启动时自动更新表结构(可选createnone)。
    • useSSL=false:禁用SSL(本地开发可关闭)。
    • serverTimezone=UTC:统一时区,避免时间差问题。

‌2. 验证配置

启动项目,若控制台输出以下日志,说明数据库连接成功:

HikariPool-1 - Start completed

‌四、创建实体类与Repository

‌1. 定义实体类(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:标记为JPA实体。
    • @Table:指定映射的表名。
    • @Data:Lombok注解,自动生成getter/setter。

‌2. 创建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. 实现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 {@Autowiredprivate 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 {@Autowiredprivate UserService userService;@PostMappingpublic User createUser(@RequestBody User user) {return userService.saveUser(user);}@GetMapping("/{username}")public User getUser(@PathVariable String username) {return userService.findUserByUsername(username);}
}

‌六、测试与验证

‌1. 启动应用

运行启动类DemoApplication,观察控制台是否生成建表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测试API

  • ‌新增用户‌(POST请求):
    URL:http://localhost:8080/api/users
    Body(JSON):
    {"username": "csdn_user","password": "123456","email": "csdn@example.com"
    }
    
  • 查询用户‌(GET请求):
    URL:http://localhost:8080/api/users/csdn_user

‌七、常见问题与解决方案

‌Q1:数据库连接失败(Access denied)

  • 原因‌:用户名/密码错误,或用户无权限访问数据库。
  • 解决‌
    • 检查application.properties中的usernamepassword
    • 在MySQL中授权用户:
      GRANT ALL PRIVILEGES ON springboot_db.* TO 'root'@'localhost';
      FLUSH PRIVILEGES;
      

‌Q2:驱动类未找到(Driver class not found)

  • 原因‌:MySQL驱动版本与配置不匹配。
  • ‌解决‌
    • 检查spring.datasource.driver-class-name是否为com.mysql.cj.jdbc.Driver(MySQL 8.x)。
    • 确认pom.xml中MySQL依赖未冲突。

Q3:时区错误(ServerTimezone not configured)

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

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

  • 解决‌
    • 确保spring.jpa.hibernate.ddl-auto=update
    • 检查实体类@Table(name="user")是否与数据库表名一致。

总结

通过Spring Data JPA,开发者无需编写SQL即可实现MySQL数据库的CRUD操作。本文从配置到实战演示了完整的接入流程,并针对常见错误提供解决方案。

http://www.dtcms.com/wzjs/255643.html

相关文章:

  • 自己怎样建立个人网站360优化大师官方版
  • 中山如何制作网站百度com百度一下你
  • 江宁网站制作百度网盘网址
  • 做网站前台用什么问题武汉网络推广广告公司
  • 南京农业大学新校区建设网站seo排名怎么样
  • 合肥网站建设哪里有三生网络营销靠谱吗
  • wordpress 部署报错seo网站排名的软件
  • 哪个网站可以领单做效果图软文网站发布平台
  • 驻马店 网站建设近期国内热点新闻事件
  • 界面好看的网站襄阳百度开户
  • 美国主机教育网站建设新闻头条 今天
  • 辽阳好的网站建设公司wordpress建站公司
  • 仿牌外贸网站制作常用的网络营销工具有哪些
  • 西安网站开开发百度打开
  • 那个网站适合学生做兼职惠州seo网站排名
  • wordpress主题零基础宁波企业seo服务
  • 贵阳网站建设哪家公司好网站域名查询网
  • 英文站网站源码营销型网站建设怎么做
  • 长沙做网站的公司免费智能seo收录工具
  • 网站做配置文件的作用seo管理
  • 网站建设服务费属于重庆搜索引擎seo
  • 企业营销型网站建设的可行性分析广东seo网站优化公司
  • 做导购网站多少钱谷歌浏览器网页版在线
  • 好的做网站的优化网站推广排名
  • 厦门高端网站建设定制网络推广好做吗多少钱
  • 五合一建站网站流量分析工具
  • 如何做免费域名网站百度关键词排名批量查询工具
  • 网站如何转做appwindows优化大师软件介绍
  • 网页设计详细步骤seo网站优化优化排名
  • ssh做的网站建立自己的网站平台