Table of contents
2. Creation of SpringBoot project
1. What is Spring Boot?
Learning the Spring framework is to simplify Java development, and SpringBoot was born to simplify Spring development
Advantages of Spring Boot:
1. Rapid integration framework: you can quickly add external jar packages and directly add the required dependencies to the project
In the past, it was necessary to open the warehouse search framework first, correspond to the version, assign the dependent content, and paste it into the dependency of the .pom file, and the version is easy to mismatch, and errors will occur
Directly check the required dependencies to help us complete these tedious tasks
2. Run and deploy the program directly, built-in running container, no need to install some plug-ins
3. Quickly deploy projects, start and run projects without external containers
4. Can be developed using annotations, no xml configuration files are required
5. Support more monitoring indicators to better observe the operation of the project
2. Creation of SpringBoot project
IDEA create project
The professional version of IDEA does not require the installation of plug-ins
In the community version of IDEA, you need to install the plug-in first to create a SpringBoot project. The SpringBootHelper plug-in below version 2022 is free
Click to create new project
Plugin installed successfully
Once the plugin is installed, the project can be created
Click Spring Initializr
SDK, choose a stable version of JDK, such as version 1.8
In the initialization selection box, you need to pull the official data of spring, which needs to be connected to the Internet
There is a loading time in the middle, and the next interface will appear, which is pulling data
Then the project parameter settings appear:
Click next to select the springboot version and dependencies
Click next, select the name and path
Then select Maven Support
load the whole project
It is slow to create a project for the first time, because you need to download these jar packages, and create it again, you will directly use the local jar package, and it will be very fast
If an error is reported, or the dependency cannot be loaded, there is a high probability that Maven's domestic source is wrong, and the configuration needs to be checked
Click to run, run successfully
The console prints the time-consuming project startup, which proves that it has run successfully
Web version create project
Let's learn how to create a SpringBoot project on the web version
Jump to this interface
Select the same as our above configuration requirements
Then add dependencies, search for dependencies
Search to add three more commonly used dependencies
Click to download the project locally
This is equivalent to a project package. After decompressing it, it can be loaded into IDEA. Every time you can use this file to decompress to get the project, you can modify some information in pom.xml
Open it with IDEA after decompression
Also need to add framework support
After loading the dependencies, click Run
3. Use of Spring Boot
Project directory structure
All files of the project can be seen in the directory structure
The .mnv file is not needed, IDEA already has a visual Maven command window tool
HELP.md is a help document, which comes with the project after creating it, and does not need it
mvnw is used in the linux environment, and .cmd is used in the windows environment. You can use these commands to operate the project, and you can use IDEA's visual tools to operate
These four files are unnecessary. They can be deleted, and the project can start normally after deletion.
We explain the remaining directories:
Open the target file, which is the .class file required by the JVM to run, and can also be deleted, because a target file will be generated every time the project is started
test
Let's test the springboot project
The test class should be written under the demo or under the demo subpackage
Spring will only scan whether the classes under the package where the startup class is located are annotated, and will not scan other places. Create classes in other packages, and add class annotations will not be scanned! This is Spring's agreement
Remember to write the @Controller annotation, otherwise the class will not be loaded into spring
@Controller
@ResponseBody //当前类的所有方法都是返回非静态页面数据
public class Test {
@RequestMapping("/sayHello")//=@Webservlet(/url)
public String sayHello(){
return "Hello World";
}
}
After starting the project, access it in the browser