1、 创建新的project
选择Maven工程
对项目命名
选择自动下载maven中的jar包
2、pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>MySpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<!--
spring-boot-starter:spring-boot场景启动器,帮我们导入了web模块正常运行所依赖的组件
SpringBoot将所有功能场景都抽取出来,做成了一各个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入出来,要用什么功能就导入什么场景的启动器
-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
我这里pom.xml抛出了一个异常
解决办法:1、打开配置 2、把自己本地的Maven仓库settings.xml加进去后,选择apply后,ok就好了
3、编写代码,将java文件配置成Sources Root,选中java文件右键点击Make Directory as,打开后选择Sources Root即可,这个时候java前面的灰色图标会变成蓝色的。之后在java文件下创建包和java文件,按照下图目录
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // 标注一个主程序类,说明这是一个SpringBoot应用
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class,args); // Spring应用启动起来
}
}
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
7、运行项目,直接在main方法运行即可。
执行结果
8、其他:将应用打包成一个可执行的jar包
在pom.xml中增加配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
jar 包可以直接在命令行运行
java -jar /Users/xxx/spring-test01/MySpringBoot/target/MySpringBoot-1.0-SNAPSHOT.jar
POM文件
父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
SpringbBoot的仲裁中心 <name>Spring Boot Dependencies</name>
,它来管理SpringbBoot的所有依赖版本,所以导入依赖的时候可以不写版本,在dependencies里面管理的依赖自然会声明版本号
启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
springboot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器
网友评论