在IDEA中利用脚手架工具搭建项目

  1. 在IDEA中新建项目,项目类型为Spring Initializr,写项目名、存放位置、语言类型、项目类型、JDK版本等。点击下一步。
     

             注意:这里一定要将项目类型改为Maven

  2. 选择SpringBoot版本和需要的起步依赖,创建。

  3. 完成项目搭建

SpringBoot项目结构

接下来我们了解SpringBoot项目的项目结构:

src.main.java

这个目录下存放的是Java代码,在我们写好的包名下,SprinBoot生成了一个启动类,启动类的作用是启动SpringBoot项目,运行启动类的main方法即可启动SpringBoot项目。

src.main.resources

这个目录下存放的是配置文件和页面相关的代码,SpringBoot默认在static目录中存放静态资源,如css、js、图片等等。而templates中存放模板引擎,如jsp、thymeleaf等。

由于SpringBoot极大简化了Spring配置,所以只有一个application.properties配置文件,Spring的自动配置功能使得大部分的配置都有默认配置,该文件的功能是覆盖默认配置信息,该文件不写任何信息都可以启动项目。

启动后默认端口号为8080,我们可以覆盖该配置:

server.port=8888

src.test.java

这个目录下编写的是测试代码

pom文件

  1.SpringBoot项目必须继承spring-boot-starter-parent,即所有的SpringBoot项目都是spring-boot-starter-parent的子项目。spring-boot-starter-parent中定义了常用配置、依赖、插件等信息,供SpringBoot项目继承使用。

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.1.2</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

  2.SpringBoot项目中可以定义起步依赖,起步依赖不是以jar包为单位,而是以功能为单位

<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>
</dependencies>

  3.spring-boot-maven-plugin插件是将项目打包成jar包的插件。该插件打包后的SpringBoot项目无需依赖web容器,可以直接使用JDK运行

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐