Build the first SpringBoot project-hello world

Method 1: Use Spring Initializr on the official website to build

(1) Enter the official website springBoot
(2) Select Initializr to build quickly
Insert picture description here
(3) Fill in the project information, create
Insert picture description here
(4) Download the zip, unzip it and import it into IDEA
Insert picture description here

  • Note, select Import, Maven project

Insert picture description here

(5) After importing, remember to set maven as your own warehouse. Idea will automatically download the relevant jar package and wait patiently

Insert picture description here

  • Delete some useless files:
    Insert picture description here
  • It is possible that spring-boot-maven-plugin will be marked in red. Please search for the version you want in the maven repository and add the version number
    Insert picture description here

(6) Successfully created
Insert picture description here

Method 2: Build directly with IDEA

Insert picture description here
Insert picture description here
Insert picture description here

  • When creating a project, it is best to remove the package. Project name, otherwise you can only rename it after it is created (personal habit)

Insert picture description here

1. Project structure

Project structure analysis:

Complete the creation of the basic project through the above steps. The following files will be automatically generated.

1. The main startup class of the program

2. An application.properties configuration file

3. A test class

4. A pom.xml

Insert picture description here

  • Controller, dao, pojo, service are added manually

Introduction to the structure of the pom file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.LinXIaoDe</groupId>
    <artifactId>springboot-01-helloworld</artifactId>
    <version>1.0.0</version>
    <name>springboot-01-helloworld</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>14</java.version>
    </properties>

    <dependencies>
        <!-- web场景启动器 -->


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--对于spring-boot-starter所有的springboot依赖都是使用这个开头比如说:
                                            spring-boot-starter-secrity-->
        </dependency>

        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>

            <!-- 剔除依赖 -->
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <!-- 打包用的插件 -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. Add Helloworld interface and access

  • Create a new controller package in the same level directory of the main function, and create a new hello interface in the package

Insert picture description here

//helloworld接口,返回字符串
@Controller
@RequestMapping("/hello")
public class hello {
    
    
    @GetMapping("/hello")
    @ResponseBody
    public String  hello() {
    
    
        return "Hello World!!";
    }
}

  • Start, visit:

Insert picture description here

3. Modify the port number

Insert picture description here

# 更改项目的端口号
server.port=8080

Four. Customize the banner

Insert picture description here

Five. Packaged into jar

Insert picture description here
Insert picture description here

  • java -jar .\springboot-01-helloworld-1.0.0.jar Run the jar package

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44307065/article/details/108166985