CGB phase III DAY01
2021.6.23
1. Environment Version Description
1.1 IDEA version
1.2 Mysql version
You can use the latest version of mariadb. If you use version 5.5 in class, you'd better not use mysql8 0 version
2. IDEA environment configuration
2.1 IDEA environment description
Workspace: in theory, you can store multiple projects
Programmer's Ethics: there should be no Chinese, spaces or special characters in the path
2.2 see PPT for idea environment construction
2.3 IDEA plug-in description
2.3.1 community version
Note: the community version does not support Spring/JS/HTML You need to install specific plug - ins for SpringBoot
Install Spring Assistant in advance. The plug-in is not available in the official version
2.3.2 official version
Note: the official version comes with a SpringBoot plug-in So there is no need to download
2.3.3 Lombok installation
Note: when creating an entity object, you must write methods such as get/set/toString / construct / equals/hash, which must have
In order to optimize the above operations, lombok plug-in (mainstream usage) is introduced
Early installation:
2.4 introduction to springboot project
2.4.1 role of framework
- Spring MVC function: receive the data submitted by the user and return the server data (Interactive)
- Role of Spring: integrate other third-party frameworks, so that program calls can be called in a unified way (integration)
- Mybatis function: integrate JDBC to facilitate user interaction with database (persistence)
- Role of SpringBoot: simplify the operation of Spring and other frameworks (Simplified)
Understanding: SpringBoot is a framework's high-level API
2.4.2 relationship between frameworks
2.5 creating a SpringBoot project
2.5.1 create project
2.5.2 selection dependency
2.5.3 SpringBoot error (I)
2.5.3 SpringBoot error (II)
3 create SpringBoot advanced usage
3.1 description of maven coordinates
3.1.0 Maven common commands
3.1.1 description of Maven common commands
- install project packaging command
- clean clear the target file directory
Note: clean will only empty the contents of the target file directory, and the files in the local warehouse will not be cleaned
So you have to re install every time the project is packaged
3.1.2 coordinates
<!--Description of coordinates: As long as it's one Maven The project must have coordinates--> <!--groupId group ID: Reverse writing of company domain name--> <groupId>com.jt</groupId> <!--Project name is unique--> <artifactId>springboot_demo1</artifactId> <!--edition: You can specify it yourself--> <version>0.0.1-SNAPSHOT</version>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
3.1.3 function
- The path that uniquely identifies the project
- The coordinates are where future projects will be packaged
- The modified jar package file can be relied on by other projects
3.2 about POM XML file description
3.2.1 knowledge paving
Note: as a large-scale project development, it is bound to introduce a lot of jar package files (public third party, own company's jar package) Because there are many jar package files The jar package file conflict may be caused!!!
Q: how to solve it?
3.2.2 parent label description
In 2015, SpringBoot framework came into people's view, the most important of which is the definition of parent tag The parent tag, which is maintained on the Spring official website, defines the version information of almost all frameworks compatible with SpringBoot
Function: centralized definition of version number
<!--Centralized definition version number--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.1</version> <relativePath/> <!-- lookup parent from repository --> </parent>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
3.3.3 dependencies
3.3.4 function of build
3.3 description of SpringBoot configuration file
3.3.1 description of properties file
3.3.2 YML document description
3.4 introduction cases
3.4.1 case requirements
1. Edit HelloController
2. Request path http://localhost:8090/hello
3. Get the result and get the words "welcome to SpringBoot"
3.4.2 package location
matters needing attention!!!: After writing code, you must edit it in the same package and sub package of the main startup class
3.4.3 introduction cases
//History: the Controller annotation is generally used when the back-end page jumps //Current: front and back ends are separated, pages are not managed by the back end, and RestController // RestController = Controller + @ResponseBody //Interview questions: @ Controller and @ RestController //@Controller //@ResponseBody @RestController public class HelloController {<span class="token comment">/*The annotation is bound to the method */</span> <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/hello"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token string">"Hello! SpringBoot"</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
3.4 assigning values to attributes
3.4.1 business requirements
Note: if the data is written in a java file, it needs to be modified manually if the data changes Java file If there are a lot of requirements in the future, it is inconvenient to expand
Solution: assign values to attributes dynamically
3.4.2 attribute assignment in YML mode
3.4.2.1 defining attributes
3.4.2.2 defining attributes
package com.jt.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//History: the Controller annotation is generally used when the back-end page jumps
//Now: the front and back ends are separated, and the page is not managed by the back end. RestController
// RestController = Controller + @ResponseBody
//Interview questions: @ Controller and @ RestController
//@Controller
//@ResponseBody
@RestController
public class HelloController {
<span class="token comment">//private String name = "Li Si"</ span> <span class="token comment">/** * Expression: springel expression is called spel expression for short * Syntax: ${expression content} * working principle: * Container: a Map collection that stores a large amount of data in memory * 1.When the SpringBoot program starts, first load application Configuration file for YML * 2.When the program loads the key value structure, it saves the data to the Map collection (inside the container) * 3.Using Spel expression, get value through key, and then assign value to the attribute */</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${userinfo.name}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span> <span class="token comment">/*The annotation is bound to the method */</span> <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/hello"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token string">"Hello! SpringBoot:"</span><span class="token operator">+</span>name<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
3.5 pro attribute assignment
3.5.1 description of YML documents
The main function of YML file: edit the integration of SpringBoot and third-party framework
If a large amount of business data is written into YML file, it will cause the coupling between business and framework Inconvenient to manage
Idea: can business data be optimized Keep the code clean
Implementation strategy: realize business decoupling through the configuration file of properties
3.5.2 Edit pro configuration file
3.5.3 data injection
-
Annotation in class
-
Assign values to attributes