Method 1: Use Spring Initializr on the official website to build
(1) Enter the official website springBoot
(2) Select Initializr to build quickly
(3) Fill in the project information, create
(4) Download the zip, unzip it and import it into IDEA
- Note, select Import, Maven project
(5) After importing, remember to set maven as your own warehouse. Idea will automatically download the relevant jar package and wait patiently
- Delete some useless files:
- 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
(6) Successfully created
Method 2: Build directly with IDEA
- 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)
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
- 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
//helloworld接口,返回字符串
@Controller
@RequestMapping("/hello")
public class hello {
@GetMapping("/hello")
@ResponseBody
public String hello() {
return "Hello World!!";
}
}
- Start, visit:
3. Modify the port number
# 更改项目的端口号
server.port=8080
Four. Customize the banner
Five. Packaged into jar
java -jar .\springboot-01-helloworld-1.0.0.jar
Run the jar package