[Java Web] 011 -- SpringBootWeb comprehensive case (delete/modify employees, file upload, configuration files)

Table of contents

1. New employees

1. Demand analysis

2. Code implementation

2. File upload

1. Introduction (basic example of file upload)

①. File upload front-end page request

②、multipart/form-data

③, the server receives the file

④, code implementation

⑤ Summary

2. Local storage (rarely used)

①. Implementation ideas

②. Implementation steps

③. Default file upload size

3. Alibaba Cloud OSS (Object Storage Service)

① Introduction to Alibaba Cloud

②. Object Storage Service (OSS)

③. General ideas for using third-party services

④. Third-party service--preparation work

⑤. Third-party services -- refer to the SDK and write examples

⑥, third-party services -- case integration OSS

⑦ Summary

3. Modify employees

1. Query echo

①. Implementation ideas

②, code implementation

2. Modify employees

①. Implementation ideas

②, code implementation

4. Configuration file (Application.properties)

1. Problem analysis (hard-coded problem of Alibaba Cloud OSS configuration information)

2. Parameter configuration

3. yml configuration file (recommended)

①, configuration format

②, configuration file priority

③, the basic syntax of the yml configuration file

④, yml data format

⑤, yml configuration file replaces properties

⑥ Summary

4、@ConfigurationProperties

①, problem analysis

②. Solution (@ConfigurationProperties(prefix = "aliyun.oss"))

③, solve the warning

④、@ConfigurationProperties与@Value


1. New employees

1. Demand analysis

Specific implementation ideas:

2. Code implementation

The specific implementation process:

Ⅰ. Controller method:

Ⅱ. Service method:

service interface:

Service implementation class:

Ⅲ. Mapper method:

2. File upload

1. Introduction (basic example of file upload)

①. File upload front-end page request

File upload front-end page ( upload.html ):

Submit the form to view request information:

Request header data:

Request payload (payload):

②、multipart/form-data

After using multipart/form-data, the content will be separated by the data after the boundary

③, the server receives the file

In order to be recognized and received, the name of the form item in the front-end must be consistent with the name of the formal parameter of the back-end method

④, code implementation

Ⅰ. Controller method:

Ⅱ. Front-end page:

Ⅲ. Run the test:

Server-side code breakpoint:

The currently uploaded file is just a temporary file (3 form items):

But as long as the response to the file upload request is completed, the temporary file will be automatically deleted, so how to save the file requires the content of the following two sections.

⑤ Summary

2. Local storage (rarely used)

①. Implementation ideas

 

②. Implementation steps

Ⅰ. Store the file in the disk directory of the server: E:\images :

Ⅱ. Postman test:

send request:

Ⅲ. Optimization (to solve the problem of duplication of uploaded file names):

Construct a unique file name (cannot be repeated): uuid (universal unique identifier, essentially a fixed-length and unique string)

③. Default file upload size

In SpringBoot, the default file upload size is up to 1M

Modify the configuration file ( Application.properties ):

3. Alibaba Cloud OSS (Object Storage Service)

① Introduction to Alibaba Cloud

②. Object Storage Service (OSS)

③. General ideas for using third-party services

Preparation -> refer to the official SDK, write the entry program -> case integration OSS

④. Third-party service--preparation work

Create buckets:

Get AccessKey (secret key):

Create an AccessKey:

⑤. Third-party services -- refer to the SDK and write examples

Official Document Java Version: Introduction to Multiple Installation Methods of OSSJavaSDK_Object Storage-Alibaba Cloud Help Center

Upload file stream sample code :

We only need to change the relevant content to our own, without changing the core code

After the corresponding modification:

⑥, third-party services -- case integration OSS

Implementation ideas:

Implementation steps:

Example:

Ⅰ. Introduce Aliyun OSS file upload tool class (AliOSSUtils.java):

Ⅱ. Modify the Controller method:

⑦ Summary

这里只说本地存储的缺点,而不说云存储的感觉有些不太客观,像云存储也存在其自身的缺点,像(需要有联网环境、收费、云服务一旦崩掉,资源无法访问等)

三、修改员工

1、查询回显

①、实现思路

②、代码实现

具体实现步骤:

Ⅰ、Controller方法(getById):

Ⅱ、service方法:

service接口:

service实现类:

Ⅲ、Mapper方法:

Ⅳ、Postman测试:

2、修改员工

①、实现思路

②、代码实现

具体实现步骤:

Ⅰ、Controller方法:

Ⅱ、service方法:

service接口:

service实现类:

Ⅲ、Mapper方法:

XML映射文件:

Ⅳ、Postman测试:

数据库表:

四、配置文件(Application.properties)

1、问题分析(阿里云OSS配置信息的硬编码问题)

问题解决:(@Value:外部配置的属性注入)

示例:

2、参数配置化

将所有的配置参数交给application.properties配置文件统一管理:

3、yml配置文件(推荐使用)

①、配置格式

application.yml

常见配置文件格式对比:

②、配置文件优先级

③、yml配置文件的基本语法

④、yml数据格式

示例:

⑤、yml配置文件替换properties

示例:(配置完成后,将properties文件备份一下,即可将其从项目中删除)

⑥、小结

4、@ConfigurationProperties

①、问题分析

②、解决方案(@ConfigurationProperties(prefix = "aliyun.oss"))

解决办法:将其打上注解@ConfigurationProperties(prefix = "aliyun.oss"),让其实现自动注入:

示例:

Ⅰ、创建AliOSSProperties.java实体类),将其打上@ConfigurationProperties注解:

Ⅱ、在AliOSSUtils.java文件中,实现AliOSSProperties对象的自动注入:

③、解决警告

解决警告办法:(可选操作)

在AliOSSProperties.java上加入注解出现了以下警告:

该警告就是提醒我们还缺少了一项依赖:

这项依赖的作用就是会自动识别被@ConfigurationProperties这个注解标识的这个bean对象,然后在配置文件当中提示与这个bean对象的属性名相对应的名字

 

④、@ConfigurationProperties与@Value

@Value:单个注入

@ConfigurationProperties:批量注入

 

Guess you like

Origin blog.csdn.net/qq_41071754/article/details/130035450