Spring官网教程——Accessing data with MySQL实践

Spring官网教程——Accessing data with MySQL
https://spring.io/guides/gs/accessing-data-mysql/

在这里插入图片描述

打算从今天开始,每天看一个Spring官方的教程,熟悉一下各种注解和格式。做一些笔记。都是比较基础的部分。对了,期间也会借助ChatGPT辅助整理知识点以及增进理解。

今天的这个项目是连接MySQL数据库进行操作的,使用了Spring Data JPA

这是一个很常用的部分,增删改查。

Spring官网很nice,给出的代码有complete,即最后完成的样子,也有初始的initial文件,下面可以自己动手一步步实现。

我们先偷看一眼完成后的样子:
在这里插入图片描述

可以看到三个类,一个接口,以及配置application.properties

再看一眼initial
在这里插入图片描述

就从这里开始吧。

第一步,创建数据库

我们按照它的指示从命令行进入(下面是windows的操作)

mysql -u用户名 -p密码

在这里插入图片描述

登录成功:
在这里插入图片描述

建立新的数据库

create database db_example; -- Creates the new database
create user 'springuser'@'%' identified by 'ThePassword'; -- Creates the user
grant all on db_example.* to 'springuser'@'%'; -- Gives all privileges to the new user on the newly created database

键入后如下
在这里插入图片描述

第二步 修改application.properties文件

Spring boot 默认使用内置的H2 Database, 我们想用 MySQL,那就得告诉它(通过修改application.properties)

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true

官方对spring.jpa.hibernate.ddl-auto=update中后置的选项有四种:none:不做修改, update:更新根据给定实体结构, create:每次初始创建数据库,关闭时销毁, create-drop:创建数据库,SessionFactory closes 时drop

H2 和 其他内置的数据库,默认是create-drop , MySQL 是none

第三步 创建Entity模型

Create the @Entity Model

就是创建一个实体类,这里创建了User
在这里插入图片描述

可以看到,类上面有一个注解@Entity,是用来完成实体类和表的映射,加了这个注解的时候,类的左边会出现一个数据库还有一个表的图标。

第四步 创建Repository

Create the Repository

这里其实我们只创建了一个接口UserRepository
在这里插入图片描述

如官网所言

Spring automatically implements this repository interface in a bean that has the same name (with a change in the case — it is called userRepository).

ChatGPT翻译

Spring自动地在一个同名的bean中实现了这个仓库接口(大小写有所改变——它被称为userRepository)。

第四步 创建Controller

Create a Controller

Controller 是用来处理HTTP Requests的,
在这里插入图片描述

Contorller这里,可以学到很多。

@GetMapping就像是别人要从我这里拿走什么,那我就返回给它一些东西,返回的时候用@ResponseBody包装一下,以告诉对方这是你要的

@PostMapping就像是一个投递员把东西放进了你的柜子,你要打开它,发现里面是@RequestParam指明的两个东西,于是把它们存起来,最后告诉投递员东西我收到了。

它们后面跟的path就是路径,最后我们回复的信息或者东西的去向。
在这里插入图片描述

如果不想明说是哪类请求,可以用@RequestMapping注解

第五步 创建启动类AccessingDataMysqlApplication

Create an Application Class

这里比较容易,但是用到了注解@SpringBootApplication,这是每个教程里都会反复解释的,于是我们认真看一下。
在这里插入图片描述

@SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration: Tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.
  • @ComponentScan: Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.

ChatGPT翻译如下

@SpringBootApplication 是一个便利注解,它添加了以下所有内容:

  • @Configuration: 将该类标记为应用程序上下文中bean定义的来源。
  • @EnableAutoConfiguration: 告诉Spring Boot根据类路径设置、其他bean和各种属性设置开始添加bean。例如,如果spring-webmvc在类路径上,此注解将应用程序标记为Web应用程序并激活关键行为,例如设置DispatcherServlet
  • @ComponentScan: 告诉Spring在com/example包中查找其他组件、配置和服务,使其能够找到控制器。
第六步 测试

由于在本地运行,就不使用./mvnw clean packagejar包了,直接用./mvnw spring-boot:run运行。

这次学习到了win11的Intellij Idea自带的HTTP Client,可以用来生成Get PostRequest

示例如下:1.Tools 下找到 HTTP Client 下,此处官网给出了curl的格式,就用最后一项转换一下。
在这里插入图片描述

在出现的界面输入:

curl http://localhost:8080/demo/add -d name=First -d email=someemail@someemailprovider.com

就会对应生成一个Post请求
在这里插入图片描述

运行一下,有如下输出
在这里插入图片描述

可以看到,与我们在MainController 中有@PostMapping注解的addNewUser方法里返回值——Saved

这个时候,我们换个方式再来测试一下,在浏览器输入

localhost:8080/demo/all

对应了MainController 中带有@GetMapping注解的getAllUsers方法,其返回值是所有的user。
在这里插入图片描述

正好就是我们刚才添加的。

此外,也不难发现,浏览器里的路径/all也就是@GetMapping后的(path="/all")

最后,这些HTTP RequestIntellij Idea.idea文件夹下
在这里插入图片描述

这个教程实现了后端与MySQL的交互,实现了向MySQL存入数据,从MySQL读取数据。并且对注解有了更深入的理解,了解了HTTP Request,如GET POST。是很实用的教程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值