第一个搭建SpringBoot项目(连接mysql)
首先新建项目找到Spring Initializr 我使用的URL是https://start.spring.io这里最低的JDK版本是17,而且当网速不好的时候可能会显示超时,这里可以选用阿里云的镜像https://start.aliyun.com可以更快一些但是里面还是有一些区别的

我们这里选择Java语言,Maven框架

在这里我们选择一些我们需要用到的
这个版本可以选低一点更加稳定

进来之后我们需要先等pom文件加载,我们可以选择刷新Maven
加载完之后由于我们加载了MySQL所以还不能运行可以先注释掉,然后刷新maven

后面就可以运行了,但是它默认的是8080端口,有可能会有被占用的我们可以更改端口

后面既可以运行了

运行结果就是这样的后面没有了,刚开始由于我学习的时候和别人运行的不一样,还以为是卡住了,调试了好久好久,结果发现这就已经是运行了
下面我们就可以去页面看看结果了

用阿里云镜像的输出会有点不一样但是没有问题。
下面我们就可以创建一个测试类

新建一个TestController
@RestController
public class TestController {@GetMapping("/hello")public String hello(){return "hello word!";}
}
再去页面观察

rest api规范
路径
路径又称"终点"(endpoint),表示API的具体网址。在RESTfuI架构中,每个网址代表一种资源(resource),所以网址中不能有动词,只能有名词,而且所用的名词往往与数据库的表格名对应。
Http 动词
GET(SELECT):从服务器取出资源(一项或多项)。
POST(CREATE):在服务器新建一个资源。
PUT(UPDATE):在服务器更新资源(客户端提供改变后的完整资源)。。PATCH(UPDATE):在服务器更新资源(客户端提供改变的属性)
DELETE(DELETE):从服务器删除资源。
新建一个数据库,创建表student

使用mysql语句是这样的

创建


在这里我遇到的问题是JpaRepository我继承不了,然后不断的查找问题是更改pom文件里面的内容
加入
org.springframework.data
spring-data-jpa

然后刷新一下maven就好了
创建Student

Student
package com.example.mysqldemo3.dao;import jakarta.persistence.*;import static jakarta.persistence.GenerationType.IDENTITY;@Entity
@Table(name = "student")
public class Student {@Id@Column(name = "id")@GeneratedValue(strategy = IDENTITY)private long id;@Column(name = "name")private String name;@Column(name = "email")private String email;@Column(name = "age")private int age;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;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
返回到接口补全代码

package com.example.mysqldemo3.dao;import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;@Repository
public interface StudentRepository extends JpaRepository<Student,Long> {}
Service层

StudentService
package com.example.mysqldemo3.service;import com.example.mysqldemo3.dao.Student;public interface StudentService {Student getStudentById(long id);}
StudentServiceImpl
package com.example.mysqldemo3.service;import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dao.StudentRepository;
import jakarta.persistence.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class StudentServiceImpl implements StudentService{@Autowiredprivate StudentRepository StudentRepository;@Overridepublic Student getStudentById(long id) {return StudentRepository.findById(id).orElseThrow(RuntimeException::new) ;}
}
controller层

package com.example.mysqldemo3.controller;import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class StudentController {@Autowiredprivate StudentService studentService;@GetMapping("/student/{id}")public Student getStudentById(@PathVariable long id){return studentService.getStudentById(id);}
}
连接数据库这些配置写在application里面
数据库
spring.application.name=mysql-demo3
server.port=8085
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名字?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=sgz250hhh
在数据库中填入数据

后面就可以去网页测试了

再往后我们需要做一些信息的隐藏,不想去展示这莫多的数据
创建

StudentDTO进行封装
package com.example.mysqldemo3.dto;public class StudentDTO {private long id;private String name;private String email;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;}
}
新建converter将student的数据进行转换成DTO

StudentConverter
package com.example.mysqldemo3.converter;import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dto.StudentDTO;public class StudentConverter {public static StudentDTO converStudent(Student student){//student转换成DTO对象StudentDTO studentDTO = new StudentDTO();studentDTO.setId(student.getId());studentDTO.setName(student.getName());studentDTO.setEmail(student.getEmail());return studentDTO;}
}
新建
![]()
判断是否查询成功
Response
package com.example.mysqldemo3;public class Response <T>{//返回后端的一些格式private T data;private boolean success;private String errorMsg;public static <K> Response<K> newSuccess(K data){//返回成功Response<K> response = new Response<>();response.setData(data);response.setSuccess(true);return response;}public static Response<Void> newFail(String errorMsg){//返回失败Response<Void> response = new Response<>();response.setErrorMsg(errorMsg);response.setSuccess(false);return response;}public T getData() {return data;}public void setData(T data) {this.data = data;}public boolean isSuccess() {return success;}public void setSuccess(boolean success) {this.success = success;}public String getErrorMsg() {return errorMsg;}public void setErrorMsg(String errorMsg) {this.errorMsg = errorMsg;}
}
StudentService
package com.example.mysqldemo3.service;import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dto.StudentDTO;public interface StudentService {StudentDTO getStudentById(long id);}
StudentServiceImpl
package com.example.mysqldemo3.service;import com.example.mysqldemo3.converter.StudentConverter;
import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dao.StudentRepository;
import com.example.mysqldemo3.dto.StudentDTO;
import jakarta.persistence.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class StudentServiceImpl implements StudentService{@Autowiredprivate StudentRepository StudentRepository;@Overridepublic StudentDTO getStudentById(long id) {Student student = StudentRepository.findById(id).orElseThrow(RuntimeException::new);return StudentConverter.converStudent(student);}
}
StudentController
package com.example.mysqldemo3.controller;import com.example.mysqldemo3.Response;
import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dto.StudentDTO;
import com.example.mysqldemo3.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class StudentController {@Autowiredprivate StudentService studentService;@GetMapping("/student/{id}")public Response<StudentDTO> getStudentById(@PathVariable long id){return Response.newSuccess(studentService.getStudentById(id));}
}
刷新页面
就可以看见age被隐藏起来了

后面的就是增加删除和修改的操作可以使用Postman进行接口测试,可以直接官网下载
StudentServiceImpl
package com.example.mysqldemo3.service;import com.example.mysqldemo3.converter.StudentConverter;
import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dao.StudentRepository;
import com.example.mysqldemo3.dto.StudentDTO;
import jakarta.persistence.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;import java.util.List;@Service
public class StudentServiceImpl implements StudentService{@Autowiredprivate StudentRepository StudentRepository;@Overridepublic StudentDTO getStudentById(long id) {Student student = StudentRepository.findById(id).orElseThrow(RuntimeException::new);return StudentConverter.converStudent(student);}@Overridepublic Long addNewStudent(StudentDTO studentDTO) {List<Student> studentList = StudentRepository.findByEmail(studentDTO.getEmail());if (!CollectionUtils.isEmpty(studentList)){throw new IllegalStateException("email:"+studentDTO.getEmail()+"has been taken");}Student student = StudentRepository.save(StudentConverter.converStudent(studentDTO));return student.getId();}@Overridepublic void deleteStudentById(long id) {StudentRepository.findById(id).orElseThrow(()->new IllegalArgumentException("id"+id+"doesn`s exist!"));StudentRepository.deleteById(id);}@Overridepublic StudentDTO updateStudentById(long id, String name, String email) {Student studentDB = StudentRepository.findById(id).orElseThrow(()->new IllegalArgumentException("id"+id+"doesn`s exist!"));if (StringUtils.hasLength(name) && !studentDB.getName().equals(name)){studentDB.setName(name);}if(StringUtils.hasLength(email) && !studentDB.getEmail().equals(email)){studentDB.setEmail(email);}Student student = StudentRepository.save(studentDB);return StudentConverter.converStudent(student);}}
StudentService
package com.example.mysqldemo3.service;import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dto.StudentDTO;public interface StudentService {StudentDTO getStudentById(long id);Long addNewStudent(StudentDTO studentDTO);void deleteStudentById(long id);StudentDTO updateStudentById(long id, String name, String email);
}
StudentRepository
package com.example.mysqldemo3.dao;import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;import java.util.List;@Repository
public interface StudentRepository extends JpaRepository<Student,Long> {List<Student> findByEmail(String emile);}
StudentConverter
package com.example.mysqldemo3.converter;import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dto.StudentDTO;public class StudentConverter {public static StudentDTO converStudent(Student student){//student转换成DTO对象StudentDTO studentDTO = new StudentDTO();studentDTO.setId(student.getId());studentDTO.setName(student.getName());studentDTO.setEmail(student.getEmail());return studentDTO;}public static Student converStudent(StudentDTO StudentDTO){//student转换成DTO对象Student student = new Student();student.setName(StudentDTO.getName());student.setEmail(StudentDTO.getEmail());return student;}
}
StudentController
package com.example.mysqldemo3.controller;import com.example.mysqldemo3.Response;
import com.example.mysqldemo3.dao.Student;
import com.example.mysqldemo3.dto.StudentDTO;
import com.example.mysqldemo3.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
public class StudentController {@Autowiredprivate StudentService studentService;@GetMapping("/student/{id}")public Response<StudentDTO> getStudentById(@PathVariable long id){return Response.newSuccess(studentService.getStudentById(id));}@PostMapping("/student")public Response<Long> addNewStudent(@RequestBody StudentDTO studentDTO){return Response.newSuccess(studentService.addNewStudent(studentDTO));}@DeleteMapping("/student/{id}")public void deleteStudentById(@PathVariable long id){studentService.deleteStudentById(id);}@PutMapping("/student/{id}")public Response<StudentDTO> updateStudentById(@PathVariable long id,@RequestParam(required = false) String name,@RequestParam(required = false) String email){return Response.newSuccess(studentService.updateStudentById(id,name,email));}
}


最后就可以使用maven对文件进行打包了
相关文章:
第一个搭建SpringBoot项目(连接mysql)
首先新建项目找到Spring Initializr 我使用的URL是https://start.spring.io这里最低的JDK版本是17,而且当网速不好的时候可能会显示超时,这里可以选用阿里云的镜像https://start.aliyun.com可以更快一些但是里面还是有一些区别的 我们这里选择Java语言&a…...
docker部署rabbitMQ 单机版
获取rabbit镜像:我们选择带有“mangement”的版本(包含web管理页面); docker pull rabbitmq:management 创建并运行容器: docker run -d --name rabbitmq -p 5677:5672 -p 15677:15672 rabbitmq:management --name:…...
PDF 全文多语言 AI 摘要 API 数据接口
PDF 全文多语言 AI 摘要 API 数据接口 PDF / 文本摘要 AI 生成 PDF 文档摘要 AI 处理 / 智能摘要。 1. 产品功能 支持多语言摘要生成;支持 formdata 格式 PDF 文件流传参;快速处理大文件;基于 AI 模型,持续迭代优化;…...
《信息系统安全》课程实验指导
第1关:实验一:古典密码算法---代换技术 任务描述 本关任务:了解古典密码体制技术中的代换技术,并编程实现代换密码的加解密功能。 注意所有明文字符为26个小写字母,也就是说字母表为26个小写字母。 相关知识 为了完…...
Accelerated Soft Error Testing 介绍
加速软错误测试(Accelerated Soft Error Testing, ASET)是一种评估半导体器件或集成电路(ICs)在高辐射环境中发生软错误率(Soft Error Rate, SER)的方法。这种测试方法通过模拟或加速软错误的发生,以便在较短时间内评估器件的可靠性。软错误指的是那些不会对硬件本身造成…...
Redis缓存常用的读写策略
缓存常用的读写策略 缓存与DB的数据不一致问题,大多数都是指DB的数据已经修改,而缓存中的数据还是旧数据的情况。 旁路缓存模式 对于读操作:基本上所有模式都是先尝试从缓存中读,没有的话再去DB读取,然后写到缓存中…...
9月产品更新 | 超10项功能升级,快来看看你的需求上线了吗?
Smartbi用户可以在官网(PC端下载),更新后便可以使用相关功能,也可以在官网体验中心体验相关功能。 接下来,我们一起来看看都有哪些亮点功能更新吧。 ▎插件商城 Smartbi麦粉社区的应用市场新增了“插件”模块…...
ARP协议工作原理析解 (详细抓包分析过程)
目录 1. ARP 协议 2. 工作原理 3. ARP 协议报文格式 4. ARP 缓存的查看和修改 5. tcpdump 抓包分析 ARP 协议工作原理 5.1 搭建 2 台虚拟机 5.2 在主机 192.168.0.155 打开一个shell命令行开启抓包监听 5.3 在主机 192.168.0.155 打开另一个shell命令行 telnet 192.168.…...
axure动态面板
最近转管理岗了,作为项目负责人,需要常常与客户交流沟通,这时候画原型的能力就是不可或缺的本领之一了,关于axure可能很多it行业者都不是很陌生,简单的功能呢大家就自行去摸索,我们这次从动态面板开始讲起。…...
[论文笔记]Making Large Language Models A Better Foundation For Dense Retrieval
引言 今天带来北京智源研究院(BAAI)团队带来的一篇关于如何微调LLM变成密集检索器的论文笔记——Making Large Language Models A Better Foundation For Dense Retrieval。 为了简单,下文中以翻译的口吻记录,比如替换"作者"为"我们&quo…...
Linux平台屏幕|摄像头采集并实现RTMP推送两种技术方案探究
技术背景 随着国产化操作系统的推进,市场对国产化操作系统下的生态构建,需求越来越迫切,特别是音视频这块,今天我们讨论的是如何在linux平台实现屏幕|摄像头采集,并推送至RTMP服务。 我们知道,Linux平台&…...
梧桐数据库|中秋节活动·抽奖领取大闸蟹
有话说 众所周不知,我的工作就是做一个国产的数据库产品—中国移动梧桐数据库(简称WuTongDB)。 近期我们举办了一次小活动,来提升梧桐数据库的搜索量和知名度,欢迎大家来参加,免费抽奖领取大闸蟹哦~~~ 具…...
Python怎么发送邮件:基础步骤与详细教程?
Python怎么发送邮件带附件?怎么使用Python发送邮件? 无论是工作中的通知、报告,还是生活中的问候、邀请,电子邮件都扮演着不可或缺的角色。那么,Python怎么发送邮件呢?AokSend将详细介绍Python发送邮件的基…...
[译] 大模型推理的极限:理论分析、数学建模与 CPU/GPU 实测(2024)
译者序 本文翻译自 2024 年的一篇文章: LLM inference speed of light, 分析了大模型推理的速度瓶颈及量化评估方式,并给出了一些实测数据(我们在国产模型上的实测结果也大体吻合), 对理解大模型推理内部工…...
vue3 响应式 API:readonly() 与 shallowReadonly()
readonly() readonly()是一个用于创建只读代理对象的函数。它接受一个对象 (不论是响应式还是普通的) 或是一个 ref,返回一个原值的只读代理。 类型 function readonly<T extends object>(target: T ): DeepReadonly<UnwrapNestedRefs<T>>以下…...
迁移学习与知识蒸馏对比
应用场景不同 迁移学习:通常用于不同但相关的任务之间的知识迁移。特别是当目标任务的数据量不足时,可以从一个已经在大规模数据上训练好的模型中获取有用的特征或参数。典型场景包括计算机视觉任务,比如你在ImageNet上训练了一个ResNet&…...
【Java-反射】
什么是反射? JAVA反射机制是在运行状态中,创建任意一个类,能获取这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言…...
移动UI设计要求越来越高,最为设计师应如何迎头赶上
一、引言 在当今数字化高速发展的时代,移动设备已经成为人们生活中不可或缺的一部分。随着科技的不断进步和用户需求的日益增长,移动 UI 设计的要求也越来越高。作为移动 UI 设计师,我们面临着巨大的挑战,需要不断提升自己的能力…...
大数据-121 - Flink Time Watermark 详解 附带示例详解
点一下关注吧!!!非常感谢!!持续更新!!! 目前已经更新到了: Hadoop(已更完)HDFS(已更完)MapReduce(已更完&am…...
国行 iPhone 15 Pro 开启苹果 Apple Intelligence 教程
一、前言苹果在 iOS 18.1 测试版上正式开启了 Apple Intelligence 的内测,但国行设备因政策原因不支持,且国行设备在硬件上被锁定。不过,我们可以通过一些方法来破解国行 iPhone 15 Pro,使其能够开启 Apple Intelligence。 以下是…...
KubeSphere 容器平台高可用:环境搭建与可视化操作指南
Linux_k8s篇 欢迎来到Linux的世界,看笔记好好学多敲多打,每个人都是大神! 题目:KubeSphere 容器平台高可用:环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...
CentOS下的分布式内存计算Spark环境部署
一、Spark 核心架构与应用场景 1.1 分布式计算引擎的核心优势 Spark 是基于内存的分布式计算框架,相比 MapReduce 具有以下核心优势: 内存计算:数据可常驻内存,迭代计算性能提升 10-100 倍(文档段落:3-79…...
从零实现STL哈希容器:unordered_map/unordered_set封装详解
本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说,直接开始吧! 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...
2025盘古石杯决赛【手机取证】
前言 第三届盘古石杯国际电子数据取证大赛决赛 最后一题没有解出来,实在找不到,希望有大佬教一下我。 还有就会议时间,我感觉不是图片时间,因为在电脑看到是其他时间用老会议系统开的会。 手机取证 1、分析鸿蒙手机检材&#x…...
BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践
6月5日,2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席,并作《智能体在安全领域的应用实践》主题演讲,分享了在智能体在安全领域的突破性实践。他指出,百度通过将安全能力…...
根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:
根据万维钢精英日课6的内容,使用AI(2025)可以参考以下方法: 四个洞见 模型已经比人聪明:以ChatGPT o3为代表的AI非常强大,能运用高级理论解释道理、引用最新学术论文,生成对顶尖科学家都有用的…...
MySQL账号权限管理指南:安全创建账户与精细授权技巧
在MySQL数据库管理中,合理创建用户账号并分配精确权限是保障数据安全的核心环节。直接使用root账号进行所有操作不仅危险且难以审计操作行为。今天我们来全面解析MySQL账号创建与权限分配的专业方法。 一、为何需要创建独立账号? 最小权限原则…...
C++:多态机制详解
目录 一. 多态的概念 1.静态多态(编译时多态) 二.动态多态的定义及实现 1.多态的构成条件 2.虚函数 3.虚函数的重写/覆盖 4.虚函数重写的一些其他问题 1).协变 2).析构函数的重写 5.override 和 final关键字 1&#…...
Qemu arm操作系统开发环境
使用qemu虚拟arm硬件比较合适。 步骤如下: 安装qemu apt install qemu-system安装aarch64-none-elf-gcc 需要手动下载,下载地址:https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x…...
python爬虫——气象数据爬取
一、导入库与全局配置 python 运行 import json import datetime import time import requests from sqlalchemy import create_engine import csv import pandas as pd作用: 引入数据解析、网络请求、时间处理、数据库操作等所需库。requests:发送 …...
