使用idea创建Spring Boot项目

Dear 丶 2021-12-19 23:57 476阅读 0赞

1、 选择文件创建: file->new ->Project->Spring Initializr看到以下界面,选取蓝色标记的一列,注意的是我们也可以使用它的默认网站进行生成,网址就是http://start.spring.io。选中点击next即可。
在这里插入图片描述
2、在Group栏输入组织名,Artifact就是项目名。其它选择默认就好,直接Next
在这里插入图片描述
3、 目前我们只需要勾选上WEB依赖即可,其他的依赖设置后边在进行讲解。这时候我们一个SpringBoot就生成完毕,可以进行简单的WEB应用开发。
在这里插入图片描述
4、选择finish完成即可他会自动下载相应的文件配置(需要联网)
在这里插入图片描述
5、生成以后项目的结构图如下:
在这里插入图片描述
6、打开会看到有个main方法以及一句代码。这就是默认启动类在这里插入图片描述

  1. package com.gx.myfirstspringboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class MyFirstSpringbootApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(MyFirstSpringbootApplication.class, args);
  8. }
  9. }

7、点击旁边的小三角可以启动看看是否能运行
在这里插入图片描述
当在运行窗口看到: Tomcat started on port(s): 8080 (http) with context path ‘’就说明启动成功了
在这里插入图片描述
访问:http://localhost:8080出现404,这是springboot为我们自动生成的错误页面/error,因为此时我们还没有创建一个Controller接口
在这里插入图片描述
我在myfirstspringboot同级创建了一个包Controller写一个接口映射类testController,然后重新访问http://localhost:8080/test,浏览器输出 hello spring boot! 表示项目可以成功访问!
在这里插入图片描述

因为和myfirstspringboot同级所以需要注解@ComponentScan(basePackages= {“com.gx.controller”})指定包中类的位置(也可以把入口文件MyFirstSpringbootApplication移动到包的更目录就不需要使用该注解了)

MyFirstSpringbootApplication文件

  1. package com.gx.myfirstspringboot;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. @SpringBootApplication
  7. @ComponentScan(basePackages= { "com.gx.controller"})
  8. public class MyFirstSpringbootApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(MyFirstSpringbootApplication.class, args);
  11. }
  12. }

testController:测试文件

  1. package com.gx.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. @Controller
  6. public class TestController {
  7. @RequestMapping("/test")
  8. @ResponseBody
  9. public String test() {
  10. System.out.println("hello Spring Boot");
  11. return "hello Spring Boot!";
  12. }
  13. }

在这里插入图片描述

需要导入包
在这里插入图片描述
pom.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.gx</groupId>
  12. <artifactId>my-first-springboot</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>my-first-springboot</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. <dependency>
  30. <!--Thymeleaf麝香叶jar 包-->
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  33. </dependency>
  34. </dependencies>
  35. <build>
  36. <plugins>
  37. <plugin>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-maven-plugin</artifactId>
  40. </plugin>
  41. </plugins>
  42. </build>
  43. </project>

在这里插入图片描述

创建完成后的多出来的这几个文件的作用,其中 HELP.md md 、mvnw linux 、mvnw.cmd 这三个基本上都是用不到的 看着不舒服可以直接删掉哦!!!介绍一下各个文件的作用.

.gitignore 用git做版本控制时 用这个文件控制那些文件或文件夹 不被提交(不用git的话可删除 没影响)
HELP.md md是一种文档格式 这个就是你项目的帮助文档(可删除 没影响)
mvnw linux上处理mevan版本兼容问题的脚本(可删除 没影响)
mvnw.cmd windows 上处理mevan版本兼容问题的脚本(可删除 没影响)
spring-boot.iml 是IDEA特有的文件每个导入IDEA的项目都会生成一个项目同名的 .iml文件 用于保存你对这个项目的配置 (删了程序重新导入后还会生成 但由于配置丢失可能会造成程序异常)

发表评论

表情:
评论列表 (有 0 条评论,476人围观)

还没有评论,来说两句吧...

相关阅读