本人介绍两种方式
1、maven 创建 2、idea创建
1:maven 工具直接创建
maven 安装包自行下载。
maven maven 3.6.1
1.1 maven 执行命令
mvn archetype:generate -DgroupId=com.xiaowen -DartifactId=chapter01 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
成功截图
创建成功后用idea
1.2用idea 2023.打开
1.3 添加测试代码
1.3.1 pom文件
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xiaowen</groupId>
<artifactId>chapter0102</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>chapter0102</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.3.2 添加启动类
这里介绍两种方式启动
1.3.2.1 方式一 用组合注解 @SpringBootApplication
package com.xiaowen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
// System.out.println( "Hello World!" );
SpringApplication.run(App.class,args);
}
}
1.3.2.2 使用 方式一二用组合注解 @EnableAutoConfiguration,@ComponentScan
package com.xiaowen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
1.3.3 添加测试Controller文件
注意加上RestController注解
package com.xiaowen.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello(){
return "Hello,Spring Boot";
}
@GetMapping("/hello1")
public String sayHello1(){
return "Hello,Spring Boot1";
}
}
1.4启动
启动项目
下面将介绍两种常用的控制台启动项目的方式。
1.4.1使用 Maven 启动项目 mvn spring-boot:run。
1.4.2 运行 jar 包启动项目
执行mvn package 然后使用java -jar 执行target 下生成的jar文件
2、idea创建
选择File->Project->Spring Initializr 后按如图所示创建项目
2.1 步骤一
注意选择maven和对应的jdk版本
2.2、步骤二
注意springboot java的最后一个版本是2.7.18
选择Spring boot 2.7.18 Web中选择Spring Web
2.3、步骤三
创建完毕