Creation and use of SpringBoot

Table of contents

 

1. What is Spring Boot?

2. Creation of SpringBoot project

IDEA create project

Web version create project

3. Use of Spring Boot

Project directory structure

test


 

 

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

b54a6245a23440448c36da808743a2d9.png

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

0165de51d7a148ada4fd8e497a774d2b.png

Click to create new project

8f7542586e4247ff954ea8bf5491039c.png

Plugin installed successfully 

Once the plugin is installed, the project can be created

acab88a717a3451f833a94e1107a522b.png

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:

d4b5d200fa05416e9a7960ddf40f9397.png

 Click next to select the springboot version and dependencies

e76c1e7533ad4ff7b5f51c6a5ab1e4a0.png

Click next, select the name and path

6dba23479ef742db9244e7d6a9bb51f5.png

 Then select Maven Support

63d77446e7b44a7498e89e5753e7d391.png

fda43f0d66cf46d8b357df12cc40f799.png load the whole project

0b89eaf781f24006b180c495c898c388.png

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 

2ddab92927c74b66a920033d391eef0f.png

Web version create project

Let's learn how to create a SpringBoot project on the web version

6ae7e19f6c69430396f4cb95243b26ed.png

Jump to this interface

dda274c1e6a545c795fb593a72995ac0.pngSelect the same as our above configuration requirements

4171b8a2d81a46958552197b811f2b67.png

Then add dependencies, search for dependencies

8800b71c54b04211a0d8217ff0f037a1.png

4bfe26f408cc441c986413c65deffebe.png Search to add three more commonly used dependencies

b1b615969d4b4ab38bde0d6b02f65a2e.png Click to download the project locally

fe9eb12ac5be4cc08f0f904392ed92d7.png

3519b4f321894d71a46d66e6cdca3d0f.png

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

4ea86e7288ca40cf8414aa3ce6e8690a.png

b384cd8dd33f4a1f8ba354275c244980.png

Open it with IDEA after decompression

76243ee209514bb6928001cf41bd1ed6.png

cba74f1426a0400b83a9c1a6d5eefe88.png

Also need to add framework support

569f31ea5b2f45e79b60310ee0e8de4b.png

After loading the dependencies, click Run

b2fddf7874684d9a868609e93299b7c2.png

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:

6e9116b7dcd040049e8f230694bc895a.png

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 

580778a1f553483cb93c5350f9fece97.png

test

Let's test the springboot project

The test class should be written under the demo or under the demo subpackage

e36a75f5008c48399fc6c09e91efec1e.png

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

acfb8a91a2bb489db147c6705ce0c437.png

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/chenchenchencl/article/details/130260316