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, ifspring-webmvc
is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up aDispatcherServlet
.@ComponentScan
: Tells Spring to look for other components, configurations, and services in thecom/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 package
打jar
包了,直接用./mvnw spring-boot:run
运行。
这次学习到了win11的Intellij Idea
自带的HTTP Client
,可以用来生成Get
Post
等Request
。
示例如下: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 Request
在Intellij Idea
的.idea
文件夹下
这个教程实现了后端与MySQL的交互,实现了向MySQL存入数据,从MySQL读取数据。并且对注解有了更深入的理解,了解了HTTP Request,如GET POST。是很实用的教程。